0
0
PostgreSQLquery~20 mins

Subqueries with EXISTS in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subquery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of EXISTS with a simple subquery
Given the tables employees and departments, what is the output of the following query?
SELECT e.name FROM employees e WHERE EXISTS (SELECT 1 FROM departments d WHERE d.id = e.department_id AND d.location = 'New York');
PostgreSQL
CREATE TABLE employees (id INT, name TEXT, department_id INT);
CREATE TABLE departments (id INT, location TEXT);
INSERT INTO employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', 10);
INSERT INTO departments VALUES (10, 'New York'), (20, 'Chicago');
AAlice, Charlie
BBob
CAlice, Bob, Charlie
DNo rows
Attempts:
2 left
💡 Hint
EXISTS returns true if the subquery finds any matching rows.
query_result
intermediate
2:00remaining
Filtering with NOT EXISTS
What does this query return?
SELECT d.id FROM departments d WHERE NOT EXISTS (SELECT 1 FROM employees e WHERE e.department_id = d.id);
PostgreSQL
CREATE TABLE employees (id INT, name TEXT, department_id INT);
CREATE TABLE departments (id INT);
INSERT INTO employees VALUES (1, 'Alice', 10), (2, 'Bob', 20);
INSERT INTO departments VALUES (10), (20), (30);
A10, 20
BNo rows
C10, 20, 30
D30
Attempts:
2 left
💡 Hint
NOT EXISTS returns true when the subquery finds no matching rows.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in EXISTS subquery
Which option contains a syntax error in the EXISTS subquery?
SELECT name FROM employees WHERE EXISTS (SELECT * FROM departments WHERE id = department_id);
PostgreSQL
CREATE TABLE employees (id INT, name TEXT, department_id INT);
CREATE TABLE departments (id INT);
INSERT INTO employees VALUES (1, 'Alice', 10), (2, 'Bob', 20);
INSERT INTO departments VALUES (10), (20), (30);
ASELECT name FROM employees WHERE EXISTS (SELECT 1 FROM departments WHERE departments.id = employees.department_id);
BSELECT name FROM employees WHERE EXISTS (SELECT 1 FROM departments WHERE id = employees.department_id);
CSELECT name FROM employees WHERE EXISTS (SELECT 1 FROM departments WHERE id = employees.department_id AND );
DSELECT name FROM employees WHERE EXISTS (SELECT * FROM departments WHERE id = department_id);
Attempts:
2 left
💡 Hint
Look for incomplete conditions or misplaced syntax in the subquery.
optimization
advanced
2:00remaining
Optimizing EXISTS subquery for performance
Which query is generally more efficient for checking existence of related rows?
Option 1:
SELECT name FROM employees WHERE EXISTS (SELECT 1 FROM departments WHERE id = employees.department_id);

Option 2:
SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments);
ABoth have the same performance in all cases.
BOption 1 is more efficient because EXISTS stops at first match.
COption 2 is more efficient because IN uses indexes better.
DOption 2 is more efficient because it returns all matches at once.
Attempts:
2 left
💡 Hint
EXISTS stops searching after finding the first matching row.
🧠 Conceptual
expert
2:00remaining
Understanding EXISTS with correlated subqueries
Consider the query:
SELECT e.name FROM employees e WHERE EXISTS (SELECT 1 FROM projects p WHERE p.lead_id = e.id AND p.status = 'active');

What does this query return?
ANames of employees who lead at least one active project.
BNames of employees who have any project regardless of status.
CNames of employees who do not lead any active project.
DNames of all employees.
Attempts:
2 left
💡 Hint
EXISTS with a correlated subquery checks for matching rows per outer row.