Sie sind auf Seite 1von 133

Structured Query

Language
Part I
AC 505

SQL: Outline
Introduction to SQL
Queries in SQL
Update Statements in SQL
Data Definition in SQL
Views in SQL
Embedded SQL
2

Introduction to SQL:
Outline
What is SQL?
Objectives of SQL
History of SQL
Components of SQL
Writing SQL Commands
3

Introduction to SQL:
Outline
What is SQL?
Objectives of SQL
History of SQL
Components of SQL
Writing SQL Commands
4

What is SQL?
Structured Query Language
Transform-oriented language
Non-procedural language
Essentially free-format
Standard English

Introduction to SQL:
Outline
What is SQL?
Objectives of SQL
History of SQL
Components of SQL
Writing SQL Commands
6

Objectives of SQL
To allow a user to create the database
and relation structures.
To allow a user to perform basic data
management tasks.
To allow a user to perform both simple
and complex queries to transform raw
data into information.
7

Objectives of SQL
To perform data management tasks
with minimal effort such that the
command structure and syntax must be
easy to learn.
To be portable.

Introduction to SQL:
Outline
What is SQL?
Objectives of SQL
History of SQL
Components of SQL
Writing SQL Commands
9

SQL History
Originally Structured English Query
Language or SEQUEL, developed by IBM
around 1975.
In 1986, Official American National
Standardization Institute (ANSI) defined an
SQL standard
In 1987, International Organization for
Standardization (ISO) adopted the
standard - informally known as SQL/86.
10

SQL History
In 1989, and again in 1992 , ISO
produced SQL/92 or SQL/2, officially
named International Standard
Database Language SQL (1992)
By 1992, over 100 DBMS products
supported SQL features.

11

Introduction to SQL:
Outline
What is SQL?
Objectives of SQL
History of SQL
Components of SQL
Writing SQL Commands
12

SQL Components
Data Definition Language (DDL)
Data Manipulation Language (DML)
Defining and Maintaining Integrity
Maintaining Security

13

Introduction to SQL:
Outline
What is SQL?
Objectives of SQL
History of SQL
Components of SQL
Writing SQL Commands
14

SQL Notation
An SQL statement consists of:
reserved words
user-defined words

A terminator (usually the semicolon ;)


is used to mark the end of an SQL
statement.
Most components are case
insensitive except for literal
character data.
15

Readable SQL
Each clause should begin on a new line.
The beginning of a clause should line up
with the beginning of other clauses.
If a clause has several parts, they
should each appear on a separate line
and be indented under the start of the
clause to show the relationship.
16

Backus Naur Form


(BNF)

Upper case letters are used for


representing reserved words and
they must be spelt correctly.
Lower case letters are used to
represent user-defined words.
A vertical bar (|) indicates a choice
among alternatives; for example a|b|c.
Curly braces { } indicate a required
element: for example, {a}.
17

Backus Naur Form


(BNF)

Square brackets [ ] indicate an


optional element: for example, [a].
An ellipsis () is used to indicate
optional repetition of an item
zero or more times. For example: {a|b}
[,c]
means either a or b followed by zero or
more repetitions of c separated by
commas.
18

Data Manipulation
SELECT
INSERT
UPDATE
DELETE

19

Data Definition
CREATE TABLE
ALTER TABLE
DROP TABLE

20

SQL: Outline
Introduction to SQL
Queries in SQL
Update Statements in SQL
Data Definition in SQL
Views in SQL
Embedded SQL
21

COMPANY DATABASE
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
DEPARTMENT(DNumber,DName,MgrENo)
DEPT_LOCATION(DNo, Location)
PROJECT(PNo, PName, PLocation,DNum)
WORKS_ON(ENo, PNo, Hours)
DEPENDENT(ENo, DependentName, Sex, Bdate,
Relationship)
POSITION(Position, Salary)
22

Queries: Outline
Simple or basic queries
Sorting results
Aggregate functions
Grouping results
Subqueries
Multi-table queries
Combining result tables
23

Queries: Outline
Simple or basic queries
Sorting results
Aggregate functions
Grouping results
Subqueries
Multi-table queries
Combining result tables
24

SELECT Statement:
Objective
To retrieve data from one or more
database tables.

25

SELECT Statement:
Format

SELECT [DISTINCT|ALL] { * |
[column_exp [AS new_name]
[,] ]}
FROM table_name [alias] [,]
[WHERE condition]
[GROUP BY column_list]
[HAVING condition]
[ORDER By column_list];

26

Sequence of
Processing a Select
Statement

FROM

WHERE
GROUP BY

Specifies the table or


tables to be used.
Filters the rows subject to
some condition.
Forms groups of rows with
the same column value.

27

Sequence of
Processing a Select
Statement
HAVING
SELECT

ORDER BY

Filters the groups subject


to some condition.
Specifies which columns
are to appear in the
output.
Specifies the order of the
output.
28

SELECT Statement: Format


SELECT [DISTINCT|ALL] { * |
[column_exp [AS new_name]

[,] }

FROM table_name [alias] [,]


[WHERE condition]
[GROUP BY column_list]
[HAVING condition]
[ORDER By column_list]
29

SELECT CLAUSE
SELECT [DISTINCT|ALL] { * |
[column_exp [AS new_name]
[,] }
Specifies which columns are to appear in
the output.
DISTINCT is used to eliminate duplicates.
Asterisk * expresses all columns.
column_exp represents a column name or
expression
30

FROM CLAUSE
FROM table_name [alias] [,]
Specifies the table or tables to be used.
table_name is the name of an existing
database table
alias is an optional abbreviation for
table_name
31

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
32

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
33

Retrieve all columns, all rows


Query#01: List full details of all
employees.

34

Retrieve all columns, all rows


EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo,
DNo)
SELECT *
FROM EMPLOYEE;

35

List full details of all employees


ENo

Name

Address

Sex

CivilStatus

BDate

Position

SuperENo

DNo

emp01

Juan dela Cruz

Manila

male

married

01/01/50

manager

emp02

Maria Clara

Pasay

female

single

01/02/70

secretary

emp01

dept01

emp03

Crisostomo Ibarra

Makati

male

single

01/03/60

assistant manager

emp01

dept01

emp04

Sisa

Quezon City

female

married

02/02/55

staff assistant 1

emp01

dept01

emp05

Elias

Quezon City

male

married

03/03/55

staff assistant 2

emp01

dept01

emp06

Basilio

Mandaluyong

male

single

04/04/55

staff assistant 1

emp01

dept01

emp07

Crispin

Caloocan

male

single

02/02/72

clerk 1

emp01

dept01

emp08

Tasio

Manila

male

divorced

07/02/55

clerk 2

emp01

dept01

emp09

Damaso

Bulacan

male

separated

08/02/62

clerk 3

emp01

dept01

emp10

Simon

San Juan

male

married

12/02/55

staff assistant 2

emp01

dept01

dept01

36

List full details of all employees


ENo

Name

Address

Sex

CivilStatus

BDate

Position

SuperENo

DNo

emp11

Jose Rizal

Manila

male

married

28/10/40

manager

emp12

Gabriela Silang

Pasay

female

single

01/02/70

secretary

emp11

dept02

emp13

Andres Bonifacio

Manila

male

divorced

01/04/63

assistant manager

emp11

dept02

emp14

Tandang Sora

Pasig

female

separated

02/05/58

staff assistant 1

emp11

dept02

emp15

Juan Luna

Manila

male

married

03/03/55

staff assistant 2

emp11

dept02

emp16

Antonio Luna

San Juan

male

single

04/04/55

staff assistant 1

emp11

dept02

emp17

Apolinario Mabini

Pasig

male

single

02/02/72

clerk 1

emp11

dept02

emp18

Emilio Jacinto

Manila

male

single

03/05/58

clerk 2

emp11

dept02

emp19

Ninoy Aquino

Manila

male

married

20/12/66

clerk 3

emp11

dept02

emp20

Gregorio del Pilar

Quezon City

male

married

11/12/55

staff assistant 2

emp11

dept02

emp21

Evelio Javier

Manila

male

single

30/09/62

clerk 3

emp11

dept02

dept02

37

List full details of all employees


ENo

Name

Address

Sex

CivilStatus

BDate

Position

SuperENo

DNo

emp31

Corazon Aquino

Manila

female

married

05/08/50

manager

emp32

Fidel Ramos

Pasay

male

single

15/08/72

secretary

emp31

dept03

emp33

Joseph Estrada

Manila

male

divorced

06/06/64

assistant manager

emp31

dept03

emp34

Ferdinand Marcos

Pasig

male

separated

28/05/59

staff assistant 1

emp31

dept03

emp35

Diosdado Macapagal

Manila

male

married

15/10/58

staff assistant 2

emp31

dept03

emp36

Ramon Magsaysay

San Juan

male

single

04/04/60

staff assistant 1

emp31

dept03

emp37

Elpidio Quirino

Pasig

male

single

02/07/75

clerk 1

emp31

dept03

emp38

Manuel Quezon

Manila

male

single

01/06/58

clerk 2

emp31

dept03

emp39

Manuel Roxas

Manila

male

married

20/03/68

clerk 3

emp31

dept03

emp40

Emilio Aguinaldo

Quezon City

male

separated

11/12/59

staff assistant 2

emp31

dept03

emp41

Carlos Garcia

Manila

male

single

30/01/64

clerk 3

emp31

dept03

emp42

Sergio Osmena

Manila

male

divorced

31/03/65

clerk 2

emp31

dept03

emp43

Jose Laurel

Manila

male

married

20/06/66

clerk 1

emp31

dept03

dept03

38

List full details of all employees


ENo

Name

Address

Sex

CivilStatus

BDate

Position

SuperENo

DNo

emp51

Ping Lacson

Manila

male

single

01/06/48

manager

emp52

Gloria Arroyo

Mandaluyong

female

single

15/07/62

secretary

emp51

dept04

emp53

Alfredo Lim

Pasig

male

divorced

06/08/63

assistant manager

emp51

dept04

emp54

Miriam Santiago

Pasig

male

separated

29/07/56

staff assistant 1

emp51

dept04

emp55

Lauren Legarda

Manila

female

married

18/10/61

staff assistant 2

emp51

dept04

emp56

Nikki Coseteng

Manila

female

single

04/04/64

staff assistant 1

emp51

dept04

emp57

Nur Misuari

Jolo

male

married

02/08/75

clerk 1

emp51

dept04

emp58

Commander Robot

Sulu

male

single

06/05/57

clerk 2

emp51

dept04

emp59

Joma Sison

Manila

male

married

25/03/70

clerk 3

emp51

dept04

emp60

Nora Aunor

San Juan

female

separated

15/12/62

staff assistant 2

emp51

dept04

emp61

Vilma Santos

Makati

female

single

31/01/68

clerk 3

emp51

dept04

emp62

Sharon Cuneta

Makati

female

divorced

19/04/63

clerk 2

emp51

dept04

emp63

Frank Drilon

Pasay

male

single

20/09/63

clerk 1

emp51

dept04

emp64

Manny Villar

Pasay

male

single

20/05/67

clerk 1

emp51

dept04

dept04

39

Retrieve all columns, all rows


Query#02: List full details of all
departments

40

Retrieve all columns, all rows


DEPARTMENT(DNumber,DName,MgrE
No)
SELECT DNumber, DName, MgrENo
FROM DEPARTMENT;

41

List full details of all


departments
DNumber

DName

MgrENo

dept01

Noli Me Tangere

emp01

dept02

Heroes

emp11

dept03

Presidents

emp31

dept04

News Makers

emp51
42

Retrieve all columns, all rows


Query#03: List full details of all
projects
PROJECT(PNo, PName, PLocation,
DNum)
SELECT PNo, PName, DNum,
PLocation
FROM PROJECT;
43

Full Details of All Projects


PNo
proj01
proj02
proj03
proj04
proj05
proj06
proj07
proj08
proj09
proj10
proj11
proj12
proj13
proj14
proj15

PName
Alpha
Beta
Gamma
Theta
Rose
Tulip
Carnation
Red
Blue
Green
Earth
Mars
Venus
Jupiter
Pluto

DNum
dept01
dept01
dept01
dept01
dept02
dept02
dept02
dept03
dept03
dept03
dept04
dept04
dept04
dept04
dept04

PLocation
USA
Europe
Africa
Asia
England
Holland
France
Manila
Davao
Cebu
Europe
Asia
USA
Australia
Latin America

44

Retrieve all columns, all rows


Query#04: List full details of all positions
POSITION(Position, Salary)
SELECT *
FROM POSITION;
45

List full details of all


positions
Position

Salary

assistant manager

70,000.00

clerk 1

10,000.00

clerk 2

15,000.00

clerk 3

20,000.00

clerk 4

25,000.00

manager

100,000.00

secretary

20,000.00

staff assistant 1

40,000.00

staff assistant 2

50,000.00

staff assistant 3

60,000.00

46

Retrieve all columns, all rows


Query#05: List full details of all employees
dependents
DEPENDENT(ENo, DependentName,
Sex, Bdate, Relationship)
SELECT *
FROM DEPENDENT;
47

Full details employees dependents


ENo
emp01
emp01
emp04
emp05
emp05
emp08
emp09
emp10
emp11
emp13
emp14
emp14
emp15
emp15
emp19

DependentName
Bernadette
Sebastian
Philip
James
Rose
Jude
Peter
John
James
Paul
Martin
Nicholas
Mark
Vincent
Francis

Sex
female
male
male
male
female
male
male
male
male
male
male
male
male
male
male

BDate
1995/01/25
1990/01/05
1990/03/01
1990/06/02
1994/05/26
1995/02/03
1995/04/05
1995/02/20
1990/03/06
1993/01/03
1992/01/07
1996/02/27
1991/08/08
1997/03/28
1998/04/29

Relationship
daughter
son
son
son
daughter
son
son
son
son
son
son
son
son
son
son

48

Full details employees dependents


ENo
emp19
emp20
emp20
emp31
emp31
emp33
emp33
emp34
emp34
emp35
emp35
emp39
emp40
emp42
emp43

DependentName
Luke
Angela
Bartolomew
Anne
Timothy
Agatha
Mary
Joaquin
Lucy
Dominic
Valentine
Claire
Therese
Joan
Thomas

Sex
male
female
male
female
male
female
female
male
female
male
male
female
female
female
male

BDate
1992/07/01
1999/05/30
1995/02/10
1994/03/09
1996/06/05
1997/07/06
1993/04/11
1998/05/12
1998/08/07
1997/06/13
1996/09/05
1996/07/14
1995/08/15
1994/09/16
1994/05/17

Relationship
son
daughter
son
daughter
son
daughter
daughter
son
daughter
son
son
daughter
daughter
daughter
son

49

Full details employees dependents


ENo
emp53
emp53
emp54
emp54
emp55
emp55
emp57
emp57
emp59
emp59
emp60
emp60
emp62
emp62

DependentName
Magdalene
Patrick
Joseph
Veronica
Isidore
Lorenzo
George
Pedro
Fidelis
Pio
Ignacia
Margaret
Faustina
Scholastica

Sex
female
male
male
female
male
male
male
male
male
male
female
female
female
female

BDate
1993/06/18
1995/10/08
1994/11/01
1992/07/19
1993/12/02
1991/08/20
1992/01/03
1990/09/21
1991/02/04
1998/10/22
1997/12/23
1992/03/05
1996/11/24
1991/04/06

Relationship
daughter
son
son
daughter
son
son
son
son
son
son
daughter
daughter
daughter
daughter

50

Retrieve all columns, all rows


Query#06: List full details of all
departments locations
DEPT_LOCATION(DNo, Location)
SELECT *
FROM DEPT_LOCATION;
51

List full details of all


departments locations
DNo
dept01
dept01
dept02
dept02
dept03
dept03
dept04
dept04
dept04

DLocation
Makati
USA
France
Makati
Davao
Makati
Germany
Makati
USA
52

Retrieve all columns, all rows


Query#07: List full details of all
employees assigned projects.
WORKS_ON(ENo, PNo, Hours)
SELECT *
FROM WORKS_ON;
53

Full details of all employees projects


ENo
emp01
emp01
emp01
emp01
emp02
emp02
emp02
emp02
emp03
emp03
emp04
emp04
emp05
emp05
emp06

PNo
proj01
proj02
proj03
proj04
proj01
proj02
proj03
proj04
proj01
proj03
proj03
proj04
proj01
proj04
proj01

Hours
2
3
4
5
4
3
2
1
5
6
4
3
4
2
5

54

Full details of all employees projects


ENo
emp06
emp07
emp07
emp08
emp09
emp09
emp10
emp11
emp11
emp11
emp12
emp12
emp13
emp13
emp14

PNo
proj02
proj03
proj04
proj04
proj01
proj05
proj02
proj05
proj06
proj07
proj05
proj06
proj05
proj06
proj06

Hours
5
5
1
1
1
1
1
6
6
6
3
4
5
6
8

55

Full details of all employees projects


ENo
emp14
emp15
emp15
emp16
emp16
emp17
emp17
emp18
emp18
emp19
emp20
emp20
emp21
emp31
emp31

PNo
proj07
proj05
proj07
proj05
proj07
proj05
proj06
proj06
proj07
proj05
proj03
proj06
proj07
proj01
proj05

Hours
7
1
1
3
2
4
5
7
6
8
1
1
2
3
3

56

Full details of all employees projects


ENo
emp31
emp31
emp31
emp32
emp32
emp33
emp33
emp34
emp34
emp35
emp35
emp36
emp36
emp37
emp37

PNo
proj08
proj09
proj10
proj08
proj09
proj08
proj09
proj09
proj10
proj08
proj10
proj08
proj10
proj09
proj10

Hours
5
3
4
4
3
4
5
3
2
5
1
4
6
3
3

57

Full details of all employees projects


ENo
emp38
emp38
emp39
emp40
emp40
emp41
emp42
emp43
emp43
emp51
emp51
emp51
emp51
emp51
emp51

PNo
proj08
proj10
proj08
proj09
proj10
proj10
proj08
proj07
proj09
proj01
proj02
proj03
proj04
proj05
proj06

Hours
4
3
5
4
4
3
2
1
1
1
1
1
1
1
1

58

Full details of all employees projects


ENo
emp51
emp51
emp51
emp51
emp51
emp51
emp51
emp51
emp51
emp52
emp52
emp53
emp53
emp54
emp54

PNo
proj07
proj08
proj09
proj10
proj11
proj12
proj13
proj14
proj15
proj11
proj12
proj13
proj14
proj12
proj15

Hours
1
1
1
1
5
6
4
3
3
4
5
4
3
2
1

59

Full details of all employees projects


ENo
emp55
emp55
emp56
emp56
emp57
emp57
emp58
emp58
emp59
emp60
emp61
emp62
emp63
emp64
emp64

PNo
proj12
proj14
proj11
proj13
proj12
proj14
proj14
proj15
proj11
proj12
proj10
proj14
proj10
proj09
proj15

Hours
5
6
4
3
4
5
3
4
2
1
5
6
4
3
3

60

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
61

Retrieve specific
columns, all rows
A

Table1
B
C

a1

b1

c1

a2

b2

c2

a3

b3

c3

Resulting
Table

SELECT B
FROM Table1;

B
b1
b2
b3
62

Retrieve specific
columns, all rows
Query#08: Produce a list of all projects showing
only the names and locations of projects.
PROJECT(PNo, PName, PLocation,DNum)
SELECT PName, PLocation
FROM PROJECT;
63

List names and locations of


all projects
PName
Alpha
Beta
Gamma
Theta
Rose
Tulip
Carnation
Red
Blue
Green
Earth
Mars
Venus
Jupiter
Pluto

PLocation
USA
Europe
Africa
Asia
England
Holland
France
Manila
Davao
Cebu
Europe
Asia
USA
Australia
Latin America

64

Retrieve specific
columns, all rows
Query#09: Produce a list of all department
names
DEPARTMENT(DNumber,DName,MgrEN
o)
SELECT DName
FROM DEPARTMENT;
65

List names of all departments


DName
Noli Me Tangere
Heroes
Presidents
News Makers

66

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
67

Use of Distinct
A

Table1
B
C

a1

b1

c1

a2

b2

c2

a2

b3

c3

a3

b4

c3

SELECT A
FROM Table1;

SELECT Distinct A
FROM Table1;

RT

RT

a1

a1

a2

a2

a2

a3

a3

68

Use of Distinct
Query#10: Produce a list of all filled-up
positions.
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
SELECT DISTINCT Position
FROM EMPLOYEE;
69

List of all filled-up positions


Position
assistant manager
clerk 1
clerk 2
clerk 3
manager
secretary
staff assistant 1
staff assistant 2

70

List full details of all


available positions
Position

Position

assistant manager

assistant manager

70,000.00

clerk 1

10,000.00

clerk 2

15,000.00

clerk 3

20,000.00

clerk 3

clerk 4

25,000.00

manager

manager

100,000.00

secretary

secretary

20,000.00

staff assistant 1

40,000.00

staff assistant 2

50,000.00

staff assistant 3

60,000.00

clerk 1
clerk 2

staff assistant 1
staff assistant 2

Salary

71

Use of Distinct
Query#11: Produce a list of all assigned
(employees work on) project numbers.
WORKS_ON(ENo, PNo, Hours)
SELECT DISTINCT PNo
FROM WORKS_ON;
72

List of all assigned projects


PNo
proj01
proj02
proj03
proj04
proj05
proj06
proj07
proj08
proj09
proj10
proj11
proj12
proj13
proj14
proj15

73

Full Details of All Projects


PNo
proj01
proj02
proj03
proj04
proj05
proj06
proj07
proj08
proj09
proj10
proj11
proj12
proj13
proj14
proj15

PNo
proj01
proj02
proj03
proj04
proj05
proj06
proj07
proj08
proj09
proj10
proj11
proj12
proj13
proj14
proj15

PName
Alpha
Beta
Gamma
Theta
Rose
Tulip
Carnation
Red
Blue
Green
Earth
Mars
Venus
Jupiter
Pluto

DNum
dept01
dept01
dept01
dept01
dept02
dept02
dept02
dept03
dept03
dept03
dept04
dept04
dept04
dept04
dept04

PLocation
USA
Europe
Africa
Asia
England
Holland
France
Manila
Davao
Cebu
Europe
Asia
USA
Australia
Latin America

74

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
75

Calculated Fields
An SQL expression can involve:
Addition (+)
Subtraction(-)
Multiplication (*)
Division (/)
Parentheses can be used to build
complex expressions.
76

Calculated Fields
Table1
A B C

SELECT A*10
FROM Table1;

10 b1 c1
20 b2 c2
30 b3 c3

SELECT A*10 As
Product
FROM Table1;

R Table

R Table

Expr01

Product

100

100

200

200

300

300

77

Calculated Fields
Query #12: Produce a list of yearly
salaries of all positions.
POSITION(Position, Salary)
SELECT Position, Salary*12
FROM POSITION;
78

List of Yearly salaries of all


positions
Position

Expr1001

assistant manager

840,000.00

clerk 1

120,000.00

clerk 2

180,000.00

clerk 3

240,000.00

clerk 4

300,000.00

manager

1,200,000.00

secretary

240,000.00

staff assistant 1

480,000.00

staff assistant 2

600,000.00

staff assistant 3

720,000.00

79

Calculated Fields
Query #13: Produce a list of yearly
salaries (AS YearlySalary) of all
positions.
POSITION(Position, Salary)
SELECT Position, Salary*12 AS
YearlySalary
FROM POSITION;
80

List of Yearly salaries of all


positions
Position

YearlySalary

assistant manager

840,000.00

clerk 1

120,000.00

clerk 2

180,000.00

clerk 3

240,000.00

clerk 4

300,000.00

manager

1,200,000.00

secretary

240,000.00

staff assistant 1

480,000.00

staff assistant 2

600,000.00

staff assistant 3

720,000.00

81

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
82

ROW SELECTION
(WHERE clause)
SELECT [DISTINCT|ALL] { * |
[column_exp [AS new_name]
[,] }
FROM table_name [alias] [,]
[WHERE search condition]

83

Five Basic Search


Conditions
Comparison. Compare the value of
one expression to the value of another
expression.
Range. Test whether the value of an
expression falls within a specified
range of values.
Set membership. Test whether the
value of an expression equals one of a
set of values.
84

Five Basic Search


Conditions
Pattern match. Test whether a string
matches a specified pattern.
Null. Test whether a column has a null
(unknown) value.

85

Simple Comparison
Operators
Equals =
Is less than <
Is greater than
>
Is less than or equal to <=
Is greater than or equal to >=
Is not equal to (ISO standard)
<>
Is not equal to (allowed in some) !=
86

RdDb2000

Simple comparison search


condition
Table1
A B C

SELECT A, B
FROM Table1
Where A=10;

10 b1 c1
20 b2 c2
30 b3 c3

SELECT A, B
FROM Table1
Where A <>10;

R Table

10 b1

R Table

20 b2
30 b3

87

Simple comparison search


condition

Query#14: List all employees (Name) with


positions of secretary.

EMPLOYEE(ENo, Name, Address, Sex,


CivilStatus, BDate, Position, SuperENo, DNo)
SELECT Name
FROM EMPLOYEE
WHERE Position='secretary';
88

List of employees with


position of secretary
Name
Maria Clara
Gabriela Silang
Fidel Ramos
Gloria Arroyo

89

Simple comparison search


condition
Query#15: List all positions with salaries
greater than or equal to 40,000.00
(Position, Salary).
POSITION(Position, Salary)
SELECT Position, Salary
FROM POSITION
WHERE Salary >= 40000.00 ;
90

List of positions with


salary >= 40,000.00
Position
manager

Salary
100,000.00

assistant manager

70,000.00

staff assistant 1

40,000.00

staff assistant 2

50,000.00

staff assistant 3

60,000.00
91

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
92

Logical Operators
AND
OR
NOT
Parentheses ()

93

Rules for Evaluating a


Conditional Expression
An expression is evaluated left to
right.
Subexpressions in brackets are
evaluated first.
NOTs are evaluated before ANDs and
ORs.
ANDs are evaluated before ORs.
94

Compound comparison search


condition
Table1
A B C
10 b1 c1
20 b2 c2
30 b3 c3

SELECT A, B
FROM Table1
WHERE A=10 AND
B=b1;
SELECT A, B
FROM Table1
WHERE A <>10 AND
B=b1;
SELECT A, B
FROM Table1
WHERE A <>10 OR
B=b1;

R Table

10 b1

R Table

R Table

10 b1
20 b2
30 b3
95

Compound comparison search


condition
Query#16: List all employees who are males
and positions are manager (Name, Position).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
SELECT Name, Position
FROM EMPLOYEE
WHERE Sex='male' AND
Position='manager' ;
96

List of employees who are


males and positions are
manager
Name

Position

Juan dela Cruz

manager

Jose Rizal

manager

Ping Lacson

manager

97

Compound comparison search


condition
Query#17: List all departments which are
located in USA or Germany (DNo, Location).
DEPT_LOCATION(DNo, Location)
SELECT DNo, Location
FROM DEPT_Location
WHERE DLocation='USA' OR
DLocation='Germany' ;
98

List all departments which


are located in USA or
Germany
DNo

DLocation

dept01

USA

dept04

USA

dept04

Germany
99

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
100

Range Operators
Attribute BETWEEN x AND y
Attribute NOT BETWEEN x AND y
Attribute >= literal1 AND
Attribute <= literal2

101

Range search condition


Table1
A B C
10 b1 c1
20 b2 c2
30 b3 c3

SELECT A, B
FROM Table1
WHERE A BETWEEN
10 AND 30;
SELECT A, B
FROM Table1
WHERE A NOT
BETWEEN 10 AND 30;

R Table

R Table

10 b1
20 b2
30 b3

R Table

10 b1

SELECT A, B
FROM Table1
WHERE A >=5 AND A<=15;

102

Range search condition


Query#18: List all positions with salaries
ranging from 20,000.00 to 40,000
(Position, Salary).
POSITION(Position, Salary)
SELECT *
FROM POSITION
WHERE Salary BETWEEN 20000 AND
40000 ;
103

Range search condition


Query#19: List all positions with salaries
ranging from 20,000.00 to 40,000 (Position,
Salary).
POSITION(Position, Salary)
SELECT *
FROM POSITION
WHERE Salary >= 20000 AND
Salary <= 40000;
104

List all positions with


salaries ranging from
20,000.00 to 40,000.00
Position

Salary

secretary

20,000.00

staff assistant 1

40,000.00

clerk 3

20,000.00

clerk 4

25,000.00
105

Range search condition


Query#20: List all employees with birth dates
between 1940/01/01 and 1950/06/30 (Name,
BDate).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
SELECT Name, BDate
FROM EMPLOYEE
WHERE BDate >=#1940/01/01# AND BDate
<= #1955/06/30#;
106

Range search condition


Query#21: List all employees with birth dates
between 1940/01/01 and 1950/06/30 (Name,
BDate).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
SELECT Name, BDate
FROM EMPLOYEE
WHERE BDate BETWEEN #1/1/1940# AND
#6/30/1955#;
107

List all employees with birth dates


between 1940/01/01 and 1950/06/30
Name
Juan dela Cruz
Sisa
Elias
Basilio
Tasio
Simon
Jose Rizal
Juan Luna
Antonio Luna
Corazon Aquino
Ping Lacson

BDate
1950/01/01
1955/02/02
1955/03/03
1955/04/04
1955/02/07
1955/02/12
1940/10/28
1955/03/03
1955/04/04
1950/08/05
1948/06/01
108

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
109

Set Membership
Operators
Attribute IN ('literal1', 'literal2'
'literaln')
Attribute NOT IN ('literal1', 'literal2'
'literaln')
Note: tests whether a data value
matches one of a list of values; more
efficient than using OR
110

Set membership search condition

Table1
A B C
10 b1 c1
20 b2 c2
30 b3 c3

SELECT A, B
FROM Table1
WHERE A IN (10,20,30);
SELECT A, B
FROM Table1
WHERE A =10 or A=20 or
A=30;

R Table

R Table

10 b1
20 b2
30 b3

SELECT A, B
FROM Table1
WHERE A NOT IN (10,20,30);

111

Set membership search condition


Query#22: List all employees with positions of
clerk1, clerk2 and clerk3 (Name, Position).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
SELECT Name, Position
FROM EMPLOYEE
WHERE Position IN ('clerk 1', 'clerk 2', 'clerk
3');
112

Set membership search condition


Query#23: List all employees with positions of
clerk1, clerk2 and clerk3 (Name, Position).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
SELECT Name, Position
FROM EMPLOYEE
WHERE Position='clerk 1' OR
Position= 'clerk 2' OR Position= 'clerk 3';
113

List all employees with positions


of clerk 1, clerk 2 and clerk 3
Name

Position

Crispin

clerk 1

Tasio

clerk 2

Damaso

clerk 3

Apolinario Mabini clerk 1


Emilio Jacinto

clerk 2

Ninoy Aquino

clerk 3

Evelio Javier

clerk 3

Elpidio Quirino

clerk 1

Manuel Quezon

clerk 2

Manuel Roxas

clerk 3

114

List all employees with positions


of clerk 1, clerk 2 and clerk 3
Name

Position

Carlos Garcia

clerk 3

Sergio Osmena

clerk 2

Jose Laurel

clerk 1

Nur Misuari

clerk 1

Commander Robot

clerk 2

Joma Sison

clerk 3

Vilma Santos

clerk 3

Sharon Cuneta

clerk 2

Frank Drilon

clerk 1

Manny Villar

clerk 1

115

Set membership search condition


Query#24: List all departments which are not
located in Makati, USA, or Germany (DNo,
DLocation).
DEPT_LOCATION(DNo, Location)
SELECT DNo, DLocation
FROM DEPT_LOCATION
WHERE DLocation NOT IN ('Makati', 'USA',
'Germany');
116

List all departments which are


not located in Makati, USA, or
Germany
DNo

DLocation

dept02

France

dept03

Davao

117

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
118

Pattern Matching Symbols


*

(%)
asterisk character represents any
sequence of zero or
more character
(wild card);
? (_)
question mark character represents any
single character.

119

Pattern Matching
Operators
Attribute LIKE H* means the first
character must start with H, but the rest of
the string can be anything.
Attribute LIKE H??? means that there
must be exactly four characters in the
string, the first of which must be an H.
Attribute Like Star#* ESCAPE #
means the use of an escape character to
include * in the string.

120

Pattern Matching
Operators

Attribute LIKE *e means any


sequence of characters, of length at
least 1, with the last character an e.
Attribute LIKE *Fun* means a
sequence of characters of any length
containing Fun.
Attribute NOT LIKE H* means
the name cannot start with an H.
121

Pattern match search condition

Table1
A B C
10 b1 c1
20 b2 c2
30 b3 c3

SELECT A, B
FROM Table1
WHERE B LIKE b*;
SELECT A, B
FROM Table1
WHERE B LIKE *4;
SELECT A, B
FROM Table1
WHERE B LIKE *2*;

R Table

R Table

10 b1
20 b2
30 b3

R Table

20 b2

122

Pattern match search condition


Query#25: List all employees with names
beginning with P (Name).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo,
DNo)
SELECT Name
FROM EMPLOYEE
WHERE Name Like P*;
123

List of employees with


names beginning with P
Name
Ping Lacson

124

Pattern match search condition


Query#26: List all employees with names
having the string son (Name).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo,
DNo)
SELECT Name
FROM EMPLOYEE
WHERE Name Like *son*;
125

List of employees with


names having the string son
Name
Ping Lacson
Joma Sison

126

Pattern match search condition


Query#27: List all employees with names
having exactly 11 characters (Name).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
SELECT Name
FROM EMPLOYEE
WHERE Name like '???????????';
127

List of employees with


names having exactly 11
characters
Name
Maria Clara
Fidel Ramos
Jose Laurel
Ping Lacson
Alfredo Lim
Nur Misuari
128

Simple Queries
Retrieve all columns, all rows
Retrieve specific columns, all rows
Use of Distinct
Calculated fields
Simple comparison search condition
Compound comparison search condition
Range search condition
Set membership search condition
Pattern match search condition
Null search condition
129

Null Operators
Attribute IS NULL
Attribute IS NOT NULL
Note:A null is considered to have an
unknown value such that the
attribute cannot be compared to
another string.
130

Null search condition


Query#28: List all employees who do not have
supervisors (Name).
EMPLOYEE(ENo, Name, Address, Sex,
CivilStatus, BDate, Position, SuperENo, DNo)
SELECT Name
FROM EMPLOYEE
WHERE SuperENo IS NULL;
131

List all employees who do


not have supervisors
Name
Juan dela Cruz
Jose Rizal
Corazon Aquino
Ping Lacson
132

CENTRAL BANK
DATABASE
Bank (Code, BankName, BankAddress)
Customer (SSN, CName, CAddr, Phone)
Branch (Code, BranchNo, BranchAddress)
Account (AcctNo, BranchNo, Code,
Balance, Type)
Loan (LoanNo, BranchNo, Code,
LoanAmount, Type)
A-C (AccountNo, SSN)
L-C (LoanNo, SSN)
133

Das könnte Ihnen auch gefallen