Logic Precedence

  • Didn't know whether OR or AND had priority, so guessed (correctly 🙂 )

    It's not something I've ever really needed to know, doesn't everyone use brackets in cases like this, to make it clear what is meant?

  • david.wright-948385 (2/12/2014)


    Always add brackets when mixing operators. Even if you know the rules, brackets make a calculation less prone to misunderstanding.

    Until you have several layers of brackets, at which point pairing them up can become as difficult as parsing the text. This can be alleviated by formatting, e.g:

    WHERE

    (

    (a = b)

    OR

    (b = c)

    )

    AND

    (

    (c = d)

    OR

    (

    (d = e)

    AND

    (e = f)

    )

    )

    as opposed to

    WHERE ((a = b) OR (b

    = c)) AND ((c = d) OR (

    (d = e) AND (e = f)))

    The latter, of course, being the type of code outputted by Microsoft's query designers on a regular basis...

  • twin.devil (2/12/2014)


    very good question for the beginners to understand what OR , AND does.

    Thanks for sharing ..

    Yes - and even for the experienced to remind them of the order of operations.:cool:

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • sknox (2/12/2014)


    ...This can be alleviated by formatting, e.g:

    WHERE

    (

    (a = b)

    OR

    (b = c)

    )

    AND

    ...

    as opposed to

    WHERE ((a = b) OR (b = c)) AND ((c = d) OR ((d = e) AND (e = f)))

    Exactly. IMHO, this goes for general query layout too.

  • david.wright-948385 (2/12/2014)


    sknox (2/12/2014)


    ...This can be alleviated by formatting, e.g:

    WHERE

    (

    (a = b)

    OR

    (b = c)

    )

    AND

    ...

    as opposed to

    WHERE ((a = b) OR (b = c)) AND ((c = d) OR ((d = e) AND (e = f)))

    Exactly. IMHO, this goes for general query layout too.

    +10

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Simple... Thanks, Steve!

  • david.wright-948385 (2/12/2014)

    Exactly. IMHO, this goes for general query layout too.

    My old boss thought I was being too neat when I put each join criteria on a separate line and each predicate of the where clause on it's own line. Then again, he wrote all his queries in lower case only, no spaces, and used the text editor line length as the only line "terminator". Guess who's code was easier to maintain. 😉

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • To illustrate the points being made about parenthesis.

    DECLARE @Logic TABLE ( ID INT, Product VARCHAR(50) );

    INSERT INTO @Logic

    VALUES ( 1, 'Baseball Hat' ),

    ( 2, 'Bicycle' ),

    ( 3, 'Snowboard' ),

    ( 4, 'Goggles' ),

    ( 5, 'Shows' );

    SELECT ID

    FROM @Logic

    WHERE (Product = 'Bicycle' OR Product = 'Snowboard') AND ID = 4;

    Changes the answer to the QotD.

    Enjoy!

  • Easiest SSC question in AGES. Thx SJ.

  • easy one..

    Thanks Steve.

  • Easy one. Surprised only 69% got it right.

  • Ed Wagner (2/12/2014)


    It was a simple question, but nonetheless a good one. How many times have we all been tripped up somewhere and spent hours on it, only to find that the problem was something so simple that we don't even want to admit it to ourselves? Always good to keep the basics in mind, no matter how far back they get. 😉

    +1 Great question Steve. Ed, amen to your post. I must say, it's happened to me before and a good refresher on the basics is always welcome. 🙂



    Everything is awesome!

  • Interesting one for me. Thanks Steve. 🙂

Viewing 13 posts - 16 through 27 (of 27 total)

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