Recall & Review
beginner
What is a primary key in a database table?
A primary key is a unique identifier for each row in a table. It ensures that no two rows have the same value in that column or set of columns.
Click to reveal answer
beginner
What is a foreign key in a database table?
A foreign key is a column or set of columns in one table that refers to the primary key in another table. It creates a link between the two tables.
Click to reveal answer
beginner
Why do we join tables on primary key to foreign key?
We join tables on primary key to foreign key to combine related data from two tables, like matching a customer with their orders.
Click to reveal answer
beginner
Write a simple SQL query to join two tables on primary key to foreign key.
SELECT * FROM Customers JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Click to reveal answer
intermediate
What happens if you join tables on columns that are not primary key and foreign key?
The join might return incorrect or duplicate data because the link between tables is not guaranteed to be unique or valid.
Click to reveal answer
What does a foreign key in a table represent?
✗ Incorrect
A foreign key links to the primary key in another table to connect related data.
Which SQL clause is used to combine rows from two tables based on a related column?
✗ Incorrect
JOIN is used to combine rows from two tables based on a related column.
In the query: SELECT * FROM A JOIN B ON A.id = B.a_id; which is likely the primary key?
✗ Incorrect
A.id is likely the primary key, and B.a_id is the foreign key referencing it.
What is the main benefit of joining tables on primary key to foreign key?
✗ Incorrect
Joining on primary key to foreign key ensures data integrity and correct matching of related rows.
If a foreign key value does not match any primary key, what happens in an INNER JOIN?
✗ Incorrect
In an INNER JOIN, rows without matching keys are excluded from the result.
Explain in your own words why joining tables on primary key to foreign key is important.
Think about how two tables relate to each other in real life, like customers and their orders.
You got /4 concepts.
Describe how you would write a SQL query to join two tables using a primary key and foreign key.
Remember the format: SELECT * FROM Table1 JOIN Table2 ON Table1.key = Table2.key
You got /3 concepts.