0
0
PostgreSQLquery~20 mins

Why joins are essential in PostgreSQL - Challenge Your Understanding

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
What is the output of this INNER JOIN query?

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;
PostgreSQL
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');
A[{"name": "Alice", "course": "Math"}, {"name": "Alice", "course": "Science"}, {"name": "Bob", "course": null}, {"name": "Charlie", "course": "History"}]
B[{"name": "Alice", "course": "Math"}, {"name": "Bob", "course": null}, {"name": "Charlie", "course": "History"}]
C[{"name": "Alice", "course": "Math"}, {"name": "Alice", "course": "Science"}, {"name": "Charlie", "course": "History"}]
D[{"name": "Alice", "course": null}, {"name": "Bob", "course": null}, {"name": "Charlie", "course": null}]
Attempts:
2 left
💡 Hint

INNER JOIN returns only matching rows from both tables.

🧠 Conceptual
intermediate
1:30remaining
Why do we use JOINs in databases?

Which of the following best explains why JOINs are essential in relational databases?

AJOINs combine rows from two or more tables based on related columns to retrieve meaningful combined data.
BJOINs create new tables permanently by merging existing tables.
CJOINs delete duplicate rows from a single table to clean data.
DJOINs are used to encrypt data between tables for security.
Attempts:
2 left
💡 Hint

Think about how data is stored in separate tables but often needs to be seen together.

📝 Syntax
advanced
2:00remaining
Which query correctly uses a LEFT JOIN to include all students?

Given the same students and enrollments tables, which query will return all students including those without enrollments?

ASELECT students.name, enrollments.course FROM students LEFT JOIN enrollments ON students.student_id = enrollments.student_id;
BSELECT students.name, enrollments.course FROM students INNER JOIN enrollments ON students.student_id = enrollments.student_id;
CSELECT students.name, enrollments.course FROM enrollments LEFT JOIN students ON students.student_id = enrollments.student_id;
DSELECT students.name, enrollments.course FROM students RIGHT JOIN enrollments ON students.student_id = enrollments.student_id;
Attempts:
2 left
💡 Hint

LEFT JOIN keeps all rows from the left table.

optimization
advanced
2:00remaining
How can JOINs improve query performance compared to multiple separate queries?

Why is using JOINs often more efficient than running separate queries and combining results in application code?

AJOINs force the database to scan all tables fully, making queries slower than separate queries.
BJOINs let the database engine optimize data retrieval and reduce the number of queries sent, lowering network overhead.
CJOINs require creating temporary tables which increases disk usage and slows performance.
DJOINs always use more memory and slow down queries compared to separate queries.
Attempts:
2 left
💡 Hint

Think about how databases handle data internally versus application code.

🔧 Debug
expert
2:00remaining
What error does this JOIN query produce?

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;
AERROR: syntax error at or near "JOIN"
BERROR: column "enrollments.name" does not exist
CNo error, query runs successfully
DERROR: column "students.id" does not exist
Attempts:
2 left
💡 Hint

Check the column names used in the ON clause carefully.