Ch 1 Basic SQL Statement
Ch 1 Basic SQL Statement
SQL Statements
Objectives
• After completing this lesson, you
should be able to do the following:
–– List
List the
the capabilities
capabilities of
of SQL
SQL SELECT
SELECT
statements
statements
–– Execute
Execute aa basic
basic SELECT
SELECT statement
statement
Capabilities of SQL SELECT
Selection
Statements
Projection
Table 1 Table 1
Join
Table 1 Table 2
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.
Writing SQL Statements
–– 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 or
or 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.
Selecting All Columns
SQL> SELECT *
2 FROM dept;
DEPTNO LOC
--------- -------------
10 NEW YORK
20 DALLAS
30 CHICAGO
40 BOSTON
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
Arithmetic Expressions
• Create expressions on NUMBER and
DATE data by using arithmetic
operators.
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
Using Arithmetic Operators
SQL> SELECT ename, sal, sal+300
2 FROM emp;
ENAME 12*SAL+COMM
---------- -----------
KING
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;
optional
optional AS
AS keyword
keyword between
between column
column
name
name and
and alias
alias
–– Requires
Requires double
double quotation
quotation marks
marks ifif itit
contains
contains spaces
spaces or or special
special characters
characters or or
is
is case
case sensitive
sensitive
Using Column Aliases
SQL> SELECT ename AS name, sal salary
2 FROM emp;
NAME SALARY
------------- ---------
...
Employees
-------------------
KINGPRESIDENT
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER
MARTINSALESMAN
ALLENSALESMAN
...
14 rows selected.
Literal Character Strings
–– A
A literal
literal is
is aa character,
character, expression,
expression, oror
number
number included
included inin the
the SELECT
SELECT list.
list.
–– Date
Date and
and character
character literal
literal values
values must
must
be
be enclosed
enclosed within
within single
single quotation
quotation
marks.
marks.
–– Each
Each character
character string
string isis output
output once
once forfor
each
each row
row returned.
returned.
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.
Duplicate Rows
• The default display of queries is all
rows, including duplicate rows.
SQL>
SQL> SELECT
SELECT deptno
deptno
22 FROM
FROM emp;
emp;
DEPTNO
---------
10
30
10
20
...
14 rows selected.
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
Displaying Table Structure
• Use the SQL*Plus DESCRIBE
command to display the structure of a
table.
DESC[RIBE]
DESC[RIBE] tablename
tablename
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)
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
specifying
specifying column
column names
names
–– Using
Using SQL*Plus
SQL*Plus editor
editor