Retrieving data from multiple tables Flashcards

1
Q

In MySQL what is the default JOIN type?

A

inner join

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What two types of JOIN’s are available in MySQL?

A

inner and outer JOIN’s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When selecting a column that’s present on multiple tables what should be prefixed when making a selection?

A

the table name or alias

(customer_id being present on multiple tables)

SELECT orders.customer_id, first_name, last_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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
A
  • *SELECT** order_id, o.customer_id, first_name, last_name
  • *FROM** orders o
  • *JOIN** customers ON o.customer_id = customers.customer_id
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True or False

These aliases would return the same results

FROM orders AS o

FROM orders o

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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
A

o and c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False

When querying multiple tables, it’s best to:

  1. SELECT * (from every table)
  2. then make the relevant JOIN first (using aliases)
  3. then filter the columns needed.
A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False

To join multiple tables in different databases you’d prefix the database in the JOIN statement?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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
A

sql_inventory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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
A

True

Good for making organizational charts shows who reports to whom if the data is available.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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
A

3 tables

orders
customers
order_statuses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In MySQL what is a composite primary key?

A

A table where two columns define the primary key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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
A

Joint compound condition, more than two columns are the primary keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Refactor this query using implicit join syntax

  • *SELECT** *
  • *FROM** orders o
  • *JOIN** customers c
  • *ON** o.customer_id = c.customer_id
A
  • *SELECT** *
  • *FROM** orders o, customers c
  • *WHERE** o.customer_id = c.customer_id
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a potential peril using an implicit join syntax?

A

If you forget the WHERE clause you’d end up with a funky cross-join.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or False

It’s better to use an implicit join statement when executing queries?

17
Q

What two types of OUTER JOINS are available in slq?

A

LEFT
RIGHT

18
Q

True or False

In MySQL a LEFT JOIN will include all the data in the left table regardless of any other condition being met?

A

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
19
Q

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
A

customers table

20
Q

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
A

orders table

same results returned by an inner default join in this instance

21
Q

True or False

In MySQL, the position of the left & right tables in outer joins can change the data being returned.

22
Q

True or False

In MySQL the OUTER and INNER keywords are required?

A

False

JOIN - default inner

LEFT - outer

RIGHT - outer

23
Q

What is the power of an OUTER JOIN?

A

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.

24
Q

As a rule of thumb it’s best practice to avoid RIGHT JOINS in MySQL?

A

True

Just switch which table is on the left and use a LEFT JOIN

25
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
26
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)
27
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)
28
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.
29
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)
30
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.
31
Cross joins are used to?
Join every record from the left table to the right table accepts no conditions.
32
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
33
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
34
What are the two outer joins in SQL?
right or left
35
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