0
0
MySQLquery~20 mins

Scalar subqueries in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scalar Subquery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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?
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;
A[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "Marketing"}, {"name": "Carol", "dept_name": "Sales"}]
B[{"name": "Alice", "dept_name": null}, {"name": "Bob", "dept_name": null}, {"name": "Carol", "dept_name": null}]
C[{"name": "Alice", "dept_name": "Marketing"}, {"name": "Bob", "dept_name": "Sales"}, {"name": "Carol", "dept_name": "Marketing"}]
D[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "Sales"}, {"name": "Carol", "dept_name": "Sales"}]
Attempts:
2 left
💡 Hint
Think about how the subquery uses the outer query's department_id to find the matching department name.
🧠 Conceptual
intermediate
1:30remaining
Understanding scalar subquery behavior
Which of the following statements about scalar subqueries is true?
AA scalar subquery returns a table that can be joined directly in the FROM clause.
BA scalar subquery can return multiple columns but only one row without error.
CA scalar subquery can return multiple rows and columns and still be used in a SELECT clause.
DA scalar subquery must return exactly one row and one column; otherwise, it causes an error.
Attempts:
2 left
💡 Hint
Think about what happens if the subquery returns more than one value when used as a single value.
📝 Syntax
advanced
2: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');
ASELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');
BSELECT * FROM employees WHERE department_id = (SELECT id, name FROM departments WHERE name = 'Sales');
CSELECT * FROM employees WHERE department_id = (SELECT id FROM departments WHERE name = 'Sales');
DSELECT * FROM employees WHERE department_id = (SELECT id FROM departments WHERE name = 'Sales' LIMIT 1);
Attempts:
2 left
💡 Hint
Remember scalar subqueries must return only one column.
optimization
advanced
2:30remaining
Optimizing scalar subqueries in SELECT
Given this query:
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?
ARewrite using a JOIN with a GROUP BY to precompute average salaries per department.
BAdd DISTINCT inside the scalar subquery to reduce duplicates.
CReplace the scalar subquery with a correlated subquery that uses LIMIT 1.
DUse a scalar subquery that selects salary instead of AVG(salary).
Attempts:
2 left
💡 Hint
Think about how many times the subquery runs and how to reduce repeated calculations.
🔧 Debug
expert
2: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?
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?
ABecause the subquery syntax is incorrect and always returns NULL.
BBecause the scalar subquery returns multiple rows for Carol causing NULL to be assigned.
CBecause there is no department with id 30 in the departments table, so the subquery returns no rows, resulting in NULL.
DBecause the employees table has a NULL department_id for Carol.
Attempts:
2 left
💡 Hint
Check if the department_id for Carol exists in the departments table.