0
0
SQLquery~20 mins

Nested subqueries in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Subquery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find employees with salary above department average

Given a table employees with columns id, name, department_id, and salary, which query returns the names of employees whose salary is greater than the average salary in their department?

SQL
SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = employees.department_id);
ASELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = employees.department_id);
BSELECT name FROM employees WHERE salary > AVG(salary) FROM employees GROUP BY department_id;
CSELECT name FROM employees WHERE salary > (SELECT MAX(salary) FROM employees WHERE department_id = employees.department_id);
DSELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
Attempts:
2 left
💡 Hint

Use a subquery to calculate the average salary for each employee's department.

query_result
intermediate
2:00remaining
List products priced higher than any in category 5

Given a table products with columns product_id, category_id, and price, which query lists all products with a price higher than every product in category 5?

SQL
SELECT product_id FROM products WHERE price > ALL (SELECT price FROM products WHERE category_id = 5);
ASELECT product_id FROM products WHERE price > ALL (SELECT price FROM products WHERE category_id = 5);
BSELECT product_id FROM products WHERE price > ANY (SELECT price FROM products WHERE category_id = 5);
CSELECT product_id FROM products WHERE price > (SELECT MAX(price) FROM products WHERE category_id = 5);
DSELECT product_id FROM products WHERE price >= ALL (SELECT price FROM products WHERE category_id = 5);
Attempts:
2 left
💡 Hint

Use ALL to ensure the price is greater than every price in category 5.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in nested subquery

Which option contains a syntax error in the nested subquery?

SQL
SELECT name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'NY');
ASELECT name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'NY') ORDER BY name;
BSELECT name FROM employees WHERE department_id = (SELECT department_id FROM departments WHERE location = 'NY');
CSELECT name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'NY');
DSELECT name FROM employees WHERE department_id IN SELECT department_id FROM departments WHERE location = 'NY';
Attempts:
2 left
💡 Hint

Check if the subquery is properly enclosed in parentheses.

optimization
advanced
2:00remaining
Optimize query with nested subquery for better performance

Given the query below, which option optimizes it by reducing repeated subquery execution?

SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = employees.department_id);
ASELECT name FROM employees WHERE salary > ANY (SELECT AVG(salary) FROM employees GROUP BY department_id);
BSELECT name FROM employees WHERE salary > ALL (SELECT AVG(salary) FROM employees GROUP BY department_id);
CWITH dept_avg AS (SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id) SELECT e.name FROM employees e JOIN dept_avg d ON e.department_id = d.department_id WHERE e.salary > d.avg_salary;
DSELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
Attempts:
2 left
💡 Hint

Use a common table expression (CTE) or join to avoid recalculating averages for each row.

🧠 Conceptual
expert
2:00remaining
Understanding correlated vs non-correlated subqueries

Which statement correctly describes the difference between correlated and non-correlated subqueries?

ABoth correlated and non-correlated subqueries run once per query and do not depend on the outer query.
BA correlated subquery depends on the outer query for each row, while a non-correlated subquery runs once independently of the outer query.
CCorrelated subqueries can only be used in WHERE clauses, non-correlated only in FROM clauses.
DA non-correlated subquery depends on the outer query for each row, while a correlated subquery runs once independently of the outer query.
Attempts:
2 left
💡 Hint

Think about whether the subquery uses columns from the outer query.