Challenge - 5 Problems
Subquery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why use subqueries in SQL?
Which of the following best explains why subqueries are needed in SQL?
Attempts:
2 left
💡 Hint
Think about how you can use one query's result inside another query.
✗ Incorrect
Subqueries allow you to use the result of one query inside another. This helps when you need to filter or calculate based on intermediate data.
❓ query_result
intermediate2:00remaining
Output of a subquery filtering example
Given the tables Employees (id, name, salary) and Departments (id, name, manager_id), what is the output of this query?
SELECT name FROM Employees WHERE salary > (SELECT AVG(salary) FROM Employees);
Attempts:
2 left
💡 Hint
Look at what the subquery calculates and how it is used in the WHERE clause.
✗ Incorrect
The subquery calculates the average salary. The outer query selects employees with salary greater than that average.
📝 Syntax
advanced2:00remaining
Identify the correct syntax for a subquery in SELECT
Which option shows the correct syntax to use a subquery inside a SELECT statement to get the department name for each employee?
SQL
Tables: Employees(id, name, department_id), Departments(id, name)
Attempts:
2 left
💡 Hint
Check the placement of the subquery and alias syntax.
✗ Incorrect
Option D correctly uses a subquery in SELECT with proper aliasing and references the outer table.
❓ optimization
advanced2:00remaining
Why might subqueries cause performance issues?
Which reason best explains why subqueries can sometimes slow down SQL queries?
Attempts:
2 left
💡 Hint
Think about how many times the subquery might execute.
✗ Incorrect
Correlated subqueries run once per outer row, which can slow down queries if the outer table is large.
🔧 Debug
expert2:00remaining
Find the error in this subquery usage
What error will this query produce?
SELECT name FROM Employees WHERE salary > (SELECT salary FROM Employees WHERE id = 10);
Attempts:
2 left
💡 Hint
What happens if the subquery finds no matching row?
✗ Incorrect
If no employee with id=10 exists, the subquery returns no rows, causing the outer query to compare salary to NULL, resulting in no matches.