0% found this document useful (0 votes)
1 views

SQL_intro

The document provides an overview of SQL statements, including data retrieval, manipulation, and definition commands. It outlines the capabilities of SQL SELECT statements, basic syntax, and examples of selecting data from tables. Additionally, it discusses the use of column aliases, handling null values, and eliminating duplicate rows.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

SQL_intro

The document provides an overview of SQL statements, including data retrieval, manipulation, and definition commands. It outlines the capabilities of SQL SELECT statements, basic syntax, and examples of selecting data from tables. Additionally, it discusses the use of column aliases, handling null values, and eliminating duplicate rows.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 44

I

Introduction

Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


SQL Statements
SELECT Data retrieval

INSERT
UPDATE Data manipulation language (DML)
DELETE

CREATE
ALTER
DROP Data definition language (DDL)
RENAME
TRUNCATE

COMMIT
ROLLBACK Transaction control
SAVEPOINT

GRANT Data control language (DCL)


REVOKE
I-2 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.
Tables Used in the Course
EMP
EMPNO
EMPNO ENAME
ENAME JOB
JOB MGR
MGR HIREDATE
HIREDATE SAL
SAL COMM
COMM DEPTNO
DEPTNO
---------
--------- ----------
---------- ---------
--------- ---------
--------- ---------
--------- ---------
--------- ---------
--------- ---------
---------
7839
7839 KING
KING PRESIDENT
PRESIDENT 17-NOV-81
17-NOV-81 5000
5000 10
10
7698
7698 BLAKE
BLAKE MANAGER
MANAGER 7839
7839 01-MAY-81
01-MAY-81 2850
2850 30
30
7782
7782 CLARK
CLARK MANAGER
MANAGER 7839
7839 09-JUN-81
09-JUN-81 1500
1500 10
10
7566
7566 JONES
JONES MANAGER
MANAGER 7839
7839 02-APR-81
02-APR-81 2975
2975 20
20
7654
7654 MARTIN
MARTIN SALESMAN
SALESMAN 7698
7698 28-SEP-81
28-SEP-81 1250
1250 1400
1400 30
30
7499
7499 ALLEN
ALLEN SALESMAN
SALESMAN 7698
7698 20-FEB-81
20-FEB-81 1600
1600 300
300 30
30
7844
7844 TURNER
TURNER SALESMAN
SALESMAN 7698
7698 08-SEP-81
08-SEP-81 1500
1500 00 30
30
DEPT 7900
7900 JAMES
JAMES CLERK
CLERK 7698
7698 03-DEC-81
03-DEC-81 950
950 30
30
7521
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
DEPTNO DNAMEWARD
DEPTNO DNAME SALESMAN
LOC
LOC 7698 22-FEB-81 1250 500 30
--------- 7902
7902 FORD
FORD
--------- --------------
--------------
ANALYST
ANALYST
----------
----------
7566
7566 03-DEC-81
03-DEC-81 SALGRADE
3000
3000 20
20
7369 SMITH
7369 10
SMITH CLERK
CLERK NEW
ACCOUNTING 7902
7902 17-DEC-80
17-DEC-80 800
800
GRADE LOSAL 20
20
HISAL
10 ACCOUNTING NEW GRADE LOSAL HISAL
YORK 7788 SCOTT
7788 SCOTT ANALYST
ANALYST 7566
7566 09-DEC-82 3000
09-DEC-82 ---------
3000 --------- 20
20
YORK --------- --------- ---------
---------
20 7876
7876 ADAMS
ADAMS CLERK
CLERK 7788
7788 12-JAN-83
12-JAN-83 1100
1100 11 20
20
20 RESEARCH
RESEARCH DALLAS
DALLAS 700
700 1200
1200
7934
7934 MILLER CLERK 7782 23-JAN-82 1300 10
30 SALESMILLER
30 SALES CLERK
CHICAGO
CHICAGO 7782 23-JAN-82 1300 22 1201
1201 10
1400
1400
40
40 OPERATIONS
OPERATIONS BOSTON
BOSTON 33 1401
1401 2000
2000
44 2001
2001 3000
3000
55 3001
3001 9999
9999

I-3 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Objectives

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

I-4 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Capabilities of SQL SELECT
Statements
Selection Projection

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

I-6 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 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 DNAME LOC


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

I-8 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

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

I-10 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-11 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-12 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,
unassigned,
unassigned, 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.
I-13 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.
Null Values in Arithmetic
Expressions
Arithmetic
Arithmetic expressions
expressions containing
containing aa null
null
value
value evaluate
evaluate to
to null.
null.

SQL> select ename NAME, 12*sal+comm


2 from emp
3 WHERE ename='KING';

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

I-14 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;
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

I-15 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-16 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,
including
including duplicate
duplicate rows.
rows.
SQL>
SQL> SELECT
SELECT deptno
deptno
22 FROM
FROM emp;
emp;

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

I-17 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-18 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


I
Restricting and Sorting Data

Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Objectives

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

I-20 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Limiting Rows Using a Selection
EMP
EMPNO ENAME JOB ... DEPTNO
"…retrieve all
7839 KING PRESIDENT 10 employees
7698 BLAKE MANAGER 30 in department 10"
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...

EMP
EMPNO ENAME JOB ... DEPTNO

7839 KING PRESIDENT 10


7782 CLARK MANAGER 10
7934 MILLER CLERK 10

I-21 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Limiting Rows Selected
•• Restrict
Restrict the
the rows
rows returned
returned by
by using
using the
the
WHERE
WHERE clause.
clause.
SELECT [DISTINCT] {*, column [alias], ...}
FROM table
[WHERE condition(s)];

•• The
The WHERE
WHERE clause
clause follows
follows the
the FROM
FROM
clause.
clause.

I-22 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Using the WHERE Clause

SQL> SELECT ename, job, deptno


2 FROM emp
3 WHERE job='CLERK';

ENAME JOB DEPTNO


---------- --------- ---------
JAMES CLERK 30
SMITH CLERK 20
ADAMS CLERK 20
MILLER CLERK 10

I-23 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Character Strings and Dates
•• Character
Character strings
strings and
and date
date values
values are
are
enclosed
enclosed in
in single
single quotation
quotation marks
marks
•• Character
Character values
values are
are case-sensitive
case-sensitive and
and
date
date values
values are
are format-sensitive
format-sensitive
•• Default
Default date
date format
format is
is 'DD-MON-YY'
'DD-MON-YY'

SQL>
SQL> SELECT
SELECT ename,
ename, job,
job, deptno
deptno
22 FROM
FROM emp
emp
33 WHERE
WHERE ename
ename == 'JAMES';
'JAMES';

I-24 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Comparison Operators

Operator Meaning

= Equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

<> Not equal to

I-25 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-26 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Other Comparison Operators

Operator Meaning

BETWEEN Between two values (inclusive)


...AND...

IN(list) Match any of a list of values

LIKE Match a character pattern

IS NULL Is a null value

I-27 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Using the BETWEEN Operator
Use
Use the
the BETWEEN
BETWEEN operator
operator to
to display
display
rows
rows based
based on
on aa range
range of
of values.
values.
SQL> SELECT ename, sal
2 FROM emp
3 WHERE sal BETWEEN 1000 AND 1500;

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

I-28 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Using the IN Operator
Use
Use the
the IN
IN operator
operator to
to test
test for
for values
values in
in aa
list.
list.

SQL> SELECT empno, ename, sal, mgr


2 FROM emp
3 WHERE mgr IN (7902, 7566, 7788);

EMPNO ENAME SAL MGR


--------- ---------- --------- ---------
7902 FORD 3000 7566
7369 SMITH 800 7902
7788 SCOTT 3000 7566
7876 ADAMS 1100 7788

I-29 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Using the LIKE Operator
•• Use
Use the
the LIKE
LIKE operator
operator to
to perform
perform
wildcard
wildcard searches
searches of
of valid
valid search
search string
string
values.
values.
•• Search
Search conditions
conditions can
can contain
contain either
either
literal
literal characters
characters or
or numbers.
numbers.
–– %
% denotes
denotes zero
zero or
or many
many characters
characters
–– __ denotes
denotes one
one character
character
SQL> SELECT ename
2 FROM emp
3 WHERE ename LIKE 'S%';

I-30 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Using the LIKE Operator

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

SQL> SELECT ename, mgr


2 FROM emp
3 WHERE mgr IS NULL;

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

I-32 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Logical Operators

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

I-33 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Using the AND Operator
AND requires both conditions to be TRUE.

SQL> SELECT empno, ename, job, sal


2 FROM emp
3 WHERE sal>=1100
4 AND job='CLERK';

EMPNO ENAME JOB SAL


--------- ---------- --------- ---------
7876 ADAMS CLERK 1100
7934 MILLER CLERK 1300

I-34 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Using the OR Operator
OR
OR requires
requires either
either condition
condition to
to be
be TRUE.
TRUE.
SQL> SELECT empno, ename, job, sal
2 FROM emp
3 WHERE sal>=1100
4 OR job='CLERK';

EMPNO ENAME JOB SAL


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

7839 KING PRESIDENT 5000


7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7566 JONES MANAGER 2975
7654 MARTIN SALESMAN 1250
...
14 rows selected.
I-35 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 JOB
---------- ---------
KING PRESIDENT
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
WARD SALESMAN

I-36 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Rules of Precedence

Order Evaluated Operator


1 All comparison
operators
2 NOT
3 AND
4 OR

Override
Override rules
rules of
of precedence
precedence by
by using
using
parentheses.
parentheses.

I-37 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Rules of Precedence
SQL> SELECT ename, job, sal
2 FROM emp
3 WHERE job='SALESMAN'
4 OR job='PRESIDENT'
5 AND sal>1500;

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

I-38 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Rules of Precedence
Use
Use parentheses
parentheses to
to force
force priority.
priority.
SQL> SELECT ename, job, sal
2 FROM emp
3 WHERE (job='SALESMAN'
4 OR job='PRESIDENT')
5 AND sal>1500;

ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
KING
KING PRESIDENT
PRESIDENT 5000
5000
ALLEN
ALLEN SALESMAN
SALESMAN 1600
1600

I-39 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


ORDER BY Clause
•• Sort
Sort rows
rows with
with the
the ORDER
ORDER BYBY clause
clause
–– ASC:
ASC: ascending
ascending order,
order, default
default
–– DESC:
DESC: descending
descending order
order
•• The
The ORDER
ORDER BY BY clause
clause comes
comes last
last in
in the
the
SELECT
SELECT statement.
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-40 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-41 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-42 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.


Sorting by Multiple Columns
•• The
The order
order of
of ORDER
ORDER BY
BY list
list is
is the
the order
order of
of
sort.
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
You can
can sort
sort by by aa column
column that that is is not
not in
in the
the
SELECT
SELECT list.
list.
I-43 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.
Summary

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


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

I-44 Copyright ‫ س‬Oracle Corporation, 1998. All rights reserved.

You might also like