Docsql
Docsql
Docsql
What is SQL?
When a user wants to get some information
from a database file, he can issue a query.
A query is a userrequest to retrieve data or
information with a certain condition.
SQL is a query language that allows user to
specify the conditions. (instead of algorithms)
Concept of SQL
2
General
Structure
Comparison
Grouping
Display Order
Logical
Operators
Output
Union
UNION
The Situation:
Student Particulars
field
id
name
dob
sex
class
hcode
dcode
remission
mtest
type
width
numeric
4
character
10
date
8
character
1
character
2
character
1
character
3
logical
1
numeric
2
contents
student id number
name
date of birth
sex: M / F
class
house code: R, Y, B, G
district code
fee remission
Math test score
I
SELECT ...... FROM ...... WHERE ......
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHERE condition
I
eg. 1
Result
id
name
9801 Peter
9802 Mary
9803 Johnny
9804 Wendy
9805 Tobe
:
:
dob
06/04/86
01/10/86
03/16/86
07/09/86
10/17/86
:
sex
M
F
M
F
M
:
class
1A
1A
1A
1B
1B
:
mtest
70
92
91
84
88
:
hcode
R
Y
G
B
R
:
dcode
SSP
HHM
SSP
YMT
YMT
:
remission
.F.
.F.
.T.
.F.
.F.
:
eg. 2
Class
class="1A"
1A
1A
1A
1B
1B
:
eg. 2
name
Peter
Mary
Johnny
Luke
Bobby
Aaron
:
hcode
R
Y
G
G
B
R
:
class
1A
1A
1A
1A
1A
1A
:
eg. 3
dcode
HHM
KWC
MKK
SSP
TST
YMT
eg. 4
1B Girls ?
eg. 4
eg. 4
What is "age"?
eg. 4
Functions:
# days : DATE( ) dob
# years :(DATE( ) dob) / 365
1 d.p.: ROUND(__ , 1)
eg. 4
name
Wendy
Kitty
Janet
Sandy
Mimi
age
12.1
11.5
12.4
12.3
12.2
eg. 5
name
Peter
Mary
Luke
Bobby
Aaron
Ron
Gigi
:
id
9801
9802
9810
9811
9812
9813
9824
:
class
1A
1A
1A
1A
1A
1A
1A
:
II
II
eg. 6
Result
name
Peter
Wendy
Kevin
Luke
Aaron
:
class
1A
1B
1C
1A
1A
:
bdate
Wednesday
Wednesday
Saturday
Wednesday
Saturday
:
II
eg. 7
Result
name
Wendy
Tobe
Eric
Patty
Kevin
Bobby
Aaron
:
class
1B
1B
1C
1C
1C
1A
1A
:
dob
07/09/86
10/17/86
05/05/87
08/13/87
11/21/87
02/16/86
08/02/86
:
II
eg. 8
Result
name
Luke
Aaron
Gigi
mtest
86
83
84
II
eg. 9
Result
name
Tobe
Teddy
Tim
class
1B
1B
2A
II
eg. 10
Result
name
Aaron
Janet
Paula
class
1A
1B
2A
hcode
R
R
R
III
III
eg. 11
Group By Class
class
1A
1A
1A
1A
COUNT( )
1B
1B
1B
1B
1B
COUNT( )
1B
1C
1B
1C
1C
1C
Student
COUNT( )
III
eg. 11
class
1A
1B
1C
2A
2B
2C
cnt
10
9
9
8
8
6
III
eg. 12
Group By Class
class
1A
1A
1A
1A
AVG( )
1B
1B
1B
1B
1B
AVG( )
1B
1C
1B
1C
1C
1C
Student
AVG( )
III
eg. 12
class
1A
1B
1C
2A
2B
2C
avg_mtest
85.90
70.33
37.89
89.38
53.13
32.67
III
eg. 13
dcode
HHM
KWC
MKK
SSP
TST
YMT
cnt
6
1
1
5
4
8
III
eg. 14
Result
III
eg. 15
Result
avg_mtest class
86.00
1A
77.75
1B
35.60
1C
86.50
2A
56.50
2B
IV
IV
eg. 16
id
9801
9803
9810
9811
9812
9813
Result
ORDER BY
dcode
name
Aaron
Bobby
Johnny
Luke
Peter
Ron
id
9812
9811
9803
9810
9801
9813
IV
eg. 17
name
Jimmy
Tim
Samual
Rosa
Helen
Joseph
Paula
Susan
id
9712
9713
9714
9703
9702
9715
9701
9704
class
2A
2A
2A
2A
2A
2A
2A
2A
dcode
HHM
HHM
SHT
SSP
TST
TSW
YMT
YMT
IV
eg. 18
Result
cnt
11
10
10
9
5
2
1
1
1
docode
YMT
HHM
SSP
MKK
TST
TSW
KWC
MMK
SHT
IV
eg. 19
IV
Result
Blue
House
Order
by
hcode
Green
House
:
:
name
Bobby
Teddy
Joseph
Zion
Leslie
Johnny
Luke
Kevin
George
:
hcode
B
B
B
B
B
G
G
G
G
:
class
1A
1B
2A
2B
2C
1A
1A
1C
1C
:
Order
by
class
V
INTO TABLE tablename
TO PRINTER
send to printer.
TO SCREEN
display on screen.
eg. 20
Result
id
9707
9709
9804
9819
9805
9713
9816
:
name
Zion
Yvonne
Wendy
Vincent
Tobe
Tim
Teddy
:
dob
07/29/85
08/24/85
07/09/86
03/15/85
10/17/86
06/19/85
01/30/86
:
sex
M
F
F
M
M
M
M
:
class
2B
2C
1B
1C
1B
2A
1B
:
mtest
51
10
84
29
88
91
64
:
hcode
B
R
B
Y
R
R
B
:
dcode
MKK
TST
YMT
MKK
YMT
HHM
SSP
:
remission
.F.
.F.
.F.
.F.
.F.
.T.
.F.
:
eg. 21
Result
class
1A
1A
1A
1B
1B
1B
1B
:
name
Aaron
Peter
Ron
Tobe
Janet
Kitty
Mimi
:
sex
M
M
M
M
F
F
F
:
The Situation:
Bridge Club & Chess Club
Consider the members of the Bridge Club and
the Chess Club. The two database files have
the same structure:
field
id
name
sex
class
type
numeric
character
character
character
width
4
10
1
2
contents
student id number
name
sex: M / F
class
Bridge [A]
1
2
3
4
5
Chess [B]
id
name
sex
class
9812
9801
9814
9806
9818
:
Aaron
Peter
Kenny
Kitty
Edmond
:
M
M
M
F
M
:
1A
1A
1B
1B
1C
:
1
2
3
4
5
id
name
sex
class
9802
9801
9815
9814
9817
:
Mary
Peter
Eddy
Kenny
George
:
F
M
M
M
M
:
1A
1A
1B
1B
1C
:
3
eg. 22
Result
3
eg. 23
Result
eg. 24
Result
4
field1
A
B
field2
1
2
3
field1
A
A
A
B
B
B
field2
1
2
3
1
2
3
The Situation:
Music Lesson
Each student should learn a musical instrument.
Two database files: student.dbf & music.dbf
The common field: student id
field
id
type
type
numeric
character
width contents
4
student id number
10
type of the music instrument
SELECT A
USE student
SELECT B
USE music
eg. 25
name class
id
Same id
9801
Join
Student
id
name class
9801
Product
type
9801
Music
type
eg. 25
Result
class
1A
1A
1A
1A
1A
1A
1A
:
name
Aaron
Bobby
Gigi
Jill
Johnny
Luke
Mary
:
id
9812
9811
9824
9820
9803
9810
9802
:
type
Piano
Flute
Recorder
Piano
Violin
Piano
Flute
:
4
eg. 26
Three Parts :
(1) Natural Join.
(2) Condition: m.type="Piano"
(3) GROUP BY class
eg. 26
Student
Join
Condition
m.type= "Piano"
Product
Music
Group By
class
4
eg. 26
Result
class
1A
1B
1C
cnt
4
2
1
4
eg. 27
name class
9801
Student
id
type
No match
Music
4
eg. 27
Result
class
1A
1B
1B
1C
1C
:
name
Mandy
Kenny
Tobe
Edmond
George
:
id
9821
9814
9805
9818
9817
:
4
eg. 28
4
eg. 28
Natural Join
Outer Join
No Match
4
eg. 28
4
class
1A
1A
1A
1A
1A
1A
1A
:
name
Aaron
Bobby
Gigi
Jill
Johnny
Luke
Mary
:
id
9812
9811
9824
9820
9803
9810
9802
:
type
Piano
Flute
Recorder
Piano
Violin
Piano
Flute
:
Natural Join
class
1A
1B
1B
1C
1C
:
name
Mandy
Kenny
Tobe
Edmond
George
:
id
9821
9814
9805
9818
9817
:
No Match
class
1A
1A
1A
1A
1A
1A
1A
1A
1A
1A
1B
1B
1B
1B
:
name
Aaron
Bobby
Gigi
Jill
Johnny
Luke
Mandy
Mary
Peter
Ron
Eddy
Janet
Kenny
Kitty
:
id
9812
9811
9824
9820
9803
9810
9821
9802
9801
9813
9815
9822
9814
9806
:
type
Piano
Flute
Recorder
Piano
Violin
Piano
Flute
Piano
Guitar
Piano
Guitar
empty
Recorder
:
Outer Join