Tech Junkie Blog - Real World Tutorials, Happy Coding!: SQL: Using Parentheses To Get Expected Result

Sunday, June 19, 2016

SQL: Using Parentheses To Get Expected Result

SQL Server as well as other DBMS has an order of evaluation that can throw you off. Especially when you have more than one comparison in the WHERE clause. In this example I will show you the difference between using a parentheses and not using one, and how by using parentheses can give the results that you want.  Suppose you want to get the products with CategoryID 1 and 2 that are priced less than 15 dollars in the Products table in the Northwind database. Here is the query without the parentheses:

SELECT CategoryID,ProductName,UnitPrice
FROM Products
WHERE CategoryID = 1 OR CategoryID =2 AND UnitPrice < 15

When you run the query above you would expect that all the records retrieved will have a unit price of less than $15 dollar but that is not the case. Below is the result from the query.



Unexpected results from SQL from queries without parentheses


As you can see several records have unit price that are greater than $15 dollars
Now let's run the query with parentheses

SELECT CategoryID,ProductName,UnitPrice
FROM Products
WHERE (CategoryID = 1 OR CategoryID =2AND UnitPrice < 15

Below is the result from the query

Get expected SQL results with parentheses

Now you are getting result that you've always wanted in the first place. The parentheses tells SQL Server to ignore the order of evaluation and evaluate what is in the parentheses first then evaluate the second part of the WHERE clause.

Similar Posts:

5 comments:


  1. Thanks for sharing this.,
    Leanpitch provides crash course in Brain hacks using NLP everyone can use it wisely.

    Brain hacks with NLP

    NLP training

    ReplyDelete
    Replies





    1. error404-store




      - -- Spamming tools --




      1-- cpanel https ssl secure
      2-- shell ssl secure
      3-- web-mail
      4-- smtp
      5-- rdp with send bluster
      6-- scam pages
      7-- ceo or cfo leads




      -- Spy virus slinet Exploits --




      1-- privet rat
      2-- crypter exploits
      3-- privet rdp hosting
      4-- os andriod spy virus
      5-- iso iPhone spy virus
      6-- Nokia spy virus




      - -- scanners brute-force ---




      1-- linux roots
      2-- cpanel scanner
      3-- cpanel bruter
      4-- shell scanner
      5-- smtp scanner
      6-- smtp bruter
      7-- zimbra scanners


      - -------------------


      cpanel,shell,smtp,scampages,sender,ceo or cfo leads,rdp,spyvirus,crypter,exploits,scanners


      Telegram:- @Donsmith000


      icq :- @Donsmith000


      https://error404-store.blogspot.com



      Delete


    2. Hello all
      am looking few years that some guys comes into the market
      they called themselves hacker, carder or spammer they rip the
      peoples with different ways and it’s a badly impact to real hacker
      now situation is that peoples doesn’t believe that real hackers and carder scammer exists.
      Anyone want to make deal with me any type am available

      Available Services

      ..Wire Bank Transfer all over the world

      ..Western Union Transfer all over the world

      ..Credit Cards (USA, UK, AUS, CAN, NZ)

      ..School Grade upgrade / remove Records

      ..Spamming Tool

      ..keyloggers / rats

      ..Social Media recovery

      .. Teaching Hacking / spamming / carding (1/2 hours course)

      discount for re-seller

      Contact: 24/7

      fixitrogers@gmail.com




      Delete
  2. This way they don't have to spend time opening hundreds of card packs or participating in modes they don't like to complete challenges. The way to get a lot of Coins is also very simple, that is to Buy FUT 22 Coins at UTnice. UTnice supports a variety of secure payment methods, and their website provides Comfort Trade delivery methods, you only need to pay and enter the necessary information correctly. Their delivery team will help you complete the rest.

    ReplyDelete

Search This Blog