Recall & Review
beginner
What is a scalar subquery in SQL?
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
Can a scalar subquery return multiple rows or columns?
No. A scalar subquery must return exactly one row and one column. Returning more than one row or column causes an error.
Click to reveal answer
intermediate
How do scalar subqueries differ from regular subqueries?
Scalar subqueries return a single value, while regular subqueries can return multiple rows and columns. Scalar subqueries are often used as expressions in SQL statements.
Click to reveal answer
intermediate
Example: What does this scalar subquery do?<br>
SELECT name, (SELECT MAX(score) FROM exams WHERE exams.student_id = students.id) AS max_score FROM students;
This query lists each student's name and their highest exam score. The scalar subquery finds the maximum score for each student by matching student IDs.
Click to reveal answer
beginner
Where can scalar subqueries be used in SQL statements?
Scalar subqueries can be used in SELECT lists, WHERE clauses, HAVING clauses, and SET clauses, anywhere a single value is expected.
Click to reveal answer
What does a scalar subquery return?
✗ Incorrect
A scalar subquery returns exactly one value, which means one row and one column.
Which SQL clause can contain a scalar subquery?
✗ Incorrect
Scalar subqueries can be used in the SELECT list to provide a single value per row.
What happens if a scalar subquery returns more than one row?
✗ Incorrect
If a scalar subquery returns more than one row, SQL raises an error because it expects a single value.
In this query, what is the role of the scalar subquery?<br>
SELECT product_name, (SELECT AVG(price) FROM products) AS avg_price FROM products;
✗ Incorrect
The scalar subquery calculates the average price once and shows it alongside each product.
Which of these is a valid use of a scalar subquery?
✗ Incorrect
Using a scalar subquery in a WHERE clause to compare a value is valid.
Explain what a scalar subquery is and give an example of where you might use it.
Think about a subquery that returns a single number or value to use in a condition or calculation.
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 the subquery result to a single value.
You got /3 concepts.