Bird
Raised Fist0
SQLquery~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

      Practice

      (1/5)
      1. What is the main purpose of joining tables on a primary key to a foreign key in SQL?
      easy
      A. To create a new table with all columns from both tables without conditions
      B. To delete duplicate rows from a table
      C. To combine related data from two tables based on a unique identifier
      D. To update values in one table using values from another unrelated table

      Solution

      1. Step 1: Understand primary and foreign keys

        A primary key uniquely identifies each record in a table, and a foreign key points to that primary key in another table.
      2. Step 2: Purpose of joining on these keys

        Joining on primary key to foreign key connects related records from two tables, combining their data meaningfully.
      3. Final Answer:

        To combine related data from two tables based on a unique identifier -> Option C
      4. Quick Check:

        Join on primary to foreign key = combine related data [OK]
      Hint: Primary key links uniquely; join combines related rows [OK]
      Common Mistakes:
      • Thinking join deletes duplicates
      • Assuming join creates unrelated combinations
      • Confusing join with update or delete operations
      2. Which of the following SQL JOIN statements correctly joins table Orders with Customers on the primary key CustomerID and foreign key CustomerID?
      easy
      A. SELECT * FROM Orders JOIN Customers ON Orders.OrderID = Customers.CustomerID;
      B. SELECT * FROM Orders JOIN Customers ON Orders.OrderDate = Customers.CustomerID;
      C. SELECT * FROM Orders JOIN Customers ON Orders.CustomerID = Customers.OrderID;
      D. SELECT * FROM Orders JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

      Solution

      1. Step 1: Identify correct keys for join

        The primary key in Customers is CustomerID, and Orders has CustomerID as foreign key.
      2. Step 2: Match keys in JOIN condition

        The join must be ON Orders.CustomerID = Customers.CustomerID to link related records.
      3. Final Answer:

        SELECT * FROM Orders JOIN Customers ON Orders.CustomerID = Customers.CustomerID; -> Option D
      4. Quick Check:

        Join on matching CustomerID keys = SELECT * FROM Orders JOIN Customers ON Orders.CustomerID = Customers.CustomerID; [OK]
      Hint: Join ON foreign key = primary key column names [OK]
      Common Mistakes:
      • Mixing up primary and foreign key columns
      • Joining on unrelated columns like OrderDate
      • Using wrong table columns in ON clause
      3. Given tables Employees (primary key EmployeeID) and Departments (foreign key ManagerID referencing EmployeeID), what will this query return?
      SELECT Employees.Name, Departments.DepartmentName FROM Employees JOIN Departments ON Employees.EmployeeID = Departments.ManagerID;
      medium
      A. Syntax error due to wrong join condition
      B. List of employee names who manage departments with their department names
      C. List of departments without any employee names
      D. List of all employees with all departments regardless of manager

      Solution

      1. Step 1: Understand join condition

        The join matches Employees.EmployeeID to Departments.ManagerID, linking managers to their departments.
      2. Step 2: Result of the join

        The query returns names of employees who are managers and the names of the departments they manage.
      3. Final Answer:

        List of employee names who manage departments with their department names -> Option B
      4. Quick Check:

        Join on manager ID returns managers with departments [OK]
      Hint: Join foreign key to primary key shows related records [OK]
      Common Mistakes:
      • Thinking it returns all employees regardless of management
      • Assuming syntax error due to join condition
      • Expecting departments without managers
      4. Consider these tables:
      Products(ProductID PK, Name)
      Sales(ProductID FK, Quantity)
      Why does this query cause an error?
      SELECT * FROM Products JOIN Sales ON Products.ID = Sales.ProductID;
      medium
      A. Column Products.ID does not exist, causing an error
      B. Foreign key cannot be used in JOIN condition
      C. JOIN syntax is incorrect, missing JOIN type
      D. Sales table must be listed first in FROM clause

      Solution

      1. Step 1: Check column names in JOIN condition

        The Products table has ProductID as primary key, not ID.
      2. Step 2: Identify cause of error

        Using Products.ID causes an error because that column does not exist.
      3. Final Answer:

        Column Products.ID does not exist, causing an error -> Option A
      4. Quick Check:

        Wrong column name in JOIN = error [OK]
      Hint: Verify column names exactly before joining [OK]
      Common Mistakes:
      • Using wrong or misspelled column names
      • Thinking foreign keys can't be joined
      • Assuming JOIN type is mandatory
      5. You have two tables:
      Authors(AuthorID PK, Name)
      Books(BookID PK, Title, AuthorID FK)
      Write a query to list each author with the count of books they wrote, including authors with zero books.
      hard
      A. SELECT Authors.Name, COUNT(Books.BookID) FROM Authors LEFT JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Authors.Name;
      B. SELECT Authors.Name, COUNT(Books.BookID) FROM Authors JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Authors.Name;
      C. SELECT Authors.Name, COUNT(*) FROM Books JOIN Authors ON Books.AuthorID = Authors.AuthorID GROUP BY Authors.Name;
      D. SELECT Authors.Name, COUNT(Books.BookID) FROM Books LEFT JOIN Authors ON Books.AuthorID = Authors.AuthorID GROUP BY Authors.Name;

      Solution

      1. Step 1: Use LEFT JOIN to include all authors

        LEFT JOIN keeps all authors even if they have no matching books.
      2. Step 2: Count books per author

        COUNT(Books.BookID) counts books; NULLs for authors without books count as zero.
      3. Final Answer:

        SELECT Authors.Name, COUNT(Books.BookID) FROM Authors LEFT JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Authors.Name; -> Option A
      4. Quick Check:

        LEFT JOIN + COUNT on foreign key = authors with book counts [OK]
      Hint: Use LEFT JOIN to include all from primary key table [OK]
      Common Mistakes:
      • Using INNER JOIN excludes authors with zero books
      • Counting * instead of foreign key column
      • Joining tables in wrong order