Correlated Subquery?

  • 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

  • SQLSACT (1/9/2015)


    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

    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

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • 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)

  • SQLSACT (1/9/2015)


    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)

    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 ๐Ÿ˜‰

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    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

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (1/9/2015)


    No, that won't even run.

    Because of the columns in my select list?

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (1/9/2015)


    Try it and see?

    Yep

  • 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