A scalar subquery is a subquery that returns exactly one value (one row and one column). It can be used wherever a single value is expected, like in SELECT, WHERE, or SET clauses.
Click to reveal answer
beginner
How does a scalar subquery differ from other subqueries?
A scalar subquery returns a single value, while other subqueries can return multiple rows or columns. Scalar subqueries are used as single values in expressions.
Click to reveal answer
beginner
Example: What does this scalar subquery return?
SELECT (SELECT COUNT(*) FROM employees) AS total_employees;
It returns the total number of employees as a single value in the column 'total_employees'.
Click to reveal answer
intermediate
Can a scalar subquery return more than one row or column?
No. If a scalar subquery returns more than one row or column, it causes an error because it expects exactly one value.
Click to reveal answer
intermediate
Where can scalar subqueries be used in SQL statements?
Scalar subqueries can be used in SELECT lists, WHERE clauses, HAVING clauses, and SET clauses where a single value is expected.
Click to reveal answer
What does a scalar subquery return?
AExactly one value (one row, one column)
BMultiple rows and columns
COnly multiple rows
DOnly multiple columns
✗ Incorrect
A scalar subquery returns exactly one value, which means one row and one column.
Where can you use a scalar subquery in SQL?
AOnly in JOIN conditions
BIn places expecting a single value, like SELECT or WHERE clauses
COnly in CREATE statements
DOnly in GROUP BY clauses
✗ Incorrect
Scalar subqueries are used where a single value is expected, such as in SELECT or WHERE clauses.
What happens if a scalar subquery returns more than one row?
AIt returns the first row only
BIt returns all rows concatenated
CSQL returns an error
DIt ignores extra rows silently
✗ Incorrect
If a scalar subquery returns more than one row, SQL throws an error because it expects exactly one value.
Which of these is a valid scalar subquery?
ASELECT (SELECT MAX(salary) FROM employees) AS max_salary;
BSELECT * FROM (SELECT * FROM employees);
CSELECT (SELECT name, age FROM employees);
DSELECT (SELECT salary FROM employees);
✗ Incorrect
Option A returns a single value (maximum salary), which is valid for a scalar subquery.
Why use scalar subqueries?
ATo delete data
BTo return multiple rows for joins
CTo create new tables
DTo get a single value from another query to use in expressions
✗ Incorrect
Scalar subqueries help get a single value from another query to use in expressions or conditions.
Explain what a scalar subquery is and give an example of where you might use it.
Think about a query inside parentheses that returns one number or text.
You got /3 concepts.
What error occurs if a scalar subquery returns more than one row? How can you avoid this?
Consider how to limit results to one row.
You got /3 concepts.
Practice
(1/5)
1. What does a scalar subquery return in MySQL?
easy
A. A list of tables
B. A single value that can be used in another query
C. Only column names
D. Multiple rows and columns
Solution
Step 1: Understand scalar subquery definition
A scalar subquery returns exactly one value, not multiple rows or columns.
Step 2: Identify usage in queries
This single value can be used inside another query, like in WHERE or SELECT clauses.
Final Answer:
A single value that can be used in another query -> Option B
Quick Check:
Scalar subquery = single value [OK]
Hint: Scalar subqueries always return one value only [OK]
Common Mistakes:
Thinking scalar subqueries return multiple rows
Confusing scalar subqueries with table subqueries
Assuming scalar subqueries return column names only
2. Which of the following is the correct syntax for a scalar subquery in a SELECT statement?
easy
A. SELECT name, MAX(score) FROM scores FROM students;
B. SELECT name, MAX(score) AS max_score WHERE score > 50;
C. SELECT name, (SELECT MAX(score) FROM scores) AS max_score FROM students;
D. SELECT name, (MAX(score) FROM scores) AS max_score FROM students;
Solution
Step 1: Analyze correct scalar subquery syntax
The scalar subquery must be enclosed in parentheses and return a single value, as in (SELECT MAX(score) FROM scores).
Step 2: Check placement in SELECT clause
SELECT name, (SELECT MAX(score) FROM scores) AS max_score FROM students; correctly places the scalar subquery inside the SELECT list with an alias.
Final Answer:
SELECT name, (SELECT MAX(score) FROM scores) AS max_score FROM students; -> Option C
Quick Check:
Scalar subquery syntax = SELECT name, (SELECT MAX(score) FROM scores) AS max_score FROM students; [OK]
Hint: Scalar subqueries must be in parentheses inside SELECT or WHERE [OK]
Common Mistakes:
Missing parentheses around subquery
Incorrect FROM clause placement
Using aggregate functions without subquery
3. Given the tables: students(id, name) scores(student_id, score) What will this query return?
SELECT name, (SELECT AVG(score) FROM scores WHERE student_id = students.id) AS avg_score FROM students;
medium
A. Each student's name with their average score
B. The overall average score for all students repeated for each name
C. Syntax error due to subquery
D. List of student names only without scores
Solution
Step 1: Understand the subquery correlation
The subquery calculates the average score for each student by matching student_id with students.id.
Step 2: Result of the main query
The main query returns each student's name along with their individual average score from the subquery.
Final Answer:
Each student's name with their average score -> Option A
Quick Check:
Scalar subquery returns per-student average [OK]
Hint: Correlated scalar subqueries return values per row [OK]
Common Mistakes:
Assuming subquery returns overall average for all rows
Confusing correlated and uncorrelated subqueries
Expecting syntax error when none exists
4. Identify the error in this query using a scalar subquery:
SELECT name, (SELECT score FROM scores) AS score FROM students;
medium
A. No error, query runs fine
B. Missing alias for subquery
C. Incorrect table name in subquery
D. Subquery returns multiple rows causing an error
Solution
Step 1: Analyze the subquery result
The subquery SELECT score FROM scores returns multiple rows because no WHERE clause limits it.
Step 2: Understand scalar subquery requirement
Scalar subqueries must return exactly one value; multiple rows cause an error.
Final Answer:
Subquery returns multiple rows causing an error -> Option D
Quick Check:
Scalar subquery must return one value [OK]
Hint: Scalar subqueries must return one value, not many [OK]
Common Mistakes:
Ignoring multiple rows returned by subquery
Assuming alias is mandatory for subquery
Thinking query runs without error
5. You want to find students whose score is above the average score of all students. Which query correctly uses a scalar subquery to achieve this?
hard
A. SELECT name FROM students WHERE score > (SELECT AVG(score) FROM scores);
B. SELECT name FROM students WHERE score > SELECT AVG(score) FROM scores;
C. SELECT name FROM students WHERE (SELECT AVG(score) FROM scores) < (SELECT score FROM scores WHERE student_id = students.id);
D. SELECT name FROM students WHERE score > (SELECT score FROM scores);
Solution
Step 1: Understand the goal
We want students whose individual score is greater than the average score of all students.
Step 2: Analyze each option
SELECT name FROM students WHERE score > (SELECT AVG(score) FROM scores); correctly compares each student's score with the average score using a scalar subquery.
Final Answer:
SELECT name FROM students WHERE score > (SELECT AVG(score) FROM scores); -> Option A
Quick Check:
Scalar subquery compares average with individual score [OK]
Hint: Use scalar subquery for average, correlate for individual score [OK]
Common Mistakes:
Missing parentheses around subqueries
Using subquery without correlation for individual scores
Syntax errors from missing SELECT in WHERE
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.