Challenge - 5 Problems
Scalar Subquery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of scalar subquery in SELECT clause
Consider the following tables:
employees
id | name | department_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 10
departments
id | name
10 | Sales
20 | Marketing
What is the output of this query?
employees
id | name | department_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 10
departments
id | name
10 | Sales
20 | Marketing
What is the output of this query?
SELECT name, (SELECT name FROM departments WHERE id = department_id) AS dept_name FROM employees ORDER BY id;
MySQL
SELECT name, (SELECT name FROM departments WHERE id = department_id) AS dept_name FROM employees ORDER BY id;
Attempts:
2 left
💡 Hint
Think about how the subquery uses the outer query's department_id to find the matching department name.
✗ Incorrect
The scalar subquery selects the department name where the department id matches the employee's department_id. So Alice and Carol belong to Sales (id 10), Bob belongs to Marketing (id 20).
🧠 Conceptual
intermediate1:30remaining
Understanding scalar subquery behavior
Which of the following statements about scalar subqueries is true?
Attempts:
2 left
💡 Hint
Think about what happens if the subquery returns more than one value when used as a single value.
✗ Incorrect
Scalar subqueries must return exactly one value (one row, one column). Returning more causes an error; returning zero rows returns NULL.
📝 Syntax
advanced2:00remaining
Identify the syntax error in scalar subquery usage
Which option contains a syntax error when using a scalar subquery in the WHERE clause?
MySQL
SELECT * FROM employees WHERE department_id = (SELECT id FROM departments WHERE name = 'Sales');
Attempts:
2 left
💡 Hint
Remember scalar subqueries must return only one column.
✗ Incorrect
Option B tries to return two columns (id, name) in a scalar subquery used with '=', which expects one value. This causes a syntax error.
❓ optimization
advanced2:30remaining
Optimizing scalar subqueries in SELECT
Given this query:
Which optimization improves performance without changing the output?
SELECT name, (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id) AS avg_dept_salary FROM employees e;
Which optimization improves performance without changing the output?
Attempts:
2 left
💡 Hint
Think about how many times the subquery runs and how to reduce repeated calculations.
✗ Incorrect
Using a JOIN with GROUP BY computes average salaries once per department, avoiding repeated scalar subquery execution for each employee.
🔧 Debug
expert2:30remaining
Debugging unexpected NULL from scalar subquery
Given these tables:
employees
id | name | department_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 30
departments
id | name
10 | Sales
20 | Marketing
What is the output of this query?
Why does Carol's dept_name show NULL?
employees
id | name | department_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 30
departments
id | name
10 | Sales
20 | Marketing
What is the output of this query?
SELECT name, (SELECT name FROM departments WHERE id = department_id) AS dept_name FROM employees ORDER BY id;
Why does Carol's dept_name show NULL?
Attempts:
2 left
💡 Hint
Check if the department_id for Carol exists in the departments table.
✗ Incorrect
Carol's department_id is 30, which does not exist in departments, so the scalar subquery returns zero rows, which SQL treats as NULL.