SQL2
SQL2
Objectives
EMP
EMPNO ENAME JOB ... DEPTNO
–– The
The WHERE
WHERE clause
clause follows
follows the
the FROM
FROM
clause.
clause.
Using the WHERE Clause
SQL>
SQL> SELECT
SELECT ename,
ename, job,
job, deptno
deptno
22 FROM
FROM emp
emp
33 WHERE
WHERE ename
ename == 'JAMES';
'JAMES';
Comparison Operators
Operator Meaning
= Equal to
Operator Meaning
ENAME SAL
---------- --------- Lower Higher
MARTIN 1250 limit limit
TURNER 1500
WARD 1250
ADAMS 1100
MILLER 1300
Using the IN Operator
• Use the IN operator to test for values in a
list.
SQL> SELECT empno, ename, sal, mgr
2 FROM emp
3 WHERE mgr IN (7902, 7566, 7788);
ENAME
----------
MARTIN
JAMES
WARD
–– You
You can
can use
use the
the ESCAPE
ESCAPE identifier
identifier to
to search
search
for
for "%"
"%" or
or "_".
"_".
Using the IS NULL Operator
ENAME MGR
---------- ---------
KING
Logical Operators
Operator Meaning
ENAME JOB
---------- ---------
KING PRESIDENT
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
WARD SALESMAN
Rules of Precedence
Order Evaluated Operator
1 All comparison
operators
2 NOT
3 AND
4 OR
ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
KING
KING PRESIDENT
PRESIDENT 5000
5000
MARTIN
MARTIN SALESMAN
SALESMAN 1250
1250
ALLEN
ALLEN SALESMAN
SALESMAN 1600
1600
TURNER
TURNER SALESMAN
SALESMAN 1500
1500
WARD
WARD SALESMAN
SALESMAN 1250
1250
Rules of Precedence
Use
Use parentheses
parentheses to
to force
force priority.
priority.
SQL> SELECT ename, job, sal
2 FROM emp
3 WHERE (job='SALESMAN'
4 OR job='PRESIDENT')
5 AND sal>1500;
ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
KING
KING PRESIDENT
PRESIDENT 5000
5000
ALLEN
ALLEN SALESMAN
SALESMAN 1600
1600
ORDER BY Clause
–– Sort
Sort rows
rows with
with the
the ORDER
ORDER BY
BY clause
clause
•• ASC:
ASC: ascending
ascending order,
order, default
default
•• DESC:
DESC: descending
descending order
order
–– The
The ORDER
ORDER BYBY clause
clause comes
comes last
last in
in the
the
SELECT
SELECT statement.
statement.
–– Selecting
Selecting data
data and
and changing
changing the
the order
order of
of rows
rows
displayed
displayed
–– Restricting
Restricting rows
rows by
by using
using the
the WHERE
WHERE clause
clause
–– Using
Using the
the double
double quotation
quotation marks
marks in
in column
column aliases
aliases