SQL_intro
SQL_intro
Introduction
INSERT
UPDATE Data manipulation language (DML)
DELETE
CREATE
ALTER
DROP Data definition language (DDL)
RENAME
TRUNCATE
COMMIT
ROLLBACK Transaction control
SAVEPOINT
After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• List
List the
the capabilities
capabilities of
of SQL
SQL SELECT
SELECT
statements
statements
•• Execute
Execute aa basic
basic SELECT
SELECT statement
statement
Table 1 Table 1
Join
Table 1 Table 2
I-5 Copyright سOracle Corporation, 1998. All rights reserved.
Basic SELECT Statement
SELECT
SELECT [DISTINCT]
[DISTINCT] {*,
{*, column
column [alias],...}
[alias],...}
FROM
FROM table;
table;
•• SELECT
SELECT identifies
identifies what
what columns
columns
•• FROM
FROM identifies
identifies which
which table
table
•• SQL
SQL statements
statements are
are not
not case
case sensitive.
sensitive.
•• SQL
SQL statements
statements can
can bebe on
on one
one or
or
more
more lines.
lines.
•• Keywords
Keywords cannot
cannot be
be abbreviated
abbreviated oror split
split
across
across lines.
lines.
•• Clauses
Clauses are
are usually
usually placed
placed on
on
separate
separate lines.
lines.
•• Tabs
Tabs and
and indents
indents are
are used
used to
to enhance
enhance
readability.
readability.
I-7 Copyright سOracle Corporation, 1998. All rights reserved.
Selecting All Columns
SQL> SELECT *
2 FROM dept;
DEPTNO LOC
--------- -------------
10 NEW YORK
20 DALLAS
30 CHICAGO
40 BOSTON
NAME 12*SAL+COMM
---------- -----------
KING
•• Renames
Renames aa column
column heading
heading
•• Is
Is useful
useful with
with calculations
calculations
•• Immediately
Immediately follows
follows column
column name;
name;
optional
optional AS
AS keyword
keyword between
between column
column
name
name and
and alias
alias
•• Requires
Requires double
double quotation
quotation marks
marks if
if it
it
contains
contains spaces
spaces or or special
special characters
characters
or
or is
is case
case sensitive
sensitive
NAME SALARY
------------- ---------
...
DEPTNO
---------
10
30
10
20
...
14 rows selected.
DEPTNO
---------
10
20
30
After
After completing
completing thisthis lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Limit
Limit the
the rows
rows retrieved
retrieved by
by aa query
query
•• Sort
Sort the
the rows
rows retrieved
retrieved by
by aa query
query
EMP
EMPNO ENAME JOB ... DEPTNO
•• The
The WHERE
WHERE clause
clause follows
follows the
the FROM
FROM
clause.
clause.
SQL>
SQL> SELECT
SELECT ename,
ename, job,
job, deptno
deptno
22 FROM
FROM emp
emp
33 WHERE
WHERE ename
ename == 'JAMES';
'JAMES';
Operator Meaning
= Equal to
Operator Meaning
ENAME SAL
---------- --------- Lower Higher
MARTIN 1250 limit limit
TURNER 1500
WARD 1250
ADAMS 1100
MILLER 1300
•• You
You can
can combine
combine pattern
pattern matching
matching
characters.
characters.
SQL> SELECT ename
2 FROM emp
3 WHERE ename LIKE '_A%';
ENAME
----------
JAMES
WARD
•• You
You can
can use
use the
the ESCAPE
ESCAPE identifier
identifier to
to
search
search for
for "%"
"%" or
or "_".
"_".
I-31 Copyright سOracle Corporation, 1998. All rights reserved.
Using the IS NULL Operator
Test
Test for
for null
null values
values with
with the
the IS
IS NULL
NULL
operator
operator
ENAME MGR
---------- ---------
KING
Operator Meaning
ENAME JOB
---------- ---------
KING PRESIDENT
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
WARD SALESMAN
Override
Override rules
rules of
of precedence
precedence by
by using
using
parentheses.
parentheses.
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
ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
KING
KING PRESIDENT
PRESIDENT 5000
5000
ALLEN
ALLEN SALESMAN
SALESMAN 1600
1600