0
0
SQLquery~20 mins

One-to-many relationship design in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
One-to-many Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a JOIN on one-to-many tables

Consider two tables: Authors and Books. Each author can write many books. The tables are:

Authors(id, name)
Books(id, author_id, title)

What is the output of this query?

SELECT Authors.name, Books.title
FROM Authors
JOIN Books ON Authors.id = Books.author_id
WHERE Authors.id = 1
ORDER BY Books.id;
SQL
CREATE TABLE Authors (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE Books (id INT PRIMARY KEY, author_id INT, title VARCHAR(100));

INSERT INTO Authors VALUES (1, 'Alice'), (2, 'Bob');
INSERT INTO Books VALUES (1, 1, 'Book A1'), (2, 1, 'Book A2'), (3, 2, 'Book B1');
A[{"name": "Alice", "title": "Book A1"}, {"name": "Alice", "title": "Book A2"}]
B[{"name": "Alice", "title": "Book A1"}, {"name": "Bob", "title": "Book A2"}]
C[{"name": "Alice", "title": "Book A1"}]
D[{"name": "Bob", "title": "Book B1"}]
Attempts:
2 left
💡 Hint

Remember that JOIN matches rows where the foreign key matches the primary key.

🧠 Conceptual
intermediate
1:30remaining
Identifying the foreign key in one-to-many design

In a one-to-many relationship between Customers and Orders, which table should contain the foreign key?

AOrders table should have a foreign key referencing Customers.
BBoth tables should have foreign keys referencing each other.
CCustomers table should have a foreign key referencing Orders.
DNeither table needs a foreign key in one-to-many relationships.
Attempts:
2 left
💡 Hint

Think about which side 'belongs to' the other.

📝 Syntax
advanced
2:00remaining
Correct foreign key constraint syntax

Which of the following SQL statements correctly creates a foreign key constraint for a one-to-many relationship where Orders.customer_id references Customers.id?

AALTER TABLE Orders ADD FOREIGN KEY (customer_id) TO Customers(id);
BALTER TABLE Orders ADD FOREIGN KEY (customer_id) REFERENCES Customers(id);
CALTER TABLE Orders ADD FOREIGN KEY customer_id REFERENCES Customers(id);
DALTER TABLE Customers ADD FOREIGN KEY (id) REFERENCES Orders(customer_id);
Attempts:
2 left
💡 Hint

Check the syntax for adding foreign keys in SQL.

optimization
advanced
2:30remaining
Improving query performance on one-to-many joins

You have large tables Authors and Books with a one-to-many relationship. Which indexing strategy improves the performance of this query?

SELECT Authors.name, Books.title
FROM Authors
JOIN Books ON Authors.id = Books.author_id
WHERE Authors.name LIKE 'A%';
ACreate an index on Books.id and Authors.name.
BCreate an index only on Authors.id.
CCreate an index on Books.author_id and Authors.name.
DCreate an index only on Books.title.
Attempts:
2 left
💡 Hint

Indexes on columns used in JOIN and WHERE clauses help performance.

🔧 Debug
expert
3:00remaining
Diagnosing missing rows in one-to-many join

Given these tables:

Customers(id, name)
Orders(id, customer_id, amount)

You run this query:

SELECT Customers.name, Orders.amount
FROM Customers
LEFT JOIN Orders ON Customers.id = Orders.customer_id
WHERE Orders.amount > 100;

Why might some customers with no orders appear missing from the result?

ACustomers with no orders have Orders.amount = 0, so they are filtered out.
BLEFT JOIN does not include customers without orders by default.
CThe JOIN condition is incorrect; it should be ON Orders.customer_id = Customers.id.
DThe WHERE clause filters out rows where Orders.amount is NULL, removing customers without orders.
Attempts:
2 left
💡 Hint

Think about how WHERE affects LEFT JOIN results.