Professional Documents
Culture Documents
Les 01
Les 01
Writing Basic
SQL Statements
After
After completing
completing this
this lesson,
lesson, you
you should
should b
b
ee able
able to
to do
do the
the following:
following:
•• List
List the
the capabilities
capabilities of
of SQL
SQL SELECT
SELECT stat
stat
ements
ements
•• Execute
Execute aa basic
basic SELECT
SELECT statement
statement
•• Differentiate
Differentiate between
between SQL
SQL statements
statements aa
nd
nd SQL*Plus
SQL*Plus commands
commands
Table 1 Table 1
Join
Table 1 Table 2
1-3 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 be
be 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 areare usually
usually placed
placed on
on separate
separate ll
ines.
ines.
•• Tabs
Tabs and
and indents
indents are
are used
used to
to enhance
enhance rr
eadability.
eadability.
1-5 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
•• Default
Default justification
justification
–– Left:
Left: Date
Date and
and character
character data
data
–– Right:
Right: Numeric
Numeric data
data
•• Default
Default display:
display: Uppercase
Uppercase
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
ENAME 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; optio
optio
nal
nal AS
AS keyword
keyword between
between column
column name
name aa
nd
nd alias
alias
•• Requires
Requires double
double quotation
quotation marks
marks if
if it
it co
co
ntains
ntains spaces
spaces oror special
special characters
characters oror ii
ss case
case sensitive
sensitive
NAME SALARY
------------- ---------
...
•• Concatenates
Concatenates columns
columns or
or character
character stri
stri
ngs
ngs to
to other
other columns
columns
•• Is
Is represented
represented byby two
two vertical
vertical bars
bars (||)
(||)
•• Creates
Creates aa resultant
resultant column
column that
that is
is aa cha
cha
racter
racter expression
expression
Employees
-------------------
KINGPRESIDENT
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER
MARTINSALESMAN
ALLENSALESMAN
...
14 rows selected.
•• AA literal
literal is
is aa character,
character, expression,
expression, or or n
n
umber
umber included
included in in the
the SELECT
SELECT list.
list.
•• Date
Date and
and character
character literal
literal values
values must
must b b
ee enclosed
enclosed within
within single
single quotation
quotation mark
mark
s.
s.
•• Each
Each character
character string
string isis output
output once
once forfor
each
each row
row returned.
returned.
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.
DEPTNO
---------
10
30
10
20
...
14 rows selected.
DEPTNO
---------
10
20
30
Server
SQL*Plus
Use
Use the
the SQL*Plus
SQL*Plus DESCRIBE
DESCRIBE command
command to
to
display
display the
the structure
structure of
of aa table.
table.
DESC[RIBE]
DESC[RIBE] tablename
tablename
SQL>
SQL> DESCRIBE
DESCRIBE dept
dept
Name
Name Null?
Null? Type
Type
-----------------
----------------- --------
-------- ------------
------------
DEPTNO
DEPTNO NOT
NOT NULL
NULL NUMBER(2)
NUMBER(2)
DNAME
DNAME VARCHAR2(14)
VARCHAR2(14)
LOC
LOC VARCHAR2(13)
VARCHAR2(13)
•• A[PPEND]
A[PPEND] text
text
•• C[HANGE]
C[HANGE] // old
old // new
new
•• C[HANGE]
C[HANGE] // text
text //
•• CL[EAR]
CL[EAR] BUFF[ER]
BUFF[ER]
•• DEL
DEL
•• DEL
DEL n
n
•• DEL
DEL m
mnn
•• SAVE
SAVE filename
filename
•• GET
GET filename
filename
•• START
START filename
filename
•• @
@ filename
filename
•• EDIT
EDIT filename
filename
•• SPOOL
SPOOL filename
filename
SELECT
SELECT [DISTINCT]
[DISTINCT] {*,column
{*,column [alias],...}
[alias],...}
FROM
FROM table;
table;
Use
Use SQL*Plus
SQL*Plus as
as an
an environment
environment to:
to:
•• Execute
Execute SQL
SQL statements
statements
•• Edit
Edit SQL
SQL statements
statements
•• Selecting
Selecting all
all data
data from
from different
different tables
tables
•• Describing
Describing the
the structure
structure of
of tables
tables
•• Performing
Performing arithmetic
arithmetic calculations
calculations and
and sp
sp
ecifying
ecifying column
column names
names
•• Using
Using SQL*Plus
SQL*Plus editor
editor