You are on page 1of 34

1

Writing Basic
SQL Statements

Copyright  Oracle Corporation, 1998. All rights reserved.


Objectives

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

1-2 Copyright  Oracle Corporation, 1998. All rights reserved.


Capabilities of SQL SELECT Stat
ements
Selection Projection

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.

1-4 Copyright  Oracle Corporation, 1998. All rights reserved.


Writing SQL Statements

•• 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 DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

1-6 Copyright  Oracle Corporation, 1998. All rights reserved.


Selecting Specific Columns

SQL> SELECT deptno, loc


2 FROM dept;

DEPTNO LOC
--------- -------------
10 NEW YORK
20 DALLAS
30 CHICAGO
40 BOSTON

1-7 Copyright  Oracle Corporation, 1998. All rights reserved.


Column Heading Defaults

•• 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

1-8 Copyright  Oracle Corporation, 1998. All rights reserved.


Arithmetic Expressions
Create
Create expressions
expressions on
on NUMBER
NUMBER and
and DATE
DATE d
d
ata
ata by
by using
using arithmetic
arithmetic operators.
operators.

Operator Description

+ Add

- Subtract

* Multiply

/ Divide

1-9 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.

1-10 Copyright  Oracle Corporation, 1998. All rights reserved.


Operator Precedence
_
// ++ _
**
•• Multiplication
Multiplication and
and division
division take
take priority
priority
over
over addition
addition and
and subtraction.
subtraction.
•• Operators
Operators ofof the
the same
same priority
priority are
are evalu
evalu
ated
ated from
from left
left to
to right.
right.
•• Parentheses
Parentheses areare used
used to
to force
force prioritize
prioritize
d
d evaluation
evaluation and
and to
to clarify
clarify statements.
statements.

1-11 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.

1-12 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.

1-13 Copyright  Oracle Corporation, 1998. All rights reserved.


Defining a Null Value
•• A
A null
null is
is aa value
value that
that is
is unavailable,
unavailable, una
una
ssigned,
ssigned, unknown,
unknown, oror inapplicable.
inapplicable.
•• A
A null
null is
is not
not the
the same
same asas zero
zero or
or aa blank
blank
space.
space.
SQL> SELECT ename, job, comm
2 FROM emp;

ENAME JOB COMM


---------- --------- ---------
KING PRESIDENT
BLAKE MANAGER
...
TURNER SALESMAN 0
...
14 rows selected.
1-14 Copyright  Oracle Corporation, 1998. All rights reserved.
Null Values
in Arithmetic Expressions
Arithmetic
Arithmetic expressions
expressions containing
containing aa null
null vv
alue
alue evaluate
evaluate to
to null.
null.

SQL> select ename, 12*sal+comm


2 from emp
3 WHERE ename='KING';

ENAME 12*SAL+COMM
---------- -----------
KING

1-15 Copyright  Oracle Corporation, 1998. All rights reserved.


Defining a Column Alias

•• 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

1-16 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


------------- -------------
...
1-17 Copyright  Oracle Corporation, 1998. All rights reserved.
Concatenation Operator

•• 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

1-18 Copyright  Oracle Corporation, 1998. All rights reserved.


Using the Concatenation Operat
or
SQL> SELECT ename||job AS "Employees"
2 FROM emp;

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

1-19 Copyright  Oracle Corporation, 1998. All rights reserved.


Literal Character Strings

•• 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.

1-20 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.

1-21 Copyright  Oracle Corporation, 1998. All rights reserved.


Duplicate Rows
The
The default
default display
display of
of queries
queries is
is all
all rows,
rows, ii
ncluding
ncluding duplicate
duplicate rows.
rows.
SQL>
SQL> SELECT
SELECT deptno
deptno
22 FROM
FROM emp;
emp;

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

1-22 Copyright  Oracle Corporation, 1998. All rights reserved.


Eliminating Duplicate Rows
Eliminate duplicate rows by using the DIS
TINCT keyword in the SELECT clause.
SQL> SELECT DISTINCT deptno
2 FROM emp;

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

1-23 Copyright  Oracle Corporation, 1998. All rights reserved.


SQL and SQL*Plus Interaction
SQL Statements SQL Statements
Buffer

Server
SQL*Plus

SQL*Plus Query Results


Commands
Formatted Report

1-24 Copyright  Oracle Corporation, 1998. All rights reserved.


SQL Statements Versus SQL*Pl
us Commands
SQL SQL*Plus
• A language • An environment
• ANSI standard • Oracle proprietary
• Keyword cannot be ab • Keywords can be abbr
breviated eviated
• Statements manipulate • Commands do not allo
data and table definitio w manipulation of valu
ns in the database es in the database

SQL SQL SQL*Plus SQL*Plus


statements buffer commands buffer

1-25 Copyright  Oracle Corporation, 1998. All rights reserved.


Overview of SQL*Plus
•• Log
Log in
in to
to SQL*Plus.
SQL*Plus.
•• Describe
Describe the
the table
table structure.
structure.
•• Edit
Edit your
your SQL
SQL statement.
statement.
•• Execute
Execute SQL
SQL from
from SQL*Plus.
SQL*Plus.
•• Save
Save SQL
SQL statements
statements to to files
files and
and appen
appen
d
d SQL
SQL statements
statements toto files.
files.
•• Execute
Execute saved
saved files.
files.
•• Load
Load commands
commands from
from file
file to
to buffer
buffer
to
to edit.
edit.
1-26 Copyright  Oracle Corporation, 1998. All rights reserved.
Logging In to SQL*Plus
• From Windows environment:

• From command line:


sqlplus [username[/password
[@database]]]
1-27 Copyright  Oracle Corporation, 1998. All rights reserved.
Displaying Table Structure

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

1-28 Copyright  Oracle Corporation, 1998. All rights reserved.


Displaying Table Structure

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)

1-29 Copyright  Oracle Corporation, 1998. All rights reserved.


SQL*Plus Editing Commands

•• 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

1-30 Copyright  Oracle Corporation, 1998. All rights reserved.


SQL*Plus Editing Commands
•• I[NPUT]
I[NPUT]
•• I[NPUT]
I[NPUT] text
text
•• L[IST]
L[IST]
•• L[IST]
L[IST] n
n
•• L[IST]
L[IST] m
mnn
•• R[UN]
R[UN]
•• n
n
•• n
n text
text
•• 00 text
text
1-31 Copyright  Oracle Corporation, 1998. All rights reserved.
SQL*Plus File Commands

•• SAVE
SAVE filename
filename
•• GET
GET filename
filename
•• START
START filename
filename
•• @
@ filename
filename
•• EDIT
EDIT filename
filename
•• SPOOL
SPOOL filename
filename

1-32 Copyright  Oracle Corporation, 1998. All rights reserved.


Summary

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

1-33 Copyright  Oracle Corporation, 1998. All rights reserved.


Practice Overview

•• 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

1-34 Copyright  Oracle Corporation, 1998. All rights reserved.

You might also like