You are on page 1of 74

Introduction

Copyright

Oracle Corporation, 1998. All rights reserved.

Data Storage on Different Media


SALGRADE
SALGRADE

DEPTNO
DEPTNO
----------------YORK
YORK
20
20
30
30
40
40

GRADE
LOSAL
HISAL
GRADE
LOSAL
HISAL
----------------- ----------------- ----------------DEPT
DEPT
11
700
1200
700
1200
DNAME
LOC
DNAME
LOC
22
1201
1400
1201
1400
--------------------------- ------------------33
1401
2000
1401
2000
10
NEW
10 ACCOUNTING
ACCOUNTING
NEW
44
2001
3000
2001
3000
55
3001
9999
3001
9999
RESEARCH
DALLAS
RESEARCH
DALLAS
SALES
CHICAGO
SALES
CHICAGO
OPERATIONS
BOSTON
OPERATIONS
BOSTON

Database
Electronic
spreadsheet
I-2

Filing cabinet
Copyright

Oracle Corporation, 1998. All rights reserved.

Relational Database Concept


Dr. E. F. Codd proposed the relational model
for database systems in 1970.
It is the basis for the relational database
management system (RDBMS).
The relational model consists of the following:
Collection of objects or relations
Set of operators to act on the relations
Data integrity for accuracy and consistency

I-3

Copyright

Oracle Corporation, 1998. All rights reserved.

Relational Database Definition


A relational database is a collection of
relations or two-dimensional tables.
Database

Table Name: EMP


EMPNO
EMPNO
7839
7839
7698
7698
7782
7782
7566
7566

I-4

ENAME
ENAME
KING
KING
BLAKE
BLAKE
CLARK
CLARK
JONES
JONES

JOB
JOB
PRESIDENT
PRESIDENT
MANAGER
MANAGER
MANAGER
MANAGER
MANAGER
MANAGER

Copyright

DEPTNO
DEPTNO
10
10
30
30
10
10
20
20

Table Name: DEPT


DEPTNO
DEPTNO
10
10
20
20
30
30
40
40

DNAME
DNAME
ACCOUNTING
ACCOUNTING
RESEARCH
RESEARCH
SALES
SALES
OPERATIONS
OPERATIONS

Oracle Corporation, 1998. All rights reserved.

LOC
LOC
NEW
NEW YORK
YORK
DALLAS
DALLAS
CHICAGO
CHICAGO
BOSTON
BOSTON

Data Models

Model of
system
in clients
mind

Entity model of
clients model
Table model
of entity model

Server

Tables on disk
I-5

Copyright

Oracle Corporation, 1998. All rights reserved.

Entity Relationship Model


Create an entity relationship diagram from
business specifications or narratives
EMPLOYEE
EMPLOYEE
#*
number
#*
number
**
name
name
oo
job
job title
title

DEPARTMENT
DEPARTMENT
#*
number
#*
number
**
name
name
composed of
oo
location
location

assigned to

Scenario
. . . Assign one or more employees to a
department . . .
. . . Some departments do not yet have
assigned employees . . .
I-6

Copyright

Oracle Corporation, 1998. All rights reserved.

Entity Relationship Modeling


Conventions
Entity
Soft box
Singular, unique name
Uppercase
Synonym in parentheses

EMPLOYEE
EMPLOYEE
#*
number
#*
number
**
name
name
oo
job
job title
title

Attribute
Singular name
Lowercase
Mandatory marked with *
Optional marked with o

DEPARTMENT
DEPARTMENT
#*
number
#*
number
*
name
name
composed of *
oo
location
location

assigned to

Unique Identifier (UID)


Primary marked with #
Secondary marked with (#)
I-7

Copyright

Oracle Corporation, 1998. All rights reserved.

Relational Database Terminology


2

EMPNO ENAME

JOB

4
MGR

HIREDATE

SAL

COMM DEPTNO

------------- ------------ --------------------- -------- ---------------- ----------- --------------

1
I-8

7839 KING

PRESIDENT

7698 BLAKE

MANAGER

7782 CLARK

-----------

17-NOV-81

5000

10

7839

01-MAY-81

2850

30

MANAGER

7839

09-JUN-81

2450

10

7566 JONES

MANAGER

7839

02-APR-81

2975

20

7654 MARTIN

SALESMAN

7698

28-SEP-81

1250

1400

30

7499 ALLEN

SALESMAN

7698

20-FEB-81

1600

300

30

7844 TURNER SALESMAN

7698

08-SEP-81

1500

30

7900 JAMES

CLERK

7698

03-DEC-81

950

7521 WARD

SALESMAN

7698

22-FEB-81

1250

7902 FORD

ANALYST

7566

03-DEC-81

3000

20

7369

SMITH

CLERK

7902

17-DEC-80

800

20

7788

SCOTT

ANALYST

7566

09-DEC-82

3000

20

7876

ADAMS

CLERK

7788

12-JAN-83

1100

20

7934

MILLER

CLERK

7782

23-JAN-82

1300

10

Copyright

Oracle Corporation, 1998. All rights reserved.

30
500

30

Relating Multiple Tables


Each row of data in a table is uniquely
identified by a primary key (PK).
You can logically relate data from multiple
tables using foreign keys (FK).
Table Name: EMP
EMPNO
7839
7698
7782
7566

ENAME
KING
BLAKE
CLARK
JONES

Primary key
I-9

Table Name: DEPT


JOB
PRESIDENT
MANAGER
MANAGER
MANAGER

DEPTNO
10
30
10
20

Foreign key
Copyright

DEPTNO
10
20
30
40

DNAME
ACCOUNTING
RESEARCH
SALES
OPERATIONS

Primary key

Oracle Corporation, 1998. All rights reserved.

LOC
NEW YORK
DALLAS
CHICAGO
BOSTON

Relational Database Properties


A relational database
Can be accessed and modified by
executing structured query language
(SQL) statements
Contains a collection of tables with no
physical pointers
Uses a set of operators

I-10

Copyright

Oracle Corporation, 1998. All rights reserved.

Communicating with a RDBMS


Using SQL
SQL statement
is entered
SQL>
SQL> SELECT
SELECT loc
loc
22 FROM
dept;
FROM
dept;

Statement is sent to
database
Database

Data is displayed
LOC
LOC
------------------------NEW
NEW YORK
YORK
DALLAS
DALLAS
CHICAGO
CHICAGO
BOSTON
BOSTON
I-11

Copyright

Oracle Corporation, 1998. All rights reserved.

SQL Statements
SELECT

Data retrieval

INSERT
UPDATE
DELETE

Data manipulation language (DML)

CREATE
ALTER
DROP
RENAME
TRUNCATE

Data definition language (DDL)

COMMIT
ROLLBACK
SAVEPOINT
GRANT
REVOKE
I-12

Copyright

Transaction control

Data control language (DCL)


Oracle Corporation, 1998. All rights reserved.

What Is PL/SQL?
PL/SQL is an extension to SQL with
design features of programming
languages.
Data manipulation and query statements
of SQL are included within procedural
units of code.

I-13

Copyright

Oracle Corporation, 1998. All rights reserved.

PL/SQL Environment
PL/SQL engine
PL/SQL
block

PL/SQL
block

PL/SQL
SQL

Procedural
Statement
Executor

SQL Statement Executor


Oracle10g Server

I-14

Copyright

Oracle Corporation, 1998. All rights reserved.

Benefits of PL/SQL
It is portable.
You can declare identifiers.
You can program with procedural
language control structures.
It can handle errors.

I-15

Copyright

Oracle Corporation, 1998. All rights reserved.

SQL and SQL*Plus Interaction


SQL Statements

Server
SQL*Plus
Query Results

Buffer

SQL
scripts
I-16

Copyright

Oracle Corporation, 1998. All rights reserved.

SQL Statements Versus


SQL*Plus Commands
SQL
A language
ANSI standard
Keyword cannot be
abbreviated
Statements manipulate
data and table
definitions in the
database
SQL
statements
I-17

SQL
buffer
Copyright

SQL*Plus
An environment
Oracle proprietary
Keywords can be
abbreviated
Commands do not
allow manipulation of
values in the database
SQL*Plus
commands

Oracle Corporation, 1998. All rights reserved.

SQL*Plus
buffer

Overview of SQL*Plus
Log in to SQL*Plus.
Describe the table structure.
Edit your SQL statement.
Execute SQL from SQL*Plus.
Save SQL statements to files and
append SQL statements to files.
Execute saved files.
Load commands from file to buffer
to edit.
I-18

Copyright

Oracle Corporation, 1998. All rights reserved.

Logging In to SQL*Plus
From Windows environment:

From command line:


sqlplus [username[/password
[@database]]]
I-19

Copyright

Oracle Corporation, 1998. All rights reserved.

Tables Used in the Course


EMP
EMPNO
JOB
MGR
EMPNO ENAME
ENAME
JOB
MGR
----------------- ------------------- ----------------- ----------------7839
KING
PRESIDENT
7839 KING
PRESIDENT
7698
MANAGER
7839
7698 BLAKE
BLAKE
MANAGER
7839
7782
MANAGER
7839
7782 CLARK
CLARK
MANAGER
7839
7566
MANAGER
7839
7566 JONES
JONES
MANAGER
7839
7654
SALESMAN
7698
7654 MARTIN
MARTIN
SALESMAN
7698
7499
SALESMAN
7698
7499 ALLEN
ALLEN
SALESMAN
7698
7844
SALESMAN
7698
7844 TURNER
TURNER
SALESMAN
7698
7900
CLERK
7698
DEPT
7900 JAMES
JAMES
CLERK
7698
7521
WARD
SALESMAN
7698
7521
SALESMAN
7698
DEPTNO
LOC
DEPTNO DNAME
DNAMEWARD
LOC
7902
FORD
ANALYST
7566
7902
FORD
ANALYST
7566
-------------------------- -----------------------------------7369
SMITH
CLERK
7902
7369 10
SMITH
CLERK NEW
7902
ACCOUNTING
10
ACCOUNTING
NEW
7788
ANALYST
7566
7788 SCOTT
SCOTT
ANALYST
7566
YORK
YORK
7876
ADAMS
CLERK
7788
7876
ADAMS
CLERK
7788
20
DALLAS
20 RESEARCH
RESEARCH
DALLAS
7934
MILLER
CLERK
7782
7934
CLERK
7782
30
CHICAGO
30 SALES
SALESMILLER
CHICAGO
40
BOSTON
40 OPERATIONS
OPERATIONS
BOSTON

I-20

Copyright

HIREDATE
SAL
COMM
DEPTNO
HIREDATE
SAL
COMM
DEPTNO
----------------- ----------------- ----------------- ----------------17-NOV-81
5000
10
17-NOV-81
5000
10
01-MAY-81
2850
30
01-MAY-81
2850
30
09-JUN-81
1500
10
09-JUN-81
1500
10
02-APR-81
2975
20
02-APR-81
2975
20
28-SEP-81
1250
1400
30
28-SEP-81
1250
1400
30
20-FEB-81
1600
300
30
20-FEB-81
1600
300
30
08-SEP-81
1500
00
30
08-SEP-81
1500
30
03-DEC-81
950
30
03-DEC-81
950
30
22-FEB-81
1250
500
30
22-FEB-81
1250
500
30
03-DEC-81
3000
20
03-DEC-81 SALGRADE
3000
20
17-DEC-80
800
20
17-DEC-80
800
20
GRADE
LOSAL
HISAL
GRADE
LOSAL
HISAL
09-DEC-82
3000
20
09-DEC-82 --------3000 --------20
----------------- ----------------12-JAN-83
1100
20
12-JAN-83
1100 11
20
700
1200
700
1200
23-JAN-82
1300
10
23-JAN-82
1300 22
10
1201
1400
1201
1400
33
1401
2000
1401
2000
44
2001
3000
2001
3000
55
3001
9999
3001
9999

Oracle Corporation, 1998. All rights reserved.

Displaying Table Structure


Use the SQL*Plus DESCRIBE command to
display the structure of a table.
DESC[RIBE]
DESC[RIBE] tablename
tablename

I-21

Copyright

Oracle Corporation, 1998. All rights reserved.

Displaying Table Structure


SQL>
SQL> DESCRIBE
DESCRIBE dept
dept
Name
Name
--------------------------------DEPTNO
DEPTNO
DNAME
DNAME
LOC
LOC

I-22

Copyright

Null?
Null?
--------------NOT
NOT NULL
NULL

Type
Type
------NUMBER(2)
NUMBER(2)
VARCHAR2(14)
VARCHAR2(14)
VARCHAR2(13)
VARCHAR2(13)

Oracle Corporation, 1998. All rights reserved.

Writing Basic
SQL Statements

Copyright

Oracle Corporation, 1998. All rights reserved.

Capabilities of SQL SELECT


Statements
Selection

Projection

Table 1

Table 1

Table 1
I-24

Copyright

Join

Table 2
Oracle Corporation, 1998. All rights reserved.

Basic SELECT Statement


SELECT
SELECT
FROM
FROM

[DISTINCT]
[DISTINCT] {*,
{*, column
column [alias],...}
[alias],...}
table;
table;

SELECT identifies what columns


FROM identifies which table

I-25

Copyright

Oracle Corporation, 1998. All rights reserved.

Writing SQL Statements


SQL statements are not case sensitive.
SQL statements can be on one or
more lines.
Keywords cannot be abbreviated or split
across lines.
Clauses are usually placed on
separate lines.
Tabs and indents are used to enhance
readability.
I-26

Copyright

Oracle Corporation, 1998. All rights reserved.

Selecting All Columns


SQL> SELECT *
2 FROM
dept;
DEPTNO
--------10
20
30
40

I-27

DNAME
-------------ACCOUNTING
RESEARCH
SALES
OPERATIONS

Copyright

LOC
------------NEW YORK
DALLAS
CHICAGO
BOSTON

Oracle Corporation, 1998. All rights reserved.

Selecting Specific Columns


SQL> SELECT deptno, loc
2 FROM
dept;
DEPTNO
--------10
20
30
40

I-28

LOC
------------NEW YORK
DALLAS
CHICAGO
BOSTON

Copyright

Oracle Corporation, 1998. All rights reserved.

Column Label Defaults


Default justification
Left: Date and character data
Right: Numeric data

Default display: Uppercase

I-29

Copyright

Oracle Corporation, 1998. All rights reserved.

Arithmetic Expressions
Create expressions on NUMBER and DATE
data types by using arithmetic operators.
Operator

I-30

Description

Add

Subtract

Multiply

Divide

Copyright

Oracle Corporation, 1998. All rights reserved.

Using Arithmetic Operators


SQL> SELECT ename, sal, sal+300
2 FROM
emp;
ENAME
SAL
SAL+300
---------- --------- --------KING
5000
5300
BLAKE
2850
3150
CLARK
2450
2750
JONES
2975
3275
MARTIN
1250
1550
ALLEN
1600
1900
...
14 rows selected.

I-31

Copyright

Oracle Corporation, 1998. All rights reserved.

Operator Precedence

* / +

Multiplication and division take priority


over addition and subtraction.
Operators of the same priority are
evaluated from left to right.
Parentheses are used to force prioritized
evaluation and to clarify statements.

I-32

Copyright

Oracle Corporation, 1998. All rights reserved.

Operator Precedence
SQL> SELECT ename, sal, 12*sal+100
2 FROM
emp;
ENAME
SAL 12*SAL+100
---------- --------- ---------KING
5000
60100
BLAKE
2850
34300
CLARK
2450
29500
JONES
2975
35800
MARTIN
1250
15100
ALLEN
1600
19300
...
14 rows selected.

I-33

Copyright

Oracle Corporation, 1998. All rights reserved.

Using Parentheses
SQL> SELECT ename, sal, 12*(sal+100)
2 FROM
emp;
ENAME
SAL 12*(SAL+100)
---------- --------- ----------KING
5000
61200
BLAKE
2850
35400
CLARK
2450
30600
JONES
2975
36900
MARTIN
1250
16200
...
14 rows selected.

I-34

Copyright

Oracle Corporation, 1998. All rights reserved.

Defining a Null Value


A null is a value that is unavailable,
unassigned, unknown, or inapplicable.
A null is not the same as zero or a blank
space.
SQL> SELECT
2 FROM

ename, job, comm


emp;

ENAME
JOB
COMM
---------- --------- --------KING
PRESIDENT
BLAKE
MANAGER
...
TURNER
SALESMAN
0
...
14 rows selected.
I-35

Copyright

Oracle Corporation, 1998. All rights reserved.

Null Values in Arithmetic


Expressions
Arithmetic expressions containing a null
value evaluate to null.
SQL> select ename NAME, 12*sal+comm
2 from
emp
3 WHERE ename='KING';

NAME
12*SAL+COMM
---------- ----------KING

I-36

Copyright

Oracle Corporation, 1998. All rights reserved.

Defining a Column Alias


Renames a column heading
Is useful with calculations
Immediately follows column name;
optional AS keyword between column
name and alias
Requires double quotation marks if it
contains spaces or special characters or
is case sensitive
I-37

Copyright

Oracle Corporation, 1998. All rights reserved.

Using Column Aliases


SQL> SELECT ename AS name, sal salary
2 FROM
emp;
NAME
SALARY
------------- --------...
SQL> SELECT ename "Name",
2
sal*12 "Annual Salary"
3 FROM
emp;
Name
Annual Salary
------------- ------------...
I-38

Copyright

Oracle Corporation, 1998. All rights reserved.

Concatenation Operator
Concatenates columns or character
strings to other columns
Is represented by two vertical bars (||)
Creates a resultant column that is a
character expression

I-39

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the Concatenation


Operator
SQL> SELECT
2 FROM

ename||job AS "Employees"
emp;

Employees
------------------KINGPRESIDENT
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER
MARTINSALESMAN
ALLENSALESMAN
...
14 rows selected.
I-40

Copyright

Oracle Corporation, 1998. All rights reserved.

Literal Character Strings


A literal is a character, expression, or
number included in the SELECT list.
Date and character literal values must
be enclosed within single quotation
marks.
Each character string is output once for
each row returned.

I-41

Copyright

Oracle Corporation, 1998. All rights reserved.

Using Literal Character Strings


SQL> SELECT ename ||' '||'is a'||' '||job
2
AS "Employee Details"
3 FROM
emp;
Employee
Employee Details
Details
------------------------------------------------KING
KING is
is aa PRESIDENT
PRESIDENT
BLAKE
BLAKE is
is aa MANAGER
MANAGER
CLARK
CLARK is
is aa MANAGER
MANAGER
JONES
JONES is
is aa MANAGER
MANAGER
MARTIN
MARTIN is
is aa SALESMAN
SALESMAN
...
...
14
14 rows
rows selected.
selected.

I-42

Copyright

Oracle Corporation, 1998. All rights reserved.

Duplicate Rows
The default display of queries is all rows,
including duplicate rows.
SQL>
SQL>
22

SELECT
SELECT
FROM
FROM

deptno
deptno
emp;
emp;

DEPTNO
--------10
30
10
20
...
14 rows selected.

I-43

Copyright

Oracle Corporation, 1998. All rights reserved.

Eliminating Duplicate Rows


Eliminate duplicate rows by using the
DISTINCT keyword in the SELECT clause.
SQL> SELECT DISTINCT deptno
2 FROM
emp;

DEPTNO
--------10
20
30

I-44

Copyright

Oracle Corporation, 1998. All rights reserved.

SQL*Plus Editing Commands


A[PPEND] text
C[HANGE] / old / new
C[HANGE] / text /
CL[EAR] BUFF[ER]
DEL
DEL n
DEL m n
I-45

Copyright

Oracle Corporation, 1998. All rights reserved.

SQL*Plus Editing Commands


I[NPUT]
I[NPUT] text
L[IST]
L[IST] n
L[IST] m n
R[UN]
n
n text
0 text
I-46

Copyright

Oracle Corporation, 1998. All rights reserved.

SQL*Plus File Commands


SAVE filename
GET filename
START filename
@ filename
EDIT filename
SPOOL filename
EXIT
I-47

Copyright

Oracle Corporation, 1998. All rights reserved.

Practice Overview
Selecting all data from different tables.
Describing the structure of tables.
Performing arithmetic calculations and
specifying column names.
Using SQL*Plus editor.

I-48

Copyright

Oracle Corporation, 1998. All rights reserved.

Restricting and Sorting Data

Copyright

Oracle Corporation, 1998. All rights reserved.

Limiting Rows Using a Selection


EMP
EMPNO ENAME
7839
7698
7782
7566
...

KING
BLAKE
CLARK
JONES

JOB

...

DEPTNO

PRESIDENT
MANAGER
MANAGER
MANAGER

10
30
10
20

retrieve all
employees
in department 10

EMP
EMPNO ENAME

JOB

7839 KING
PRESIDENT
7782 CLARK MANAGER
7934 MILLER CLERK

I-54

Copyright

Oracle Corporation, 1998. All rights reserved.

...

DEPTNO
10
10
10

Limiting Rows Selected


Restrict the rows returned by using the
WHERE clause.
SELECT
FROM
[WHERE

[DISTINCT] {*, column [alias], ...}


table
condition(s)];

The WHERE clause follows the FROM


clause.

I-55

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the WHERE Clause


SQL> SELECT ename, job, deptno
2 FROM
emp
3 WHERE job='CLERK';

ENAME
---------JAMES
SMITH
ADAMS
MILLER

I-56

JOB
DEPTNO
--------- --------CLERK
30
CLERK
20
CLERK
20
CLERK
10

Copyright

Oracle Corporation, 1998. All rights reserved.

Character Strings and Dates


Character strings and date values are
enclosed in single quotation marks
Character values are case-sensitive and
date values are format-sensitive
Default date format is 'DD-MON-YY'
SQL>
SQL>
22
33

I-57

SELECT
SELECT
FROM
FROM
WHERE
WHERE

ename,
ename, job,
job, deptno
deptno
emp
emp
ename
ename == 'JAMES';
'JAMES';

Copyright

Oracle Corporation, 1998. All rights reserved.

Comparison Operators
Operator

I-58

Meaning

Equal to

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

<>

Not equal to

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the Comparison


Operators
SQL> SELECT ename, sal, comm
2 FROM
emp
3 WHERE sal<=comm;

ENAME
SAL
COMM
---------- --------- --------MARTIN
1250
1400

I-59

Copyright

Oracle Corporation, 1998. All rights reserved.

Other Comparison Operators

I-60

Operator

Meaning

BETWEEN
...AND...

Between two values (inclusive)

IN(list)

Match any of a list of values

LIKE

Match a character pattern

IS NULL

Is a null value

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the BETWEEN Operator


Use the BETWEEN operator to display
rows based on a range of values.
SQL> SELECT
2 FROM
3 WHERE

ename, sal
emp
sal BETWEEN 1000 AND 1500;

ENAME
SAL
---------- --------MARTIN
1250
TURNER
1500
WARD
1250
ADAMS
1100
MILLER
1300

I-61

Copyright

Lower
limit

Higher
limit

Oracle Corporation, 1998. All rights reserved.

Using the IN Operator


Use the IN operator to test for values in a
list.
SQL> SELECT
2 FROM
3 WHERE

EMPNO
--------7902
7369
7788
7876
I-62

empno, ename, sal, mgr


emp
mgr IN (7902, 7566, 7788);

ENAME
SAL
MGR
---------- --------- --------FORD
3000
7566
SMITH
800
7902
SCOTT
3000
7566
ADAMS
1100
7788
Copyright

Oracle Corporation, 1998. All rights reserved.

Using the LIKE Operator


Use the LIKE operator to perform
wildcard searches of valid search string
values.
Search conditions can contain either
literal characters or numbers.
(%) denotes zero or many characters
( _ ) denotes one character
SQL> SELECT
2 FROM
3 WHERE
I-63

ename
emp
ename LIKE 'S%';

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the LIKE Operator


You can combine pattern matching
characters.
SQL> SELECT
2 FROM
3 WHERE

ename
emp
ename LIKE '_A%';

ENAME
---------JAMES
WARD

You can use the ESCAPE identifier to


search for % or _.
I-64

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the IS NULL Operator


Test for null values with the IS NULL
operator
SQL> SELECT
2 FROM
3 WHERE

ename, mgr
emp
mgr IS NULL;

ENAME
MGR
---------- --------KING

I-65

Copyright

Oracle Corporation, 1998. All rights reserved.

Logical Operators

I-66

Operator

Meaning

AND

Returns TRUE if both component


conditions are TRUE

OR

Returns TRUE if either component


condition is TRUE

NOT

Returns TRUE if the following


condition is FALSE

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the AND Operator


AND requires both conditions to be TRUE.
SQL>
2
3
4

SELECT
FROM
WHERE
AND

EMPNO
--------7876
7934

I-67

empno, ename, job, sal


emp
sal>=1100
job='CLERK';

ENAME
---------ADAMS
MILLER

Copyright

JOB
SAL
--------- --------CLERK
1100
CLERK
1300

Oracle Corporation, 1998. All rights reserved.

Using the OR Operator


OR requires either condition to be TRUE.
SQL>
2
3
4

SELECT
FROM
WHERE
OR

empno, ename, job, sal


emp
sal>=1100
job='CLERK';

EMPNO ENAME
JOB
SAL
--------- ---------- --------- --------7839
7698
7782
7566
7654

KING
BLAKE
CLARK
JONES
MARTIN

PRESIDENT
MANAGER
MANAGER
MANAGER
SALESMAN

5000
2850
2450
2975
1250

...
14 rows selected.
I-68

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the NOT Operator


SQL> SELECT ename, job
2 FROM
emp
3 WHERE job NOT IN ('CLERK','MANAGER','ANALYST');

ENAME
---------KING
MARTIN
ALLEN
TURNER
WARD

I-69

JOB
--------PRESIDENT
SALESMAN
SALESMAN
SALESMAN
SALESMAN

Copyright

Oracle Corporation, 1998. All rights reserved.

Rules of Precedence
Order Evaluated
1
2
3
4

Operator
All comparison
operators
NOT
AND
OR

Override rules of precedence by using


parentheses.
I-70

Copyright

Oracle Corporation, 1998. All rights reserved.

Rules of Precedence
SQL>
2
3
4
5

SELECT
FROM
WHERE
OR
AND

ENAME
ENAME
------------------KING
KING
MARTIN
MARTIN
ALLEN
ALLEN
TURNER
TURNER
WARD
WARD

I-71

ename, job, sal


emp
job='SALESMAN'
job='PRESIDENT'
sal>1500;

JOB
SAL
JOB
SAL
----------------- ----------------PRESIDENT
5000
PRESIDENT
5000
SALESMAN
1250
SALESMAN
1250
SALESMAN
1600
SALESMAN
1600
SALESMAN
1500
SALESMAN
1500
SALESMAN
1250
SALESMAN
1250

Copyright

Oracle Corporation, 1998. All rights reserved.

Rules of Precedence
Use parentheses to force priority.
SQL>
2
3
4
5

SELECT
FROM
WHERE
OR
AND

ENAME
ENAME
------------------KING
KING
ALLEN
ALLEN

I-72

ename, job, sal


emp
(job='SALESMAN'
job='PRESIDENT')
sal>1500;

JOB
SAL
JOB
SAL
----------------- ----------------PRESIDENT
5000
PRESIDENT
5000
SALESMAN
1600
SALESMAN
1600

Copyright

Oracle Corporation, 1998. All rights reserved.

ORDER BY Clause
Sort rows with the ORDER BY clause
ASC: ascending order, default
DESC: descending order
The ORDER BY clause comes last in the
SELECT statement.
SQL> SELECT
ename, job, deptno, hiredate
2 FROM
emp
3 ORDER BY hiredate;
ENAME
JOB
DEPTNO HIREDATE
---------- --------- --------- --------SMITH
CLERK
20 17-DEC-80
ALLEN
SALESMAN
30 20-FEB-81
...
14 rows selected.
I-73

Copyright

Oracle Corporation, 1998. All rights reserved.

Sorting in Descending Order


SQL> SELECT
ename, job, deptno, hiredate
2 FROM
emp
3 ORDER BY hiredate DESC;
ENAME
JOB
DEPTNO HIREDATE
---------- --------- --------- --------ADAMS
CLERK
20 12-JAN-83
SCOTT
ANALYST
20 09-DEC-82
MILLER
CLERK
10 23-JAN-82
JAMES
CLERK
30 03-DEC-81
FORD
ANALYST
20 03-DEC-81
KING
PRESIDENT
10 17-NOV-81
MARTIN
SALESMAN
30 28-SEP-81
...
14 rows selected.
I-74

Copyright

Oracle Corporation, 1998. All rights reserved.

Sorting by Column Alias


SQL> SELECT
empno, ename, sal*12 annsal
2 FROM
emp
3 ORDER BY annsal;
EMPNO ENAME
ANNSAL
--------- ---------- --------7369 SMITH
9600
7900 JAMES
11400
7876 ADAMS
13200
7654 MARTIN
15000
7521 WARD
15000
7934 MILLER
15600
7844 TURNER
18000
...
14 rows selected.
I-75

Copyright

Oracle Corporation, 1998. All rights reserved.

Sorting by Multiple Columns


The order of ORDER BY list is the order of
sort.
SQL> SELECT ename, deptno, sal
2 FROM
emp
3 ORDER BY deptno, sal DESC;
ENAME
DEPTNO
SAL
---------- --------- --------KING
10
5000
CLARK
10
2450
MILLER
10
1300
FORD
20
3000
...
14 rows selected.

You can sort by a column that is not in the


SELECT list.
I-76
Copyright

Oracle Corporation, 1998. All rights reserved.

Summary
SELECT
FROM
[WHERE
[ORDER BY

I-77

[DISTINCT] {*, column [alias], ...}


table
condition(s)]
{column, expr, alias} [ASC|DESC]];

Copyright

Oracle Corporation, 1998. All rights reserved.

Practice Overview
Selecting data and changing the order of
rows displayed
Restricting rows by using the WHERE
clause
Using the double-quotation-marks in
column aliases

I-78

Copyright

Oracle Corporation, 1998. All rights reserved.

You might also like