SQL: Using Parentheses with And / OR condition is necessary with where clause to gives expected result

/ / 4 Comments

Are parentheses necessary in SQL: In this article will explain the difference between a simple SQL select query using with parentheses and using without parentheses one, and you will notice the different result of each query. One day, my junior asked me one question why I am using parentheses ( brackets ) in most of my SQL query and is it really necessary to use round bracket. Then I thought why not make a post on it, so it helps other.

My answer was YES, I use parenthesis to bind components of where clauses, i.e., If the query has multiple and or condition, then it's necessary to use parentheses to ensure correct order of operations. In short for complex queries it necessary to use round Bracket and for the simple query, you can avoid round bracket.   Let's see with example

Example 1: Simple SQL query with/without parenthesis

  • Simple Query without parenthesis:
SELECT * 
FROM employee
WHERE department_id=1
Here's the output of above simple SQL query:  


  • Simple Query with parenthesis: Now we write the same query with parenthesis.
SELECT *
FROM employee
WHERE (department_id=1)
Here's the output of above query:

As you will see both gives the same result. Here in example 1, it won't affect the result as its a simple query. Now let's see to Example 2

Also Read: 1 Best SQL Script to take Backup of all DataBases.

Example 2: Complex SQL query with/without parenthesis (using and/or condition)

  • Complex SQL Query without parenthesis:
SELECT *
FROM employee
WHERE department_id=1 OR department_id=2
AND salary < 3000
Here's the result: SQL query without parenthesis.  


  • Complex SQL Query without parenthesis: Now we write the same query with parenthesis .i.e using round bracket over and or condition.
SELECT *
FROM employee
WHERE (department_id=1 OR department_id=2)
AND (salary < 3000)
Here's the result of above query:  



 You can easily get an idea both results are different. Here in example 2, round brackets determine the order that the AND and OR conditions are evaluated, same like what we learned in math order of operation (BODMAS).

Parentheses tell SQL Server to ignore the order of evaluation and assess what is in the parentheses first then evaluate the second part of the WHERE clause. Using sql and or queries without parentheses gives incorrect record.

Also Read: How to Get Time part from Datetime in MS SQL server?

Other Reference Link:

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to do a favor, then Buy me a coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

4 Comments

  1. Madi 02/03/2021 18:01:30
    Example 2- So, which query would you suggest better with or without parenthesis?
  2. Satinder singh 02/04/2021 14:03:37
    Hi Madi, Always prefer query with bracket, as this ensure us correct data.
  3. Rahul 03/07/2021 04:00:35
    Hi, I have a doubt, do these two queries give the same result select * from tableName where column1 > 100 and column2 < 50; select * from tableName where (column1 > 100) and column2 < 50; assume some of the records have column1 value null where column2 values are less than 50
  4. satinder singh 03/09/2021 13:47:09
    Hi rahul, Here in your case both result would be same, as you are using only and operator.