SQL Writing Subqueries 2 — Questions and Answers
Question 1: Which keyword tests whether a subquery returns any rows at all?
- EXISTS (Correct answer)
- INCLUDES
- CONTAINS
- HASROWS
Correct answer: EXISTS
EXISTS returns TRUE if the subquery produces one or more rows.
Question 2: A subquery that references a column from the outer query is called what?
- Scalar subquery
- Correlated subquery (Correct answer)
- Derived table
- Nested join
Correct answer: Correlated subquery
A correlated subquery depends on a column supplied by the outer query and runs per outer row.
Question 3: What does a scalar subquery return?
- A single value (Correct answer)
- Multiple columns
- An entire table
- A boolean only
Correct answer: A single value
A scalar subquery returns exactly one row and one column, i.e., a single value.
Question 4: Which operator lets a value match ANY result from a subquery returning multiple rows?
- IN (Correct answer)
- BETWEEN
- LIKE
- IS
Correct answer: IN
IN checks whether a value matches any value in the subquery's result set.
Question 5: Where can a subquery used as a derived table appear?
- In the FROM clause (Correct answer)
- Only in WHERE
- Only in GROUP BY
- Only in ORDER BY
Correct answer: In the FROM clause
A derived table is a subquery placed in the FROM clause and must be given an alias.
Question 6: What is required when a subquery is compared with = (equals)?
- It must return a single value (Correct answer)
- It must return multiple rows
- It must use EXISTS
- It must be correlated
Correct answer: It must return a single value
Using = with a subquery requires the subquery to return exactly one value, or an error occurs.
Question 7: Which clause CANNOT typically contain a subquery in standard SQL?
- The keyword list of an INDEX definition (Correct answer)
- WHERE
- FROM
- SELECT
Correct answer: The keyword list of an INDEX definition
Subqueries appear in SELECT, FROM, WHERE, and HAVING, but not within an index column definition.
Which keyword tests whether a subquery returns any rows at all?