SQL Statement- Rules of Precedence for Operators in an Expression



Rules of Precedence

The rules of precedence determine the order in which expressions are evaluated and calculated. The table above lists the default order of precedence. However, you can override the default order by using parentheses around the expressions that you want to calculate first.


    1-   Precedence of the AND Operator: Example

In this example, there are two conditions:
        The first condition is that the job ID is AD_PRES and the salary is greater than $15,000.
        The second condition is that the job ID is SA_REP.
Therefore, the SELECT statement reads as follows:
“Select the row if an employee is a president and earns more than $15,000, or if the employee is a sales representative.”
    
    2-  Using Parentheses: Example
In this example, there are two conditions:
        The first condition is that the job ID is AD_PRES or SA_REP.
        The second condition is that the salary is greater than $15,000.

Therefore, the SELECT statement reads as follows:
“Select the row if an employee is a president or a sales representative, and if the employee earns more than $15,000.”

SELECT last_name, job_id, salary FROM   employees WHERE  job_id = 'SA_REP' OR     job_id = AD_PRES' AND    salary > 15000;

SELECT last_name, job_id, salary FROM   employees WHERE  (job_id = 'SA_REP' OR     job_id = 'AD_PRES') AND    salary > 15000;