Understanding query control

  • Hello all,

    A colleague of mine showed me a query employing some methods of controlling query flow that I am not familiar with--in particular, the usage of the keywords inside, middle, and outside. Here is an example:

    update outside

    set OrderId = (select top 1 middle.OrderId from Orders middle

    where OrderDate =

    (select min(inside.OrderDate) from Orders inside where OrderTime > 0 and inside.CustomerId = middle.CustomerId and schedulestatus = 'D')

    and outside.CustomerId = middle.CustomerId)

    from CustomerTable outside

    where (CustomerType = 'H' or CustomerType = 'C')

    and not exists (select * from Orders where OrderId = outside.OrderId)

    These keywords seem like useful things to understand. While I generally understand what the query above does, I am having trouble looking up reference material for the usage of these keywords. Does anyone know what the usage of these keywords is called generally (e.g. query control)? Also, can someone point me in the right direction in terms of reference material to read to understand these keywords? I would appreciate any help that anyone can give.

    Thanks.

  • cjohn5552 (8/25/2009)


    Hello all,

    A colleague of mine showed me a query employing some methods of controlling query flow that I am not familiar with--in particular, the usage of the keywords inside, middle, and outside. Here is an example:

    update outside

    set OrderId = (select top 1 middle.OrderId from Orders middle

    where OrderDate =

    (select min(inside.OrderDate) from Orders inside where OrderTime > 0 and inside.CustomerId = middle.CustomerId and schedulestatus = 'D')

    and outside.CustomerId = middle.CustomerId)

    from CustomerTable outside

    where (CustomerType = 'H' or CustomerType = 'C')

    and not exists (select * from Orders where OrderId = outside.OrderId)

    These keywords seem like useful things to understand. While I generally understand what the query above does, I am having trouble looking up reference material for the usage of these keywords. Does anyone know what the usage of these keywords is called generally (e.g. query control)? Also, can someone point me in the right direction in terms of reference material to read to understand these keywords? I would appreciate any help that anyone can give.

    Thanks.

    What you are calling keywords are actually table aliases.

  • From what I see, these are not keywords as it is. The query is just Aliased using inside, outside and middle. You can replace those words with A, B and C and it will still work the same way.

    You beat me to it Lynn 🙂

    -Roy

  • Oh wow! Thanks Lynn, all of a sudden, I thought they put new operators in sql server, again. Good to know they are only ol'aliases!

    Cheers,

    J-F

  • Hahaha, that's funny. No wonder I was having trouble finding documentation for those "keywords". Sorry for the misunderstanding.

  • cjohn5552 (8/25/2009)


    Hahaha, that's funny. No wonder I was having trouble finding documentation for those "keywords". Sorry for the misunderstanding.

    No problem, it's why we are here, to help clear up misunderstandings and increase others knowledge.

  • Lynn Pettis (8/25/2009)


    cjohn5552 (8/25/2009)


    Hahaha, that's funny. No wonder I was having trouble finding documentation for those "keywords". Sorry for the misunderstanding.

    No problem, it's why we are here, to help clear up misunderstandings and increase others knowledge.

    And, occasionally learn something new ourselves - which is something I do every day :hehe:

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply