January 9, 2015 at 3:42 am
Hi all
I came across the following T-SQL query in an article I was reading.
Can someone please explain what type of query this is?
Is this some type of correlated subquery?
SELECT sku, product_description,
(SELECT plant_nbr
FROM ProductPlants AS B
WHERE B.sku = A.sku) AS plant_nbr
FROM Products AS A;
Thanks
January 9, 2015 at 3:51 am
SQLSACT (1/9/2015)
Hi allI came across the following T-SQL query in an article I was reading.
Can someone please explain what type of query this is?
Is this some type of correlated subquery?
SELECT sku, product_description,
(SELECT plant_nbr
FROM ProductPlants AS B
WHERE B.sku = A.sku) AS plant_nbr
FROM Products AS A;
Thanks
Yes it is, because the inner subquery includes a reference to a column in the main outer query.
It's a weird one, because it's much simpler to write
select p.sku
,p.product_description
,pp.plant_nbr
from dbo.Products p
join dbo.ProductPlants pp on p.sku = pp.sku
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
January 9, 2015 at 4:18 am
Thanks
Is it the same as:
select p.sku
,p.product_description
,pp.plant_nbr
from dbo.Products p
where exists (select pp.sku from dbo.ProductPlants pp where p.sku = pp.sku)
January 9, 2015 at 4:28 am
SQLSACT (1/9/2015)
ThanksIs it the same as:
select p.sku
,p.product_description
,pp.plant_nbr
from dbo.Products p
where exists (select pp.sku from dbo.ProductPlants pp where p.sku = pp.sku)
No. What would you return if there were multiple matches in the subquery?
I find this more intuitive:
select p.sku
,p.product_description
,plant_nbr = CAST(NULL AS INT)
from dbo.Products p
where exists (select 1 from dbo.ProductPlants pp where p.sku = pp.sku)
Edited: removed unavailable column ๐
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 9, 2015 at 4:29 am
No, that won't even run.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 9, 2015 at 4:37 am
GilaMonster (1/9/2015)
No, that won't even run.
Because of the columns in my select list?
January 9, 2015 at 5:10 am
Try it and see?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 9, 2015 at 5:17 am
GilaMonster (1/9/2015)
Try it and see?
Yep
January 9, 2015 at 2:48 pm
SQLSACT (1/9/2015)
SELECT sku, product_description,(SELECT plant_nbr
FROM ProductPlants AS B
WHERE B.sku = A.sku) AS plant_nbr
FROM Products AS A;
Thanks. Is it the same as:
select p.sku
,p.product_description
,pp.plant_nbr
from dbo.Products p
where exists (select pp.sku from dbo.ProductPlants pp where p.sku = pp.sku)
Not at all. Two fundamentally different queries.
The first query will return all rows from Products even if it finds no matches in ProductPlants -- any row that doesn't have a match will just return NULL instead of an actual plant_nbr. And the query will fail if more than one matching rows exists in ProductPlants, since a full table result cannot be returned into a single column that way.
The second query would only return rows that find a match in ProductPlants, but multiple matches will not cause a problem, except that it can't run because "pp" is in an EXISTS() subquery, and thus is not available to, and thus "can't be seen by", the outer query.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply