ArticleslgStudy

science

Correlated subquery

Correlated subquery is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Correlated subquery rather than just read about it. In short: In a SQL database query, a correlated subquery (also known as a synchronized subquery) is a subquery (a query nested inside another query) that uses values from the outer query. This can have major impact on performance because the correlated subquery might get recomputed every time for each row of the outer query is processed.

Key takeaways

  • Correlated subquery belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Correlated subquery to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Correlated subquery from memory before moving on to harder problems.

Reference excerpt

In a SQL database query, a correlated subquery (also known as a synchronized subquery) is a subquery (a query nested inside another query) that uses values from the outer query. This can have major impact on performance because the correlated subquery might get recomputed every time for each row of the outer query is processed. A correlated subquery can contain another correlated subquery.

Examples

Correlated subqueries in the WHERE clause Here is an example for a typical correlated subquery. In this example, the objective is to find all employees whose salary is above average for their department.

In the above query the outer query is

and the inner query (the correlated subquery) is

In the above nested query the inner query has to be re-executed for each employee. (A sufficiently smart implementation may cache the inner query's result on a department-by-department basis, but even in the best case the inner query must be executed once per department.)

Correlated subqueries in the SELECT clause Correlated subqueries may appear elsewhere besides the WHERE clause; for example, this query uses a correlated subquery in the SELECT clause to print the entire list of employees alongside the average salary for each employee's department. Again, because the subquery is correlated with a column of the outer query, it must be re-executed for each row of the result.

Correlated subqueries in the FROM clause It is generally meaningless to have a correlated subquery in the FROM clause because the table in the FROM clause is needed to evaluate the outer query, but the correlated subquery in the FROM clause can't be evaluated before the outer query is evaluated, causing a chicken-and-egg problem. Specifically, MariaDB lists this as a limitation in its documentation. However, in some database systems, it is allowed to use correlated subqueries while joining in the FROM clause, referencing the tables listed before the join using a specified keyword, producing a number of rows in the correlated subquery and joining it to the table on the left. For example, in PostgreSQL, adding the keyword LATERAL before the right-hand subquery, or in Microsoft SQL Server, using the keyword CROSS APPLY or OUTER APPLY instead of JOIN achieves the effect.

Computation of correlated subqueries A commonly used computational method for a correlated subquery is to rewrite it into an equivalent flat query (a process known as flattening

or unnesting ). The algorithm development in this direction has an advantage of low complexity. Because this is a customized approach, existing database systems cannot flatten arbitrary correlated subqueries by following certain general rules. In addition, this approach requires high engineering efforts to implement flattening algorithms into a database engine. A general computational approach is to directly execute the nested loop by iterating all tuples of the correlated columns from the outer query block and executing the subquery as many times as the number of outer-loop tuples. This simple approach has an advantage of general-purpose because it is not affected by the type of correlated operators or subquery structures. However, it has a high computational complexity. A GPU acceleration approach is used to significantly improve the performance of the nested method of high algorithmic complexity by exploiting massive parallelism and device memory locality on GPU, which accomplishes the goal for both general-purpose software design and implementation and high performance in subquery processing.

References

External links Correlated subquery with examples

Worked examples

Example 1 — a first encounter with Correlated subquery

Start with the simplest possible case. Write down what Correlated subquery claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Correlated subquery before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Correlated subquery ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Correlated subquery

In research
Correlated subquery appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Correlated subquery in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Correlated subquery is common in secondary-school and first-year university syllabi. It links to neighbouring topics SQL, Subroutines, so understanding it makes those chapters shorter.
In everyday life
Look for Correlated subquery outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Correlated subquery” →

How to study Correlated subquery in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Correlated subquery means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Correlated subquery out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Correlated subquery in simple terms?

In a SQL database query, a correlated subquery (also known as a synchronized subquery) is a subquery (a query nested inside another query) that uses values from the outer query. This can have major impact on performance because the correlated subquery might get recomputed every time for each row of…

Why does Correlated subquery matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Correlated subquery?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Correlated subquery.

Tags

  • SQL
  • Subroutines

Keep exploring