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?

A

True

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.

A

True

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
Q

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?

A

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
Q

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
A

self outer join (left)

27
Q

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
A
  • *SELECT**
    o. order_id,
    c. first_name
  • *FROM** orders o
  • *JOIN** customers c
  • *USING** (customer_id)
28
Q

In order to use the USING clause when joining tables, the columns must:

a. not match
b. match
c. doesn’t matter

A

b. columns must match and present in both tables.

29
Q

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
A
  • *SELECT** *
  • *FROM** order_items
  • *LEFT JOIN** order_item_notes
  • *USING** (order_id, product_id)
30
Q

In MySQL natural joins are what?

  • *SELECT**
    o. order_id,
    c. first_name
  • *FROM** orders o
  • *NATURAL JOIN** customers c
A

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
Q

Cross joins are used to?

A

Join every record from the left table to the right table accepts no conditions.

32
Q

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

A

explicit

33
Q

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
A

implicit cross join

34
Q

What are the two outer joins in SQL?

A

right or left

35
Q

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’

A

combine multiple queries into one result set (same or multiple tables), columns in query must be equal