0
0
SQLquery~10 mins

Many-to-many with junction tables in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Many-to-many with junction tables
Start
Two main tables
Create junction table
Insert foreign keys from both tables
Query using JOINs
Get combined results
End
This flow shows how two tables connect through a junction table to represent many-to-many relationships, then how queries combine data using JOINs.
Execution Sample
SQL
CREATE TABLE Students (StudentID INT, Name VARCHAR(50));
CREATE TABLE Courses (CourseID INT, Title VARCHAR(50));
CREATE TABLE Enrollments (StudentID INT, CourseID INT);
INSERT INTO Students VALUES (1, 'Alice'), (2, 'Bob');
INSERT INTO Courses VALUES (101, 'Math'), (102, 'History');
INSERT INTO Enrollments VALUES (1, 101), (1, 102), (2, 101);
This code creates two main tables and a junction table, then inserts data showing which students are enrolled in which courses.
Execution Table
StepActionTable AffectedData Inserted/QueriedResult/State
1Create Students tableStudentsStudentID, Name columnsEmpty Students table created
2Create Courses tableCoursesCourseID, Title columnsEmpty Courses table created
3Create Enrollments junction tableEnrollmentsStudentID, CourseID columnsEmpty Enrollments table created
4Insert studentsStudents(1, 'Alice'), (2, 'Bob')Students table has 2 rows
5Insert coursesCourses(101, 'Math'), (102, 'History')Courses table has 2 rows
6Insert enrollmentsEnrollments(1, 101), (1, 102), (2, 101)Enrollments table has 3 rows linking students and courses
7Query join Students and EnrollmentsQuerySELECT Students.Name, Courses.Title FROM Students JOIN Enrollments ON Students.StudentID = Enrollments.StudentID JOIN Courses ON Enrollments.CourseID = Courses.CourseIDResult: Alice-Math, Alice-History, Bob-Math
8End--All data linked and queried successfully
💡 All tables created, data inserted, and many-to-many query executed successfully
Variable Tracker
TableStartAfter CreationAfter InsertionsFinal State
StudentsNoneEmpty table with columns StudentID, Name2 rows: (1, 'Alice'), (2, 'Bob')2 rows with student data
CoursesNoneEmpty table with columns CourseID, Title2 rows: (101, 'Math'), (102, 'History')2 rows with course data
EnrollmentsNoneEmpty table with columns StudentID, CourseID3 rows: (1, 101), (1, 102), (2, 101)3 rows linking students and courses
Key Moments - 2 Insights
Why do we need a separate junction table instead of adding course IDs directly to the Students table?
Because each student can enroll in many courses and each course can have many students, a junction table allows storing multiple pairs without repeating or limiting data, as shown in execution_table step 6.
How does the JOIN in the query combine data from three tables?
The JOIN matches StudentID in Students with Enrollments, then CourseID in Enrollments with Courses, linking names and course titles together as seen in execution_table step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, how many enrollment records link students to courses?
A3
B2
C4
D1
💡 Hint
Check the 'Data Inserted/Queried' column in step 6 of execution_table.
At which step does the query return the combined student names and course titles?
AStep 5
BStep 3
CStep 7
DStep 4
💡 Hint
Look for the step mentioning SELECT and JOIN operations in execution_table.
If we added a new student but no enrollments, what would happen in the final query result?
AThe new student appears with no courses
BThe new student does not appear in the join result
CThe query fails with an error
DThe new student appears with all courses
💡 Hint
Recall how INNER JOIN works in SQL as shown in the query step 7.
Concept Snapshot
Many-to-many relationships use a junction table with foreign keys from both tables.
Create main tables, then a junction table.
Insert pairs of keys in junction table.
Use JOINs to combine data across tables.
This avoids data duplication and keeps relationships clear.
Full Transcript
This lesson shows how to represent many-to-many relationships in databases using junction tables. We start by creating two main tables, Students and Courses, each with their own unique IDs. Then, we create a junction table called Enrollments that holds pairs of StudentID and CourseID. We insert data into all tables, linking students to courses. Finally, we run a query joining all three tables to get a list of students and their enrolled courses. This method keeps data organized and avoids repeating information. The execution table traces each step from table creation to querying results, helping beginners see how data flows and connects.