Retrieving data from multiple tables Flashcards
In MySQL what is the default JOIN type?
inner join
What two types of JOIN’s are available in MySQL?
inner and outer JOIN’s
When selecting a column that’s present on multiple tables what should be prefixed when making a selection?
the table name or alias
(customer_id being present on multiple tables)
SELECT orders.customer_id, first_name, last_name
This code has a lot of repeat words, refactor it. Using alias.
- *SELECT** order_id, orders.customer_id, first_name, last_name
- *FROM** orders
- *JOIN** customers ON orders.customer_id = customers.customer_id
- *SELECT** order_id, o.customer_id, first_name, last_name
- *FROM** orders o
- *JOIN** customers ON o.customer_id = customers.customer_id
True or False
These aliases would return the same results
FROM orders AS o
FROM orders o
True
What are the two aliases being used in this query?
- *SELECT** order_id, o.customer_id, first_name, last_name
- *FROM** orders o
- *JOIN** customers c ON o.customer_id = c.customer_id
o and c
True or False
When querying multiple tables, it’s best to:
- SELECT * (from every table)
- then make the relevant JOIN first (using aliases)
- then filter the columns needed.
True
True or False
To join multiple tables in different databases you’d prefix the database in the JOIN statement?
True
Identify the other database being used in this query?
- *SELECT** *
- *FROM** order_items oi
- *JOIN** sql_inventory.products p
- *ON** oi.product_id = p.product_id
sql_inventory
True or False
In MySQL, it’s possible to JOIN a table with itself? What is the benefit?
USE sql_hr;
- *SELECT**
e. employee_id,
e. first_name,
m. first_name AS manager - *FROM** employees e
- *JOIN** employees m ON e.reports_to = m.employee_id
True
Good for making organizational charts shows who reports to whom if the data is available.
How many tables are being joined in this query?
- *USE** sql_store;
- *SELECT**
o. order_id,
o. order_date,
c. first_name,
os. name AS status - *FROM** orders o
- *JOIN** customers c
- *ON** o.customer_id = c.customer_id
- *JOIN** order_statuses os
- *ON** o.status = os.order_status_id
3 tables
orders
customers
order_statuses
In MySQL what is a composite primary key?
A table where two columns define the primary key.
This query is an example of what?
- *SELECT** *
- *FROM** order_items oi
- *JOIN** order_item_notes oin
- *ON** oi.order_id = oin.order_id
- *AND** oi.product_id = oin.product_id
Joint compound condition, more than two columns are the primary keys.
Refactor this query using implicit join syntax
- *SELECT** *
- *FROM** orders o
- *JOIN** customers c
- *ON** o.customer_id = c.customer_id
- *SELECT** *
- *FROM** orders o, customers c
- *WHERE** o.customer_id = c.customer_id
What is a potential peril using an implicit join syntax?
If you forget the WHERE clause you’d end up with a funky cross-join.
True or False
It’s better to use an implicit join statement when executing queries?
True
What two types of OUTER JOINS are available in slq?
LEFT
RIGHT
True or False
In MySQL a LEFT JOIN will include all the data in the left table regardless of any other condition being met?
True
- *SELECT**
c. customer_id,
c. first_name,
o. order_id - *FROM** customers c – all records returned from left table
- *LEFT JOIN** orders o – left join
- *ON** c.customer_id = o.customer_id – regardless if this is true
- *ORDER BY** c.customer_id
What is the left table in this query?
- *SELECT**
c. customer_id,
c. first_name,
o. order_id - *FROM** customers c
- *LEFT JOIN** orders o
- *ON** c.customer_id = o.customer_id
- *ORDER BY** c.customer_id
customers table
What is the right table in this query?
- *SELECT**
c. customer_id,
c. first_name,
o. order_id - *FROM** customers c
- *RIGHT JOIN** orders o
- *ON** c.customer_id = o.customer_id
- *ORDER BY** c.customer_id
orders table
same results returned by an inner default join in this instance
True or False
In MySQL, the position of the left & right tables in outer joins can change the data being returned.
True
True or False
In MySQL the OUTER and INNER keywords are required?
False
JOIN - default inner
LEFT - outer
RIGHT - outer
What is the power of an OUTER JOIN?
Returns result from the left table regardless if the join condition is met. Much like returning null values from a query on products sold and shipped.
As a rule of thumb it’s best practice to avoid RIGHT JOINS in MySQL?
True
Just switch which table is on the left and use a LEFT JOIN
If you are shown a table and are asked to write a query, but the columns can’t be found in any of the tables, what is likely the cause?
Some of the columns have aliases.
- *SELECT**
o. order_id,
o. order_date,
c. first_name AS customer,
sh. name AS shipper,
os. name AS status - *FROM** orders o
- *JOIN** customers c
- *ON** o.customer_id = c.custom
- *ON** o.shipper_id = sh.shipper_ider_id
- *LEFT JOIN** shippers sh
- *JOIN** order_statuses os
- *ON** o.status = os.order_status_id
This query is an example of a?
USE sql_hr;
- *SELECT**
e. employee_id,
e. first_name,
m. first_name AS manager - *FROM** employees e
- *LEFT JOIN** employees m
- *ON** e.reports_to = m.employee_id
self outer join (left)
Refactor this query with the USING clause
- *SELECT**
o. order_id,
c. first_name - *FROM** orders o
- *JOIN** customers c
- *ON** o.customer_id = c.customer_id
- *SELECT**
o. order_id,
c. first_name - *FROM** orders o
- *JOIN** customers c
- *USING** (customer_id)
In order to use the USING clause when joining tables, the columns must:
a. not match
b. match
c. doesn’t matter
b. columns must match and present in both tables.
Refactor this query with the USING statement
- *SELECT** *
- *FROM** order_items oi
- *LEFT JOIN** order_item_notes oin
- *ON** oi.order_id = oin.order_id AND
oi. product_id = oin.product_id
- *SELECT** *
- *FROM** order_items
- *LEFT JOIN** order_item_notes
- *USING** (order_id, product_id)
In MySQL natural joins are what?
- *SELECT**
o. order_id,
c. first_name - *FROM** orders o
- *NATURAL JOIN** customers c
A join controlled by the database schema, can produce bad results best to avoid. It will make the determination is inner, outer (left, right) join is used.
Cross joins are used to?
Join every record from the left table to the right table accepts no conditions.
What type of cross join is this?
SELECT
c.first_name AS customer,
p.name AS product
FROM customers c
CROSS JOIN products p
ORDER BY c.first_name
explicit
What type of fucked up cross join is this?
- *SELECT**
c. first_name AS customer,
p. name AS product – no p alias - *FROM** customers c, orders o
- *ORDER BY** c.first_name
implicit cross join
What are the two outer joins in SQL?
right or left
What does a UNION keyword do in MySQL?
SELECT
order_id,
order_date,
‘Active’ AS status
FROM orders
WHERE order_date >= ‘2019-01-01’
UNION
SELECT
order_id,
order_date,
‘Archived’ AS status
FROM orders
WHERE order_date < ‘2019-01-01’
combine multiple queries into one result set (same or multiple tables), columns in query must be equal