0
0
PostgreSQLquery~20 mins

Why subqueries are needed in PostgreSQL - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Subqueries in SQL
Why do we use subqueries in SQL? Choose the best explanation.
ATo create multiple tables at once in a single SQL statement.
BTo perform a query inside another query to filter or calculate data before the main query runs.
CTo speed up the database server by running queries in parallel automatically.
DTo permanently store query results as new tables without writing extra commands.
Attempts:
2 left
💡 Hint
Think about how subqueries help break down complex questions into smaller parts.
query_result
intermediate
2:00remaining
Output of a Subquery Filtering
Given the tables employees (id, name, department_id) and departments (id, name), what is the output of this query?

SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');
PostgreSQL
employees:
1, 'Alice', 2
2, 'Bob', 3
3, 'Charlie', 2
4, 'Diana', 1
departments:
1, 'HR'
2, 'Sales'
3, 'Marketing'
AAlice, Bob, Charlie
BBob, Diana
CAlice, Charlie
DDiana
Attempts:
2 left
💡 Hint
Look at which department has the name 'Sales' and match employees with that department_id.
📝 Syntax
advanced
2:00remaining
Identify the Syntax Error in Subquery Usage
Which option contains a syntax error in using a subquery in PostgreSQL?
PostgreSQL
SELECT * FROM orders WHERE customer_id = (SELECT id FROM customers WHERE name = 'John');
ASELECT * FROM orders WHERE customer_id = (SELECT id FROM customers WHERE name = 'John' LIMIT 1);
BSELECT * FROM orders WHERE customer_id = (SELECT id FROM customers WHERE name = 'John');
CSELECT * FROM orders WHERE customer_id = ANY (SELECT id FROM customers WHERE name = 'John');
DSELECT * FROM orders WHERE customer_id IN SELECT id FROM customers WHERE name = 'John';
Attempts:
2 left
💡 Hint
Check the placement of parentheses and keywords around the subquery.
optimization
advanced
2:00remaining
Why Use Subqueries Instead of Joins in Some Cases?
Which reason best explains why a subquery might be preferred over a JOIN in a query?
ASubqueries can simplify queries when filtering based on aggregated results that are hard to express with JOINs.
BSubqueries always run faster than JOINs because they use less memory.
CSubqueries automatically create indexes to speed up queries, unlike JOINs.
DSubqueries allow combining data from unrelated tables without any conditions.
Attempts:
2 left
💡 Hint
Think about when you need to filter by a calculated value like a maximum or count.
🔧 Debug
expert
2:00remaining
Debugging a Subquery Returning Multiple Rows Error
What error will this query produce and why?

SELECT name FROM employees WHERE department_id = (SELECT id FROM departments WHERE location = 'New York');

Assume the subquery returns multiple department ids.
AERROR: subquery returns more than one row because '=' expects a single value.
BNo error, query runs successfully returning all matching employees.
CERROR: syntax error near '=' because subqueries cannot be used in WHERE clauses.
DERROR: missing FROM clause in subquery.
Attempts:
2 left
💡 Hint
Consider what happens when a subquery returns more than one value but '=' expects one.