What is a correlated subquery?

Study for the SQL Basics Test. Improve your knowledge with multiple choice questions and detailed explanations. Prepare effectively to master SQL concepts!

Multiple Choice

What is a correlated subquery?

Explanation:
A correlated subquery is a subquery that references columns from the outer query. That reference ties the inner query to the specific row being processed in the outer query, so the inner query’s result can vary for each outer row. Because of this dependency, the inner query is evaluated once per row of the outer query, rather than just once. A common use is with EXISTS or IN to check for related rows that meet a condition for the current outer row. For example, if you’re listing employees who have a salary above a threshold, the inner query can refer to the outer employee’s id to test that condition. This dependence on the outer row distinguishes a correlated subquery from an uncorrelated one, which does not reference outer columns and can run independently. While a correlated subquery can return a scalar in some contexts, its defining feature is the reference to outer query columns, not the data type of the inner result.

A correlated subquery is a subquery that references columns from the outer query. That reference ties the inner query to the specific row being processed in the outer query, so the inner query’s result can vary for each outer row. Because of this dependency, the inner query is evaluated once per row of the outer query, rather than just once.

A common use is with EXISTS or IN to check for related rows that meet a condition for the current outer row. For example, if you’re listing employees who have a salary above a threshold, the inner query can refer to the outer employee’s id to test that condition. This dependence on the outer row distinguishes a correlated subquery from an uncorrelated one, which does not reference outer columns and can run independently. While a correlated subquery can return a scalar in some contexts, its defining feature is the reference to outer query columns, not the data type of the inner result.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy