---
title: "Nested Vs. Correlated Sub-Query: Top Differences"
description: "Nested queries and Correlated sub-queries are both different. Try to understand these with examples. https://srinimf.com/2023/10/09/the-easy-way-to-write-correlated-sub-query-5-examples/. Nested Queri..."
url: https://srinimf.com/2018/05/07/multiple-nested-vs-correlated-sub-query-tricky-top-differences/
date: 2018-05-07
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2018/05/pexels-photo-975261-e1525536796636.jpeg?fit=500%2C375&ssl=1
categories: ["SQL"]
tags: ["008080", "ff0000", "Nested query Vs Co-related sub query", "SQL tips and tricks"]
type: post
lang: en
---

# Nested Vs. Correlated Sub-Query: Top Differences

Nested queries and Correlated sub-queries are both different. Try to understand these with examples

Related[The Easy Way to Write Correlated Sub-query: 5 Examples](https://srinimf.com/2023/10/09/the-easy-way-to-write-correlated-sub-query-5-examples/)

## **Nested Queries**

A subquery can contain another query, making it a query with multiple nested subqueries.

Related[5 More Frequently Asked DB2 SQL Queries](https://srinimf.com/2015/05/15/5-more-frequently-asked-db2-sql-queries/)

```
SELECT customer.*
FROM customer
WHERE c_no IN
(SELECT c_no
FROM c_order
WHERE p_code IN
(SELECT p_code FROM product WHERE p_name NOT LIKE '%Nut%'
)
);
```

**My analysis**

- All the nested [SQL queries](https://srinimf.com/2023/11/09/how-to-update-a-table-neat-way-using-subquery/) run in sequence to get the result

## **Co-related sub-query**

A subquery can also be related to its outer query, where one or more columns from the outer query table are related to the column(s) of the subquery table in the WHERE clause of the subquery. This type of subquery is called the [correlated subquery](https://srinimf.com/2012/07/16/sql-sub-query-and-correlated-query/).

```
SELECT customer.*
FROM customer
WHERE c_no IN
(SELECT c_no
FROM c_order o
JOIN product p
ON o.p_code = p.p_code
WHERE p_name NOT LIKE '%Nut%'
AND customer.c_no = o.c_no
);
```

**My analysis**

- A correlated subquery has a dependency on the outer query

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2018%2F05%2F07%2Fmultiple-nested-vs-correlated-sub-query-tricky-top-differences%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2018%2F05%2Fpexels-photo-975261-e1525536796636.jpeg%3Ffit%3D500%252C375%26ssl%3D1&description=Nested%20%20Vs.%20Correlated%20Sub-Query%3A%20Top%20Differences)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2018/05/07/multiple-nested-vs-correlated-sub-query-tricky-top-differences/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2018/05/07/multiple-nested-vs-correlated-sub-query-tricky-top-differences/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2018/05/07/multiple-nested-vs-correlated-sub-query-tricky-top-differences/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2018/05/07/multiple-nested-vs-correlated-sub-query-tricky-top-differences/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Nested%20%20Vs.%20Correlated%20Sub-Query%3A%20Top%20Differences&body=https%3A%2F%2Fsrinimf.com%2F2018%2F05%2F07%2Fmultiple-nested-vs-correlated-sub-query-tricky-top-differences%2F&share=email)
-

### Like this:

Like Loading…
