Given these tables:
Students: (id, name)
Enrollments: (student_id, course_id)
Courses: (id, title)
What is the output of this query?
SELECT Students.name, Courses.title FROM Students INNER JOIN Enrollments ON Students.id = Enrollments.student_id INNER JOIN Courses ON Enrollments.course_id = Courses.id WHERE Courses.title = 'Math';
CREATE TABLE Students (id INT, name VARCHAR(20)); CREATE TABLE Enrollments (student_id INT, course_id INT); CREATE TABLE Courses (id INT, title VARCHAR(20)); INSERT INTO Students VALUES (1, 'Alice'), (2, 'Bob'); INSERT INTO Courses VALUES (10, 'Math'), (20, 'History'); INSERT INTO Enrollments VALUES (1, 10), (2, 20);
Think about which student is enrolled in the 'Math' course.
The query joins Students to Enrollments, then to Courses, filtering for 'Math'. Only Alice is enrolled in Math.
Given these tables:
Authors: (id, name)
Books: (id, author_id, title)
What is the output of this query?
SELECT Authors.name, Books.title FROM Authors LEFT JOIN Books ON Authors.id = Books.author_id WHERE Books.title IS NULL;
CREATE TABLE Authors (id INT, name VARCHAR(20)); CREATE TABLE Books (id INT, author_id INT, title VARCHAR(20)); INSERT INTO Authors VALUES (1, 'John'), (2, 'Mary'); INSERT INTO Books VALUES (10, 1, 'Book A');
LEFT JOIN keeps all authors, but WHERE filters for those without books.
Mary has no books, so Books.title is NULL for her. The query returns Mary only.
Which option contains a syntax error in this multiple JOIN query?
SELECT e.name, d.department_name, m.name AS manager_name FROM employees e JOIN departments d ON e.department_id = d.id JOIN employees m ON d.manager_id = m.id;
Look carefully at the JOIN clauses and ON keywords.
Option D is missing the ON keyword after 'departments d', causing a syntax error.
You have three large tables: Customers, Orders, and Products. You want to find all customers who ordered a product named 'Gadget'. Which query is more efficient?
Consider how filtering early can reduce rows processed in joins.
Option C filters Products first, reducing rows before joining, improving performance.
Consider these tables:
Employees: (id, name, manager_id)
Departments: (id, name, head_id)
You want to list employees with their department name and their manager's name. Which JOIN order is correct to get all employees, even those without a department or manager?
Think about which table is the main one and how LEFT JOIN preserves rows.
Option A starts from Employees and LEFT JOINs managers and departments, preserving all employees even if no manager or department exists.