SQL - Multiple Tables Flashcards

1
Q

_______ will combine rows from different tables if the join condition is true.

A

JOIN

SELECT *
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
2
Q

_______ will return every row in the left table, and if the join condition is not met, NULL values are used to fill in the columns from the right table.

A

LEFT JOIN

SELECT *
FROM table1
LEFT JOIN table2
ON table1.c2 = table2.c2;

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

_______ is a column that serves a unique identifier for the rows in the table.

A

Primary key

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

_______ is a column that contains the primary key to another table.

A

Foreign key

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

_______ lets us combine all rows of one table with all rows of another table.

A

CROSS JOIN

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

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

_______stacks one dataset on top of another.

A

UNION

SELECT *
FROM table1
UNION
SELECT *
FROM table2;

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

_______ allows us to define one or more temporary tables that can be used in the final query.

A

WITH

WITH previous_query AS (
SELECT customer_id,
COUNT(subscription_id) AS ‘subscriptions’
FROM orders
GROUP BY customer_id
)
SELECT customers.customer_name,
previous_query.subscriptions
FROM previous_query
JOIN customers
ON previous_query.customer_id = customers.customer_id;

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

What is the difference between an INNER JOIN and a LEFT JOIN?

A

LEFT JOIN combines rows from two or more tables, but unlike INNER JOIN, it does not require the join condition to be met.

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

Why is a CROSS JOIN not so useful?

A

It combines every row in one table with every row in another table.

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

You have two tables authors and books. Each book belongs to an author and references that author through a foreign key. If the primary key of the authors table is id, what would be the most sensical name for a foreign key in the books table that references the id column in authors?

A

author_id

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

What is the best definition of a primary key?

A

A unique identifier for each row or record in a given table.

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

In a LEFT JOIN, if a join condition is not met, what will it use to fill columns on the right table?

A

NULL values

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

You have two tables teachers and students. Each student belongs to a teacher. Complete the query to join the tables on the teacher id.

SELECT *
FROM students
JOIN teachers
ON __________________;

A

students.teacher_id = teachers.id

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

UNION allows us to stack one dataset on top of another. T/F

A

T

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

Which keyword would you use to alias recipes.name and chefs.name in the following query’?

SELECT recipes.name __ ‘Recipe’,
chefs.name __ ‘Chef’
FROM recipes
JOIN chefs
ON recipes.chef_id = chefs.id;

A

AS

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

What is the best definition of a foreign key?

A

A column that contains the primary key of another table in the database.