0
0
SQLquery~5 mins

Scalar subquery in SELECT in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 in places where a single value is expected, such as in the SELECT list.
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 is a scalar subquery used in a SELECT statement?
It is placed inside the SELECT clause to compute a value for each row, for example: SELECT name, (SELECT MAX(score) FROM scores WHERE scores.user_id = users.id) AS max_score FROM users;
Click to reveal answer
intermediate
What happens if a scalar subquery returns no rows?
If a scalar subquery returns no rows, it returns NULL in the SELECT statement.
Click to reveal answer
intermediate
Why use a scalar subquery instead of a JOIN?
Scalar subqueries can be simpler to write and understand when you need a single value related to each row, especially for aggregation or lookup. JOINs are better for multiple related rows.
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
Where can you use a scalar subquery in SQL?
AOnly in the GROUP BY clause
BOnly in the WHERE clause
COnly in the FROM clause
DIn the SELECT clause
What happens if a scalar subquery returns no rows?
AIt returns zero
BIt returns NULL
CIt causes an error
DIt returns an empty string
What error occurs if a scalar subquery returns more than one row?
ADivision by zero error
BSyntax error
CSubquery returns more than one row error
DNo error, it works fine
Which is a good use case for scalar subqueries?
AGetting a single aggregated value per row
BJoining multiple tables with many rows
CUpdating multiple rows at once
DCreating indexes
Explain what a scalar subquery is and how it is used in a SELECT statement.
Think about a small query inside the SELECT that gives one value per row.
You got /4 concepts.
    What errors or results can happen if a scalar subquery returns zero or multiple rows?
    Consider what SQL expects from a scalar subquery's output.
    You got /3 concepts.