Multiple Tables Flashcards

1
Q

Combining tables manually is time-consuming, what easy sequence does SQL provide?

A

JOIN

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

What does this statement mean?

SELECT *
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id;

A

The SELECT line selects all columns from our combined table
The second line specifies the first table to look into
The third line uses JOIN to combine information from orders to customers
The fourth line tells how to combine two tables. Match the orders tables customer_id column with the customer’s table’s customer_id column.

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

What syntax is used for JOIN in Tables.

A

table_name.column_name

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

Why is table_name.column_name the syntax?

A

Column names are often repeated across multiple tables so to ensure the requests for columns are unambiguous, thus syntax is used.

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

How would the query be written if only the orders table’s order_id column and customers table’s customer_name column were specified, instead of selecting all (*) columns?

A

SELECT orders.order_id
customers.customer_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id;

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

What is a simple JOIN often referred to as?

A

INNER JOIN

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

What is included in the results for a JOIN?

A

Only the rows that match the ON condition.

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

C1 C2 C2 C3
A B = B C
Q W != E R
X Y = Y Z

What would be the result for this INNER JOIN?

A

A B C
X Y Z

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

What type of JOIN does this represent?

table1.c2 = table2.c2

A

INNER JOIN

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

What type of JOIN combines two tables and keep some of the unmatched rows?

A

LEFT JOIN

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

What is the result of this Table as LEFT JOIN

C1 C2 C2 C3
A B = B C
Q W != E R
X Y = Y Z

A

A B C
Q W
X Y Z

The middle rows do not have a match so the final result will keep all rows of the first table but omit and un match row from the second table.

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

What does a LEFT JOIN do?

A

Keep all rows from the first table regardless of whether there is a matching row in the second table.

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

What does this LEFT JOIN statement do?

SELECT *
FROM table1
LEFT JOIN table 2
ON table1.c2 = table2.c2

A

The first line SELECTs all columns from both tables
The second line SELECTs table1 (the left table)
The third line performs a LEFT JOIN on table2 (right table)
The fourth line tells SQL how to perform the JOIN (by looking for matches in column c2)

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

What type of JOIN is used to combine all rows of one table with all rows of another table?

A

CROSS JOIN

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

What does this CROSS JOIN statement do?

SELECT shirts.shirt_color,
pants.pants_color
FROM shirts
CROSS JOIN pants;

A

The first line select the columns shirt_color and pants_color
The third line pulls data from the Table shirts
The fourth line performs a CROSS JOIN with pants.

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

What type of JOIN does NOT require an ON statement?

A

CROSS JOIN

17
Q

The ON statement for INNER JOIN and LEFT JOIN is used for what?

A

Joining columns!

18
Q

What type of operator stacks one dataset on top of the other?

A

UNION

19
Q

The UNION operator deals with how many tables?

A

Two

20
Q

What does this UNION statement do?

table1: table2:
pokemon type pokemon type
Bulbasaur Grass Snorlax Normal
Charmander Fire
Squirtle Water

SELECT *
FROM table1
UNION
SELECT *
FROM table2;

A

pokemon type
Bulbasaur Grass
Charmander Fire
Squirtle Water
Snorlax Normal

21
Q

What are some rules regarding a UNION?

A

Tables must have the same number of columns
The columns must have the same data types in the same order as the first Table.

22
Q

What type of statement is WITH?

A

Allows us to perform separate query

23
Q

What does this WITH statement do?

WITH previous_results AS (
SELECT ….
….
….
)

SELECT *
FROM previous_results
JOIN customers
ON _______ = ________;

A

The WITH statement allows us to perform a separate query
previous_results is the alias that we will use to reference any columns from the query inside of the WITH clause

24
Q

How does the WITH statement work?

A

Putting the first query inside the parentheses ( ) and giving it a name.
After, we can use the name as if its a table and write a new query using the first query.