0
0
SQLquery~20 mins

Joining on primary key to foreign key in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Join Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of INNER JOIN on primary key to foreign key

Given two tables Authors and Books:

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

What is the output of this query?

SELECT Authors.name, Books.title
FROM Authors
INNER JOIN Books ON Authors.author_id = Books.author_id
ORDER BY Authors.author_id, Books.book_id;
SQL
CREATE TABLE Authors (author_id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE Books (book_id INT PRIMARY KEY, title VARCHAR(100), author_id INT, FOREIGN KEY (author_id) REFERENCES Authors(author_id));

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

INNER JOIN returns rows where the join condition matches in both tables.

🧠 Conceptual
intermediate
1:30remaining
Understanding LEFT JOIN with primary key to foreign key

What does a LEFT JOIN do when joining a primary key to a foreign key?

AReturns all rows from the left table and matching rows from the right table; if no match, right columns are NULL.
BReturns only rows from the left table that have no matching foreign key in the right table.
CReturns all rows from the right table and matching rows from the left table; if no match, left columns are NULL.
DReturns only rows where the primary key matches the foreign key in both tables.
Attempts:
2 left
💡 Hint

Think about which table's rows are always included in the result.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in JOIN query

Which option contains a syntax error in joining Orders and Customers on primary key to foreign key?

SELECT Orders.order_id, Customers.name
FROM Orders
JOIN Customers ON Orders.customer_id = Customers.customer_id;
ASELECT Orders.order_id, Customers.name FROM Orders LEFT JOIN Customers ON Orders.customer_id = Customers.customer_id;
BSELECT Orders.order_id, Customers.name FROM Orders JOIN Customers ON Orders.customer_id = Customers.customer_id;
CSELECT Orders.order_id, Customers.name FROM Orders INNER JOIN Customers ON Orders.customer_id = Customers.customer_id;
DSELECT Orders.order_id, Customers.name FROM Orders JOIN Customers WHERE Orders.customer_id = Customers.customer_id;
Attempts:
2 left
💡 Hint

Check the placement of the join condition.

optimization
advanced
2:00remaining
Optimizing JOIN performance on primary key to foreign key

Which option best improves performance when joining large tables on primary key to foreign key?

AAvoid using indexes to speed up sequential scans.
BUse SELECT * to retrieve all columns to avoid missing data.
CEnsure both primary key and foreign key columns are indexed.
DJoin tables without specifying ON clause to let the database decide.
Attempts:
2 left
💡 Hint

Indexes help the database find matching rows faster.

🔧 Debug
expert
2:30remaining
Why does this JOIN return fewer rows than expected?

Given tables Students and Enrollments with a primary key to foreign key relationship, this query returns fewer rows than expected:

SELECT Students.student_id, Enrollments.course_id
FROM Students
JOIN Enrollments ON Students.student_id = Enrollments.student_id
WHERE Enrollments.course_id IS NOT NULL;

What is the most likely reason?

AThe JOIN condition is incorrect; it should be ON Enrollments.student_id = Students.student_id.
BSome students have no enrollments, so INNER JOIN excludes them.
CThe WHERE clause filters out rows where course_id is NULL, but course_id is never NULL.
DThe Students table has duplicate student_id values causing row loss.
Attempts:
2 left
💡 Hint

Think about what INNER JOIN does when no matching rows exist.