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;
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);
INNER JOIN returns rows where the join condition matches in both tables.
The INNER JOIN matches each author with their books by matching author_id. Alice has two books, Bob has one.
What does a LEFT JOIN do when joining a primary key to a foreign key?
Think about which table's rows are always included in the result.
LEFT JOIN keeps all rows from the left table, adding NULLs for right table columns when no match exists.
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;
Check the placement of the join condition.
JOIN requires the ON clause to specify the join condition; using WHERE instead causes syntax error.
Which option best improves performance when joining large tables on primary key to foreign key?
Indexes help the database find matching rows faster.
Indexes on join columns allow the database to quickly locate matching rows, improving join speed.
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?
Think about what INNER JOIN does when no matching rows exist.
INNER JOIN only returns rows with matching keys in both tables. Students without enrollments are excluded.