Bird
Raised Fist0
SQLquery~10 mins

Many-to-many with junction tables in SQL - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select all columns from the junction table named 'student_courses'.

SQL
SELECT [1] FROM student_courses;
Drag options to blanks, or click blank then click option'
Acourse_id
Bstudent_id
Cstudents
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name instead of selecting all columns.
Trying to select from a table that doesn't exist.
2fill in blank
medium

Complete the code to join 'students' and 'student_courses' on the student ID.

SQL
SELECT students.name, student_courses.course_id FROM students JOIN student_courses ON students.[1] = student_courses.student_id;
Drag options to blanks, or click blank then click option'
Aid
Bstudent_id
Ccourse_id
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong column name that doesn't exist in 'students'.
Mixing up the foreign key and primary key columns.
3fill in blank
hard

Fix the error in the query to get course names for each student by joining all three tables.

SQL
SELECT students.name, courses.name FROM students JOIN student_courses ON students.id = student_courses.student_id JOIN courses ON courses.[1] = student_courses.course_id;
Drag options to blanks, or click blank then click option'
Astudent_id
Bcourse_id
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using the foreign key column name instead of the primary key in the join condition.
Joining on columns that don't relate.
4fill in blank
hard

Fill both blanks to create a junction table named 'author_books' with foreign keys to 'authors' and 'books'.

SQL
CREATE TABLE author_books (author_id INT, book_id INT, FOREIGN KEY (author_id) REFERENCES [1](id), FOREIGN KEY (book_id) REFERENCES [2](id));
Drag options to blanks, or click blank then click option'
Aauthors
Bauthor_books
Cbooks
Dbook_authors
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing the junction table itself instead of the related tables.
Mixing up table names.
5fill in blank
hard

Fill all three blanks to select student names and their course names using joins on the junction table.

SQL
SELECT [1].name, [2].name FROM [3] JOIN student_courses ON students.id = student_courses.student_id JOIN courses ON courses.id = student_courses.course_id;
Drag options to blanks, or click blank then click option'
Astudents
Bcourses
Denrollments
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong table alias or name in the SELECT clause.
Confusing the junction table with main tables.

Practice

(1/5)
1. What is the main purpose of a junction table in a many-to-many relationship?
easy
A. To store pairs of related records from two tables using foreign keys
B. To store all data from both tables in one place
C. To replace one of the original tables completely
D. To create a one-to-one relationship between tables

Solution

  1. Step 1: Understand many-to-many relationships

    Many-to-many means each record in one table can relate to many records in another table, and vice versa.
  2. Step 2: Role of junction table

    A junction table holds pairs of foreign keys from both tables to link related records without duplicating data.
  3. Final Answer:

    To store pairs of related records from two tables using foreign keys -> Option A
  4. Quick Check:

    Junction table = pairs of foreign keys [OK]
Hint: Junction tables link two tables with pairs of keys [OK]
Common Mistakes:
  • Thinking junction table stores all data from both tables
  • Confusing junction table with a single main table
  • Assuming junction table creates one-to-one links
2. Which SQL statement correctly creates a junction table named StudentCourse linking Student and Course tables by their IDs?
easy
A. CREATE TABLE StudentCourse (StudentID INT, CourseID INT, FOREIGN KEY (StudentID) REFERENCES Student(ID));
B. CREATE TABLE StudentCourse (ID INT PRIMARY KEY, StudentID INT, CourseID INT);
C. CREATE TABLE StudentCourse (StudentID INT UNIQUE, CourseID INT UNIQUE);
D. CREATE TABLE StudentCourse (StudentID INT, CourseID INT, PRIMARY KEY (StudentID, CourseID));

Solution

  1. Step 1: Define junction table columns

    It needs two columns for foreign keys: StudentID and CourseID.
  2. Step 2: Set primary key on both columns

    Primary key on (StudentID, CourseID) ensures unique pairs and no duplicates.
  3. Final Answer:

    CREATE TABLE StudentCourse (StudentID INT, CourseID INT, PRIMARY KEY (StudentID, CourseID)); -> Option D
  4. Quick Check:

    Junction table needs composite primary key [OK]
Hint: Use composite primary key on both foreign keys [OK]
Common Mistakes:
  • Using UNIQUE on individual columns instead of composite key
  • Missing one foreign key column
  • Not defining primary key on the pair
3. Given tables Author, Book, and junction table AuthorBook with columns AuthorID and BookID, what does this query return?
SELECT Author.Name, Book.Title FROM Author
JOIN AuthorBook ON Author.ID = AuthorBook.AuthorID
JOIN Book ON Book.ID = AuthorBook.BookID;
medium
A. A list of authors and the titles of books they wrote
B. A list of books without any author names
C. A list of all authors with all books, including unrelated pairs
D. An error because of missing WHERE clause

Solution

  1. Step 1: Understand JOINs with junction table

    The query joins Author to AuthorBook by AuthorID, then AuthorBook to Book by BookID, linking authors to their books.
  2. Step 2: Result of the query

    It returns pairs of author names and book titles where the author wrote the book, no unrelated pairs included.
  3. Final Answer:

    A list of authors and the titles of books they wrote -> Option A
  4. Quick Check:

    JOINs with junction table = related pairs only [OK]
Hint: JOIN junction table to get related pairs only [OK]
Common Mistakes:
  • Thinking it returns all combinations of authors and books
  • Expecting an error without WHERE clause
  • Ignoring the role of junction table in filtering
4. You wrote this query to find all students and their courses:
SELECT Student.Name, Course.Title FROM Student
JOIN StudentCourse ON Student.ID = StudentCourse.StudentID
JOIN Course ON Course.ID = StudentCourse.CourseID
WHERE StudentCourse.StudentID = Student.ID;

But it has a problem. What is the problem?
medium
A. The WHERE clause is redundant and causes a syntax error
B. Missing alias for tables causes ambiguity
C. The WHERE clause is unnecessary because JOIN already matches IDs
D. StudentCourse.CourseID is missing in the WHERE clause

Solution

  1. Step 1: Analyze the JOIN conditions

    The JOINs already match Student.ID to StudentCourse.StudentID and Course.ID to StudentCourse.CourseID.
  2. Step 2: Check the WHERE clause

    The WHERE clause repeats the JOIN condition, which is unnecessary but not an error; however, it does not filter or add value.
  3. Final Answer:

    The WHERE clause is unnecessary because JOIN already matches IDs -> Option C
  4. Quick Check:

    JOIN matches IDs, WHERE clause redundant [OK]
Hint: JOIN conditions handle matching; WHERE often not needed here [OK]
Common Mistakes:
  • Assuming WHERE clause causes syntax error
  • Adding unnecessary conditions that duplicate JOINs
  • Confusing alias usage with errors
5. You have tables Employee, Project, and junction table EmployeeProject with EmployeeID and ProjectID. How do you find employees who work on all projects listed in Project?
hard
A. Join EmployeeProject and Project, then filter with WHERE ProjectID IS NOT NULL
B. Use GROUP BY EmployeeID and HAVING count of projects equal to total projects count
C. Select employees with a simple JOIN to EmployeeProject without grouping
D. Use DISTINCT on EmployeeID in EmployeeProject without counting projects

Solution

  1. Step 1: Count total projects

    Find total number of projects from Project table.
  2. Step 2: Group EmployeeProject by EmployeeID

    Count how many projects each employee works on.
  3. Step 3: Use HAVING to compare counts

    Only select employees whose project count equals total projects count.
  4. Final Answer:

    Use GROUP BY EmployeeID and HAVING count of projects equal to total projects count -> Option B
  5. Quick Check:

    Group and count projects per employee = all projects [OK]
Hint: Group by employee, HAVING count = total projects [OK]
Common Mistakes:
  • Not grouping and counting projects per employee
  • Using WHERE instead of HAVING for aggregate filtering
  • Ignoring total projects count in comparison