• Tidak ada hasil yang ditemukan

Restricting and Sorting Data

N/A
N/A
Protected

Academic year: 2025

Membagikan "Restricting and Sorting Data"

Copied!
18
0
0

Teks penuh

(1)

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.

(2)

Rules of Precedence

Order Evaluated Operator

1 Arithmetic operators

2 Concatenation operator

3 Comparison conditions

4 IS [NOT] NULL, LIKE,

[NOT] IN

5 BETWEEN [NOT]

6 NOT logical condition

7 AND logical condition

8 OR logical condition

(3)

Rules of Precedence (Example)

(4)

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 second condition 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.

(5)

Rules of Precedence (Example)

(6)

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 second condition 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$

(7)

Order by caluse

(8)

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 Example11)

The ORDER BY clause comes last in the SELECT statement

(9)

Example 10

(10)

Example 11

(11)

Sorting by Column Alias

(12)

Sorting by Multiple Columns

(13)

Select statement syntax with

the (Where & order by clauses)

(14)

Comments on Using

Logical operator (NOT)

In term of syntax, generally, NOT comes between exper and

comparison operator

Select fname, ageE.g

Feom emp_table

Where dept_num NOT IN(1,2);

(15)

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

E.g

Select fname, age Feom emp_table

Where dept_num NOT >3;

(16)

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);

(17)

Comments on ordering table using more than one column

Assume that we’ve created the following table:

Then fill it with the values

Create table test2( col1 number(2), col2 number(2));

col2 Col1

9 1

8 2

10 3

10 2

5 3

4 4

4 3

(18)

Comments on ordering table using more than one column

The result of the query

Select * From test2

Oreder by col2,col1;

col2 Col1

4 3

4 4

5 3

8 2

9 1

10 2

10 3

And it’s NOT the same as ordering based on the last column (col2) which is:

col2 Col1

4 4

4 3

5 3

8 2

9 1

10 3

10 2

Referensi

Dokumen terkait