Consider two tables: students and enrollments.
students has columns: student_id, name.
enrollments has columns: student_id, course.
What rows will this query return?
SELECT students.name, enrollments.course FROM students INNER JOIN enrollments ON students.student_id = enrollments.student_id ORDER BY students.name;
CREATE TABLE students (student_id INT, name TEXT); INSERT INTO students VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie'); CREATE TABLE enrollments (student_id INT, course TEXT); INSERT INTO enrollments VALUES (1, 'Math'), (1, 'Science'), (3, 'History');
INNER JOIN returns only matching rows from both tables.
The INNER JOIN returns rows where student_id matches in both tables. Bob has no enrollment, so he is excluded.
Which of the following best explains why JOINs are essential in relational databases?
Think about how data is stored in separate tables but often needs to be seen together.
JOINs let us combine related data from multiple tables to answer questions that involve more than one table.
Given the same students and enrollments tables, which query will return all students including those without enrollments?
LEFT JOIN keeps all rows from the left table.
LEFT JOIN returns all rows from students even if no matching enrollment exists. INNER JOIN excludes unmatched rows.
Why is using JOINs often more efficient than running separate queries and combining results in application code?
Think about how databases handle data internally versus application code.
JOINs allow the database to combine data efficiently in one query, reducing network trips and leveraging indexes.
Given the tables students(student_id, name) and enrollments(student_id, course), what error will this query raise?
SELECT students.name, enrollments.course FROM students JOIN enrollments ON students.id = enrollments.student_id;
Check the column names used in the ON clause carefully.
The students table has no column named id, so referencing students.id causes an error.