One-to-many relationship design in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with one-to-many relationships in databases, it's important to understand how query time grows as data increases.
We want to know how the time to get related data changes when there are more records.
Analyze the time complexity of the following SQL query.
SELECT customers.name, orders.order_id
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
WHERE customers.customer_id = 123;
This query finds all orders for one customer by joining the customers and orders tables on their IDs.
Look at what repeats when the query runs.
- Primary operation: Scanning the orders table to find matching orders for the customer.
- How many times: Once for each order that belongs to the customer.
As the number of orders for a customer grows, the query takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 orders | About 10 checks to find orders |
| 100 orders | About 100 checks |
| 1000 orders | About 1000 checks |
Pattern observation: The time grows directly with the number of orders for that customer.
Time Complexity: O(n)
This means the time to get all orders grows in a straight line with how many orders the customer has.
[X] Wrong: "The query time depends on the total number of customers in the database."
[OK] Correct: The query filters by one customer, so only that customer's orders affect time, not all customers.
Understanding how queries scale with data size helps you design efficient databases and write fast queries, a key skill in real projects.
"What if we added an index on orders.customer_id? How would that change the time complexity?"
Practice
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 AQuick Check:
One-to-many = one record to many records [OK]
- 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
Orders to Customers?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.Step 2: Add foreign key in 'many' table referencing 'one' table
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 CQuick Check:
Foreign key in 'many' table references 'one' table [OK]
- Placing foreign key in the 'one' table instead of 'many'
- Referencing wrong columns between tables
- Mixing table names in foreign key definition
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;
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 DQuick Check:
LEFT JOIN + COUNT = all customers with order counts [OK]
- Thinking COUNT counts total amount spent
- Assuming only customers with orders appear
- Confusing JOIN types and their effects
CREATE TABLE Orders (OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID));
But you get an error. What is the most likely cause?
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 BQuick Check:
Foreign key references must exist and be keys [OK]
- Assuming foreign key can reference non-key columns
- Placing foreign key in wrong table
- Mismatching data types between foreign key and referenced key
Authors(AuthorID, Name)Books(BookID, Title, AuthorID)You want to find authors who have written more than 3 books. Which query is correct?
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 AQuick Check:
JOIN + GROUP BY + HAVING filters authors by book count [OK]
- Using WHERE with aggregate functions instead of HAVING
- Missing GROUP BY clause
- Using LEFT JOIN but filtering with WHERE on aggregate
