0
0
SQLquery~5 mins

Joining on primary key to foreign key in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA link to the primary key in another table
BA unique identifier for each row in the same table
CA column that stores only numbers
DA column that cannot have NULL values
Which SQL clause is used to combine rows from two tables based on a related column?
AWHERE
BGROUP BY
CJOIN
DORDER BY
In the query: SELECT * FROM A JOIN B ON A.id = B.a_id; which is likely the primary key?
AB.a_id
BNeither is a primary key
CBoth are primary keys
DA.id
What is the main benefit of joining tables on primary key to foreign key?
AFaster data entry
BEnsures data integrity and correct matching
CAllows duplicate rows
DRemoves all NULL values
If a foreign key value does not match any primary key, what happens in an INNER JOIN?
AThe row is excluded from the result
BThe row is duplicated
CThe query fails with an error
DThe row is included with NULLs
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.