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 one-to-many relationship in database design?
It is a relationship where one record in a table (the 'one' side) can be related to many records in another table (the 'many' side). For example, one customer can have many orders.
Click to reveal answer
beginner
How do you represent a one-to-many relationship in SQL tables?
By adding a foreign key column in the 'many' side table that references the primary key of the 'one' side table.
Click to reveal answer
beginner
Why do we use foreign keys in one-to-many relationships?
Foreign keys link records between tables, ensuring data integrity by making sure the 'many' side records relate to a valid 'one' side record.
Click to reveal answer
beginner
Example: If you have tables 'Authors' and 'Books', which table should have the foreign key?
The 'Books' table should have a foreign key column referencing the 'Authors' table because one author can write many books.
Click to reveal answer
intermediate
What happens if you try to insert a record in the 'many' side table with a foreign key value that does not exist in the 'one' side table?
The database will reject the insert to maintain data integrity, because the foreign key must reference an existing record in the 'one' side table.
Click to reveal answer
In a one-to-many relationship, where is the foreign key placed?
AIn the table on the 'one' side
BIn the table on the 'many' side
CIn both tables
DNo foreign key is needed
✗ Incorrect
The foreign key is placed in the table on the 'many' side to link back to the 'one' side.
Which SQL constraint ensures that the foreign key value must exist in the referenced table?
APRIMARY KEY
BUNIQUE
CFOREIGN KEY
DCHECK
✗ Incorrect
The FOREIGN KEY constraint enforces that the value must exist in the referenced table.
If one author can write many books, what kind of relationship is this?
AOne-to-many
BOne-to-one
CMany-to-many
DNo relationship
✗ Incorrect
One author writing many books is a classic example of a one-to-many relationship.
What is the main purpose of using a foreign key in one-to-many relationships?
ATo speed up queries
BTo store duplicate data
CTo create indexes
DTo link tables and maintain data integrity
✗ Incorrect
Foreign keys link tables and ensure data integrity by enforcing valid relationships.
Which of these is a valid example of a one-to-many relationship?
AEach customer can place many orders
BEach book has many authors
CEach employee works in many departments
DEach student has one ID card
✗ Incorrect
One customer placing many orders is a one-to-many relationship.
Explain how to design a one-to-many relationship between two tables in a database.
Think about which table holds the foreign key and why.
You got /3 concepts.
Describe a real-life example of a one-to-many relationship and how it would be represented in SQL tables.
Consider something like customers and orders or authors and books.
You got /3 concepts.
Practice
(1/5)
1. What does a one-to-many relationship in a database mean?
easy
A. One record in a table relates to many records in another table
B. Many records in a table relate to one record in the same table
C. One record relates to exactly one record in another table
D. Many records relate to many records in another table
Solution
Step 1: Understand relationship types
A one-to-many relationship means one record in a table connects to multiple records in another table.
Step 2: Match definition to options
One record in a table relates to many records in another table correctly describes this as one record relating to many records in another table.
Final Answer:
One record in a table relates to many records in another table -> Option A
Quick Check:
One-to-many = one record to many records [OK]
Hint: One-to-many means one record links to many records [OK]
Common Mistakes:
Confusing one-to-many with many-to-many
Thinking one-to-many means one record links to one record
Mixing up the direction of the relationship
2. Which SQL statement correctly creates a foreign key for a one-to-many relationship from Orders to Customers?
easy
A. ALTER TABLE Customers ADD FOREIGN KEY (OrderID) REFERENCES Orders(OrderID);
B. ALTER TABLE Customers ADD FOREIGN KEY (CustomerID) REFERENCES Orders(OrderID);
C. ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
D. ALTER TABLE Orders ADD FOREIGN KEY (OrderID) REFERENCES Customers(CustomerID);
Solution
Step 1: Identify the 'many' and 'one' tables
Orders is the 'many' side, Customers is the 'one' side in a one-to-many relationship.
The foreign key should be in Orders referencing Customers, so ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); is correct.
Final Answer:
ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); -> Option C
Quick Check:
Foreign key in 'many' table references 'one' table [OK]
Hint: Foreign key goes in 'many' table pointing to 'one' table [OK]
Common Mistakes:
Placing foreign key in the 'one' table instead of 'many'
Referencing wrong columns between tables
Mixing table names in foreign key definition
3. Given these tables: Customers(CustomerID, Name) Orders(OrderID, CustomerID, Amount) What will this query return?
SELECT Customers.Name, COUNT(Orders.OrderID) AS OrderCount FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID GROUP BY Customers.Name;
medium
A. List of customers who have placed at least one order
B. List of customers with total amount spent on orders
C. List of orders with customer names repeated for each order
D. List of customers with the number of orders each placed, including customers with zero orders
Solution
Step 1: Understand the LEFT JOIN usage
LEFT JOIN keeps all customers, even those without matching orders.
Step 2: COUNT(Orders.OrderID) counts orders per customer
Grouping by customer name counts how many orders each customer has, zero if none.
Final Answer:
List of customers with the number of orders each placed, including customers with zero orders -> Option D
Quick Check:
LEFT JOIN + COUNT = all customers with order counts [OK]
Hint: LEFT JOIN + COUNT counts all, including zero matches [OK]
Common Mistakes:
Thinking COUNT counts total amount spent
Assuming only customers with orders appear
Confusing JOIN types and their effects
4. You wrote this SQL to create a one-to-many relationship:
But you get an error. What is the most likely cause?
medium
A. OrderID should not be primary key in Orders
B. Customers table does not exist or CustomerID is not a primary key
C. Foreign key should be in Customers table, not Orders
D. CustomerID column type must be VARCHAR, not INT
Solution
Step 1: Check foreign key reference validity
Foreign key must reference an existing table and a primary or unique key column.
Step 2: Verify Customers table and CustomerID key
If Customers table or CustomerID primary key is missing, error occurs.
Final Answer:
Customers table does not exist or CustomerID is not a primary key -> Option B
Quick Check:
Foreign key references must exist and be keys [OK]
Hint: Foreign key target must exist and be primary/unique key [OK]
Common Mistakes:
Assuming foreign key can reference non-key columns
Placing foreign key in wrong table
Mismatching data types between foreign key and referenced key
5. You have two tables: Authors(AuthorID, Name) Books(BookID, Title, AuthorID) You want to find authors who have written more than 3 books. Which query is correct?
hard
A. SELECT Name FROM Authors JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Name HAVING COUNT(BookID) > 3;
B. SELECT Name FROM Authors LEFT JOIN Books ON Authors.AuthorID = Books.AuthorID WHERE COUNT(BookID) > 3;
C. SELECT Name FROM Books GROUP BY AuthorID HAVING COUNT(BookID) > 3;
D. SELECT Name FROM Authors WHERE AuthorID IN (SELECT AuthorID FROM Books WHERE COUNT(BookID) > 3);
Solution
Step 1: Join Authors and Books on AuthorID
We join to connect authors with their books.
Step 2: Group by author name and filter by book count
Use GROUP BY Name and HAVING COUNT(BookID) > 3 to find authors with more than 3 books.
Final Answer:
SELECT Name FROM Authors JOIN Books ON Authors.AuthorID = Books.AuthorID GROUP BY Name HAVING COUNT(BookID) > 3; -> Option A
Quick Check:
JOIN + GROUP BY + HAVING filters authors by book count [OK]
Hint: Use GROUP BY and HAVING to filter by count in one-to-many [OK]
Common Mistakes:
Using WHERE with aggregate functions instead of HAVING
Missing GROUP BY clause
Using LEFT JOIN but filtering with WHERE on aggregate