Correlated subquery in sql Part 60 — Transcript
Full transcript
- 0:00hello welcome to brim Technologies I am
- 0:02Venit this is part 60 of SQL Server
- 0:04video series in this video we'll discuss
- 0:07about correlated subqueries in SQL
- 0:09server in part 59 of this video series
- 0:11we have discussed the basics of SQL
- 0:13Server we also discussed about how
- 0:15subqueries can be replaced using joints
- 0:18if you haven't watched that I would
- 0:19strongly encourage you to do so before
- 0:21continuing with the session in this
- 0:23session we'll discuss about correlated
- 0:24subqueries correlated subqueries are
- 0:27very simple and easy to understand now
- 0:30if the subquery depends on the outer
- 0:32query for its values then that subquery
- 0:34is called as a correlated subquery let's
- 0:37look at an example let's flip to SQL
- 0:38Server management studio now before we
- 0:40look at a correlated subquery let's look
- 0:43at a non-correlated subquery we'll be
- 0:46using the same tables that we have used
- 0:48in the previous session of this video
- 0:50series TBL products and TBL products
- 0:52sales TBL products table contains the
- 0:54product data and TBL product sales table
- 0:56contains sales related data so every
- 0:59time we sell a product an entry will be
- 1:00made into this table now let's say I
- 1:04want to retrieve all the products that
- 1:06has not been sold at least once how do I
- 1:08write that query obviously whenever we
- 1:10make a sale an entry is made into TBL
- 1:12product sales table now if you look at
- 1:14the sample data that we have we have
- 1:16sold product with ID 2 and three meaning
- 1:18laptop and desktops we haven't sold TV
- 1:21at least once so I want to retrieve uh
- 1:24the products that we have not sold at
- 1:26least one so how do I do that so select
- 1:29I want the ID name and description we'll
- 1:31we'll write those columns later first
- 1:33let's write the from Clause uh so TBL
- 1:36products select from TBL products now I
- 1:39have written the from class first
- 1:41because I'll get intellisense for the
- 1:43column name so it'll be easier to write
- 1:45type so select ID name and description
- 1:49okay so we want these three columns from
- 1:51this table
- 1:53where the
- 1:55ID column in TBL products
- 1:58table not not
- 2:02in in this table okay product ID not in
- 2:06this table so what I will do basically
- 2:08select product
- 2:10ID from TBL product sales and since
- 2:14there are duplicate product IDs I'm
- 2:17going to use the distinct keyword okay
- 2:19this is the same query that we have
- 2:21discussed about in the previous video
- 2:23session now look at this you know how
- 2:26does this query gets executed now the
- 2:29subquery gets it's executed first so if
- 2:31you look at this query this subquery can
- 2:33be executed independently so when I
- 2:36execute that query I get 2 and3 now we
- 2:38are saying select ID name description
- 2:40from TBL products where ID not in 2
- 2:43comma 3 which should give us the product
- 2:46with ID is equal to one okay this is an
- 2:49example of a noncorrelated subquery in a
- 2:52non-correlated subquery the subquery is
- 2:55not dependent on this outer query for
- 2:58its values the subquery can be executed
- 3:02independently on its own okay once the
- 3:05subquery is executed the results of this
- 3:07subquery is then substituted within
- 3:10those parentheses for use by the outer
- 3:12query okay so in a non-correlated
- 3:15subqueries subqueries usually gets
- 3:17executed only once okay now let's look
- 3:20at an example of a non-correlated
- 3:22subquery again we'll be using the same
- 3:24examples that we have discussed about in
- 3:25the previous session let's say for
- 3:27example I want to find out you know uh
- 3:30all the products that we have sold and
- 3:32the total quantity okay so basically uh
- 3:35we have three products and I want to
- 3:37find out all the three product names and
- 3:39the total Quant total you know quantity
- 3:42that we have sold so how do we do
- 3:44that so basically I want the name and
- 3:48then the total quantity now to retrieve
- 3:50the total quantity I want to use let's
- 3:52say subquery okay so I'm going to open
- 3:55and close parenthesis select now we want
- 3:58you know the total quantity that we have
- 4:00sold so I'm going to use sum aggregate
- 4:03function and sum off I want to aggregate
- 4:06this quantity sold column so
- 4:09quantity sold from TBL product sales
- 4:14table okay
- 4:17where product
- 4:19ID is equal
- 4:21to now for now let's substitute question
- 4:25mark
- 4:26there from TBL products table so where
- 4:30is name column coming from the name
- 4:32column is coming from TBL products table
- 4:34and I want to sum up this quantity sold
- 4:37for each product so the product
- 4:40ID is going to come from TBL products
- 4:46table okay so every row that we select
- 4:49from the outer query so first TV is
- 4:51selected let's say for example then what
- 4:55is the subquery look at the subquery
- 4:57that we have here select sum of
- 4:58quantities so
- 5:00so sum of quantity sold from TBL
- 5:04products where product ID is equal to
- 5:07look at this we are looking up the
- 5:08product ID in TBL products table so
- 5:11every time we select a row from the
- 5:12outer query now the subquery gets
- 5:15executed low so when I select this Row
- 5:18first it then executes this subquery
- 5:21when that row is being selected okay
- 5:23where product ID is equal to TBL
- 5:24products. ID here the ID is one it looks
- 5:28up that here we don't have an ID so sum
- 5:31for TVs will be null and then when it
- 5:34when the query executes the second row
- 5:37what happens it comes to the subquery
- 5:39looks up anvl product sales table is
- 5:41there an entry uh is there a product
- 5:44with id2 yes there is it tries to sum up
- 5:46that there's only one row so we only get
- 5:49seven and then finally when it comes to
- 5:51three okay product ID is three so there
- 5:53are three rows so it's going to add 5 4
- 5:56and 9 so 18 will be the sum so if you
- 5:59look at this now can I execute this
- 6:01subquery independently out of you know
- 6:04independently of the outer query no I
- 6:06can't look at this when I try to execute
- 6:08that I get an error because the
- 6:09multi-art identifier TBL products. ID
- 6:13cannot be bound now a correlated
- 6:16subquery will get executed for every row
- 6:19you know for the outer query in the
- 6:21outer query for every row you know this
- 6:23subquery gets executed okay so look at
- 6:26this when I execute this query together
- 6:30I get TV laptop and desktop and I get
- 6:32their some as well okay now I don't have
- 6:35a column name here but let's go ahead
- 6:36and give a meaningful column name using
- 6:39alas so let's say Quantity
- 6:42sold now we get a column name okay so
- 6:45that's a correlated subquery because
- 6:48this subquery is dependent on this outer
- 6:50query for its value for the value for
- 6:54this column product ID okay so that's a
- 6:56correlated subquery so if the subquery
- 6:59depends on the outer query for its
- 7:00values then the subquery is called as
- 7:02correlated subquery in a we Clause look
- 7:04at this in the we Clause of the subquery
- 7:07this product ID column gets its value
- 7:11from TBL products table and TBL products
- 7:13table is not present within the subquery
- 7:16okay it's present only within the outer
- 7:17query so this subquery is dependent on
- 7:20the outer query for its value in this
- 7:23case for product
- 7:24ID so here the subquery is dependent on
- 7:27the outer query for its value hence the
- 7:28subquery is a correlated subquery
- 7:30correlated subqueries get executed once
- 7:33for every row that is selected by the
- 7:35outer query correlated subquery cannot
- 7:38be executed independently of uh of the
- 7:40outer query but whereas a non-correlated
- 7:43subquery can be executed independently
- 7:47um of the outer
- 7:49query on this slide you can find
- 7:51resources for asp.net C and SQL server
- 7:53interview questions that's it for today
- 7:55thank you for listening have a great day
About this transcript
This page contains the full transcript of Correlated subquery in sql Part 60 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,328 words across 191 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.