Rules of Precedence The rules of precedence determine

  • Slides: 18
Download presentation
Rules of Precedence The rules of precedence determine the order in which expressions are

Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The next table lists the default order of precedence. You can override the default order by using parentheses around the expressions you want to calculate first.

Rules of Precedence Order Evaluated 1 Operator Arithmetic operators 2 Concatenation operator 3 4

Rules of Precedence Order Evaluated 1 Operator Arithmetic operators 2 Concatenation operator 3 4 5 6 Comparison conditions IS [NOT] NULL, LIKE, [NOT] IN [NOT] BETWEEN 7 AND logical condition 8 OR logical condition NOT logical condition

Rules of Precedence (Example)

Rules of Precedence (Example)

Rules of Precedence (Example) In the previews slide there are two conditions: �The first

Rules of Precedence (Example) In the previews slide there are two conditions: �The first condition is that the job ID is AD_PRES and the salary is greater than $15, 000. � The secondition is that the job ID is SA_REP. Based on the precedence rules, 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. ”

Rules of Precedence (Example)

Rules of Precedence (Example)

Rules of Precedence (Example) In thepreviews example, there are two conditions: � The first

Rules of Precedence (Example) In thepreviews example, there are two conditions: � The first condition is that the job ID is AD_PRES or SA_REP. �The secondition is that salary is greater than $15, 000. � Based on the precedence rules, the SELECT statement reads as follows: “Select the rows if the employee is president or sales representative, and if the employee earn more than 15, 000$ ”

Order by caluse

Order by caluse

Sorting resulted rows SQL allows sorting resulted rows by using the ORDER BY clause

Sorting resulted rows SQL allows sorting resulted rows by using the ORDER BY clause in: ASC: ascending order (the default order). (see Example 10) DESC: descending order. (see Example 11) The ORDER BY clause comes last in the SELECT statement

Example 10

Example 10

Example 11

Example 11

Sorting by Column Alias

Sorting by Column Alias

Sorting by Multiple Columns

Sorting by Multiple Columns

Select statement syntax with the (Where & order by clauses)

Select statement syntax with the (Where & order by clauses)

Comments on Using Logical operator (NOT) In term of syntax, generally, NOT comes between

Comments on Using Logical operator (NOT) In term of syntax, generally, NOT comes between exper and comparison operator E. g Select fname, age Feom emp_table Where dept_num NOT IN(1, 2);

Comments on Using Logical operator (NOT) This syntax is right for the operators (IN,

Comments on Using Logical operator (NOT) This syntax is right for the operators (IN, Between. . And. . , LIKE, IS NULl) BUT In case the symbolic comparison operator (>, <, >=, <=, =, <>) there will be an error Select fname, age E. g Feom emp_table Where dept_num NOT >3;

Comments on Using Logical operator (NOT) The Solution is to use NOT pefore the

Comments on Using Logical operator (NOT) The Solution is to use NOT pefore the whole comparsion condition i. e. NOT(exper comparison operator) E. g Select fname, age Feom emp_table Where NOT (dept_num >3);

Comments on ordering table using more than one column Assume that we’ve created the

Comments on ordering table using more than one column Assume that we’ve created the following table: Create table test 2( col 1 number(2), col 2 number(2)); Then fill it with the values Col 1 col 2 1 9 2 8 3 10 2 10 3 5 4 4 3 4

Comments on ordering table using more than one column The result of the query

Comments on ordering table using more than one column The result of the query Select * From test 2 Oreder by col 2, col 1; Col 1 col 2 3 4 4 4 3 5 3 5 2 8 1 9 2 10 3 10 2 10 And it’s NOT the same as ordering based on the last column (col 2) which is: