Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Parentheses use in an OR clause

Was wondering whether anyone would know why do we use the parentheses in this SQL: So, the format goes as follows:

Name,location and department of the service of the employees whose name starts with A or B. (A rough translation from French).

I answered the following way:

SELECT service.nom_serv, localite.ville, localite.departemen
FROM service, localite, employe
WHERE service.code_loc=localite.code_loc
AND employe.service=service.code_serv
AND ((employe.nom LIKE 'A%') OR (employe.nom LIKE 'B%'))

Basically, where the last AND is concerned for the WHERE, couldn't I simply do without the parenthesis in order to have the SQL select for me employees with their name starting either with an A or a B? What difference does positioning a parenthesis in that way make? And ahy is there a double use of parentheses? Or is it to prioritize the OR in the last clause, since an AND is preceding it?

like image 777
angela Avatar asked May 11 '11 16:05

angela


People also ask

How do I use parentheses in SQL query?

Parenthesis are not needed for only two criteria. However, if you have more than two criteria, place your criteria within parenthesis. Use OR if you are trying to fulfill one or the other criterion. For example, you want A OR B but not both.

Why do we use parentheses in SQL?

You can use parentheses to make the intention of your code clearer. This becomes very important when using AND and OR clauses, to ensure your queries return the exact subsets you need.

Do parentheses work in SQL?

When writing SQL, using parentheses can also be a useful way to distinguish the order of operations and is very complementary to the AND and OR operators. Let's look at an example. We first start with a WHERE clause that has multiple conditions. In this example, we get two results.

What means '%' in SQL?

The SQL LIKE Operator There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.


2 Answers

Take a look at the Operator Precedence in SQL Server (You've not specified that, but I'd imagine it's the same for all RDBMS). What this means is that ANDs (without parenthesis) are evaluated before1 bind more tightly than ORs.

So in your specific case, without the parenthesis, the conditions are:

  • employe.service=service.code_serv AND employe.nom LIKE 'A%'

OR

  • employe.nom LIKE 'B%'

1Evaluation order is deliberately not specified in SQL, allowing many more possible re-orderings that languages that guarantee left-to-right or precedence ordered evaluation.

like image 82
Damien_The_Unbeliever Avatar answered Sep 30 '22 20:09

Damien_The_Unbeliever


You use it to specify grouping of the clause, not priority. SQL does not allow you to specify priority as the optimizer will create the best priority for you.

AND ()

Will take both of the OR conditions in one statement. So if either is true then the AND is true as well. The inner parentheses are not necessary, but help in visualizing the separation.

Without the outer parentheses it would allow anything with the final clause as true as well.

like image 30
Dustin Laine Avatar answered Sep 30 '22 18:09

Dustin Laine