0
0
SQLquery~5 mins

Many-to-many with junction tables in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a many-to-many relationship in databases?
It is a relationship where multiple records in one table relate to multiple records in another table. For example, students can enroll in many courses, and courses can have many students.
Click to reveal answer
beginner
Why do we use a junction table in many-to-many relationships?
A junction table breaks down a many-to-many relationship into two one-to-many relationships. It stores pairs of related record IDs from the two tables, making the relationship manageable and queryable.
Click to reveal answer
intermediate
What columns does a junction table usually have?
It usually has at least two columns, each holding a foreign key that references the primary key of one of the related tables. Sometimes it also has its own primary key or additional data.
Click to reveal answer
intermediate
Write a simple SQL query to find all courses a student with ID 1 is enrolled in, using a junction table named Enrollment.
SELECT Courses.* FROM Courses JOIN Enrollment ON Courses.CourseID = Enrollment.CourseID WHERE Enrollment.StudentID = 1;
Click to reveal answer
intermediate
How does a junction table improve data integrity in many-to-many relationships?
By using foreign keys, it ensures that only valid records from the related tables can be linked. This prevents invalid or orphaned relationships and keeps data consistent.
Click to reveal answer
What does a junction table do in a many-to-many relationship?
ADeletes duplicate records automatically
BStores only one record per table
CStores pairs of related record IDs from two tables
DCreates a one-to-one relationship
Which of these is true about foreign keys in a junction table?
AThey reference primary keys in related tables
BThey are always primary keys themselves
CThey store duplicate data
DThey are not needed in junction tables
If a student can enroll in many courses and a course can have many students, what kind of relationship is this?
AOne-to-one
BNo relationship
COne-to-many
DMany-to-many
Which SQL clause is commonly used to connect tables through a junction table?
AJOIN
BORDER BY
CGROUP BY
DWHERE
What is a common name for a table that links two tables in a many-to-many relationship?
APrimary table
BJunction table
CLookup table
DIndex table
Explain how a many-to-many relationship is implemented using a junction table.
Think about how two tables connect through a third table storing pairs of IDs.
You got /4 concepts.
    Describe how you would write a SQL query to find all related records using a junction table.
    Consider joining the main tables through the junction table on matching IDs.
    You got /4 concepts.