Joining on primary key to foreign key helps combine related data from two tables. It connects main records with their linked details.
Joining on primary key to foreign key in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
SELECT columns FROM table1 JOIN table2 ON table1.primary_key = table2.foreign_key;
The primary key is a unique identifier in the main table.
The foreign key in the second table points to the primary key.
SELECT customers.name, orders.order_date FROM customers JOIN orders ON customers.customer_id = orders.customer_id;
SELECT employees.name, departments.department_name FROM employees JOIN departments ON employees.department_id = departments.department_id;
SELECT products.product_name, suppliers.supplier_name FROM products JOIN suppliers ON products.supplier_id = suppliers.supplier_id;
This example creates two tables: customers and orders. It inserts sample data and joins them on customer_id to show each order with the customer's name.
CREATE TABLE customers ( customer_id INT PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, order_date DATE, FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ); INSERT INTO customers VALUES (1, 'Alice'), (2, 'Bob'); INSERT INTO orders VALUES (101, 1, '2024-01-10'), (102, 2, '2024-01-11'), (103, 1, '2024-01-12'); SELECT customers.name, orders.order_date FROM customers JOIN orders ON customers.customer_id = orders.customer_id ORDER BY orders.order_date;
Joining on primary key to foreign key is usually fast because keys are indexed.
Make sure foreign key values exist in the primary key table to avoid missing matches.
Use INNER JOIN to get only matching rows, LEFT JOIN if you want all from primary key table even without matches.
Joining on primary key to foreign key connects related data from two tables.
Primary key uniquely identifies records; foreign key points to it.
This join helps combine main records with their linked details easily.
Practice
Solution
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.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.Final Answer:
To combine related data from two tables based on a unique identifier -> Option CQuick Check:
Join on primary to foreign key = combine related data [OK]
- Thinking join deletes duplicates
- Assuming join creates unrelated combinations
- Confusing join with update or delete operations
Orders with Customers on the primary key CustomerID and foreign key CustomerID?Solution
Step 1: Identify correct keys for join
The primary key in Customers is CustomerID, and Orders has CustomerID as foreign key.Step 2: Match keys in JOIN condition
The join must be ON Orders.CustomerID = Customers.CustomerID to link related records.Final Answer:
SELECT * FROM Orders JOIN Customers ON Orders.CustomerID = Customers.CustomerID; -> Option DQuick Check:
Join on matching CustomerID keys = SELECT * FROM Orders JOIN Customers ON Orders.CustomerID = Customers.CustomerID; [OK]
- Mixing up primary and foreign key columns
- Joining on unrelated columns like OrderDate
- Using wrong table columns in ON clause
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;
Solution
Step 1: Understand join condition
The join matches Employees.EmployeeID to Departments.ManagerID, linking managers to their departments.Step 2: Result of the join
The query returns names of employees who are managers and the names of the departments they manage.Final Answer:
List of employee names who manage departments with their department names -> Option BQuick Check:
Join on manager ID returns managers with departments [OK]
- Thinking it returns all employees regardless of management
- Assuming syntax error due to join condition
- Expecting departments without managers
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;
Solution
Step 1: Check column names in JOIN condition
The Products table has ProductID as primary key, not ID.Step 2: Identify cause of error
Using Products.ID causes an error because that column does not exist.Final Answer:
Column Products.ID does not exist, causing an error -> Option AQuick Check:
Wrong column name in JOIN = error [OK]
- Using wrong or misspelled column names
- Thinking foreign keys can't be joined
- Assuming JOIN type is mandatory
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.
Solution
Step 1: Use LEFT JOIN to include all authors
LEFT JOIN keeps all authors even if they have no matching books.Step 2: Count books per author
COUNT(Books.BookID) counts books; NULLs for authors without books count as zero.Final Answer:
SELECT Authors.Name, COUNT(Books.BookID) FROM Authors LEFT JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Authors.Name; -> Option AQuick Check:
LEFT JOIN + COUNT on foreign key = authors with book counts [OK]
- Using INNER JOIN excludes authors with zero books
- Counting * instead of foreign key column
- Joining tables in wrong order
