0
0
SQLquery~20 mins

Many-to-many with junction tables in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Many-to-many Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find all students enrolled in 'Math 101'

Given these tables:

Students(id, name)
Courses(id, title)
Enrollments(student_id, course_id)

What is the output of this query?

SELECT s.name FROM Students s
JOIN Enrollments e ON s.id = e.student_id
JOIN Courses c ON e.course_id = c.id
WHERE c.title = 'Math 101'
ORDER BY s.name;
SQL
CREATE TABLE Students (id INT, name VARCHAR(50));
CREATE TABLE Courses (id INT, title VARCHAR(50));
CREATE TABLE Enrollments (student_id INT, course_id INT);

INSERT INTO Students VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');
INSERT INTO Courses VALUES (10, 'Math 101'), (20, 'History 201');
INSERT INTO Enrollments VALUES (1, 10), (2, 10), (3, 20);
A
Alice
Bob
B
Alice
Charlie
C
Bob
Charlie
D
Alice
Bob
Charlie
Attempts:
2 left
💡 Hint

Look at which students are linked to the course titled 'Math 101' through the Enrollments table.

🧠 Conceptual
intermediate
1:30remaining
Why use a junction table in many-to-many relationships?

In a database, why do we use a junction table to represent many-to-many relationships?

ATo enforce one-to-one relationships between tables
BTo avoid using primary keys in the related tables
CTo combine two tables into one for faster queries
DTo store additional data about the relationship and link two tables without duplicating data
Attempts:
2 left
💡 Hint

Think about how many-to-many relationships connect two sets of data without repeating information.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this many-to-many query

What error does this SQL query raise?

SELECT s.name, c.title
FROM Students s
JOIN Enrollments e ON s.id = e.student_id
JOIN Courses c ON e.course_id = c.id
WHERE c.title = 'History 201'
GROUP BY s.name;
ASyntax error: missing semicolon at end
BNo error, query runs correctly
CSyntax error: 'c.title' must be in GROUP BY or aggregated
DRuntime error: table 'Enrollments' does not exist
Attempts:
2 left
💡 Hint

Check if all selected columns are properly grouped or aggregated.

optimization
advanced
1:30remaining
Optimize this many-to-many query for performance

Which change will improve performance of this query?

SELECT s.name, COUNT(*) AS course_count
FROM Students s
JOIN Enrollments e ON s.id = e.student_id
GROUP BY s.name
ORDER BY course_count DESC;
AAdd an index on Enrollments.student_id
BAdd an index on Students.name
CRemove GROUP BY clause
DAdd an index on Enrollments.course_id
Attempts:
2 left
💡 Hint

Think about which column is used to join tables and filter data.

🔧 Debug
expert
2:30remaining
Why does this many-to-many query return duplicate rows?

Given these tables and data:

Students(id, name): (1, 'Anna'), (2, 'Ben')
Courses(id, title): (100, 'Physics'), (200, 'Chemistry')
Enrollments(student_id, course_id): (1, 100), (1, 200), (2, 100), (2, 100)

What causes this query to return duplicate rows?

SELECT s.name, c.title
FROM Students s
JOIN Enrollments e ON s.id = e.student_id
JOIN Courses c ON e.course_id = c.id
ORDER BY s.name, c.title;
AJOIN condition is incorrect and duplicates rows
BDuplicate rows in Enrollments cause repeated results
CMissing DISTINCT keyword in SELECT
DCourses table has duplicate titles causing duplicates
Attempts:
2 left
💡 Hint

Check the data in the junction table for repeated entries.