Structured Query Language
Structured Query Language
1 Introduction to SQL
What is SQL?
– When a user wants to get some information
from a database file, he can issue a query.
– A query is a user–request to retrieve data or
information with a certain condition.
– SQL is a query language that allows user to
specify the conditions. (instead of algorithms)
1 Introduction to SQL
Concept of SQL
Union UNION
2 The Situation:
Student Particulars
field type width contents
id numeric 4 student id number
name character 10 name
dob date 8 date of birth
sex character 1 sex: M / F
class character 2 class
hcode character 1 house code: R, Y, B, G
dcode character 3 district code
remission logical 1 fee remission
mtest numeric 2 Math test score
I General Structure
Class Class
1A 1A
class="1A"
1A 1A
1A 1A
1B 1B
1B 1B
: :
I
eg. 2
General Structure
List the names and house code of 1A students.
1B Girls ?
I
eg. 4
General Structure
List the names and ages (1 d.p.) of 1B girls.
What is "age"?
I
eg. 4
General Structure
List the names and ages (1 d.p.) of 1B girls.
Functions:
# days : DATE( ) – dob
# years :(DATE( ) – dob) / 365
1 d.p.: ROUND(__ , 1)
I
eg. 4
General Structure
List the names and ages (1 d.p.) of 1B girls.
Group functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
1A 1A
1A
COUNT( )
1B
1B
1B
1B 1B
COUNT( )
1B
1B
1C
1C 1C
1C
COUNT( )
Student
III Grouping
eg. 11 List the number of students of each class.
SELECT class, COUNT(*) FROM student ;
GROUP BY class
class cnt
Result 1A 10
1B 9
1C 9
2A 8
2B 8
2C 6
III Grouping
eg. 12 List the average Math test score of each class.
Group By Class
class
1A
1A 1A
1A
AVG( )
1B
1B
1B
1B
AVG( )
1B
1B
1B
1C
1C 1C AVG( )
1C
Student
III Grouping
eg. 12 List the average Math test score of each class.
A B
A B
A B
field1 field2
A 1
field1 field2
A 2
A 1
A 3
B 2
B 1
3
B 2
B 3
4 The Situation:
Music Lesson
Each student should learn a musical instrument.
Two database files: student.dbf & music.dbf
The common field: student id
SELECT A
USE student
SELECT B
USE music
4 Natural Join
A Natural Join is a join operation that joins two
tables by their common column. This operation
is similar to the setting relation of two tables.
Same id 9801
9801
Join
Student Music
9801
Product
4
eg. 25
Natural Join
Make a list of students and the instruments they
learn. (Natural Join)
SELECT s.class, s.name, s.id, m.type ;
FROM student s, music m ;
WHERE s.id=m.id ORDER BY class, name
class name id type
Result 1A Aaron 9812 Piano
1A Bobby 9811 Flute
1A Gigi 9824 Recorder
1A Jill 9820 Piano
1A Johnny 9803 Violin
1A Luke 9810 Piano
1A Mary 9802 Flute
: : : :
4 Natural Join
eg. 26 Find the number of students learning piano in
each class.
Three Parts :
(1) Natural Join.
(2) Condition: m.type="Piano"
(3) GROUP BY class
4 Natural Join
eg. 26
Music
4 Natural Join
eg. 26 Find the number of students learning piano in
each class.
SELECT s.class, COUNT(*) ;
FROM student s, music m ;
WHERE s.id=m.id AND m.type="Piano" ;
GROUP BY class ORDER BY class
class cnt
Result 1A 4
1B 2
1C 1
4 Outer Join
An Outer Join is a join operation that includes
rows that have a match, plus rows that do not
have a match in the other table.
4 Outer Join
eg. 27 List the students who have not yet chosen an
instrument. (No match)
9801
No match
Student Music
4 Outer Join
eg. 27 List the students who have not yet chosen an
instrument. (No match)
SELECT class, name, id FROM student ;
WHERE id NOT IN ( SELECT id FROM music ) ;
ORDER BY class, name
class name id
Result 1A Mandy 9821
1B Kenny 9814
1B Tobe 9805
1C Edmond 9818
1C George 9817
: : :
4 Outer Join
eg. 28 Make a checking list of students and the
instruments they learn. The list should also
contain the students without an instrument.
(Outer Join)
4 Outer Join
eg. 28
Natural Join
Outer Join
No Match
4 Outer Join
eg. 28 SELECT s.class, s.name, s.id, m.type ;
FROM student s, music m ;
WHERE s.id=m.id ;
UNION ;
SELECT class, name, id, "" ;
FROM student ;
WHERE id NOT IN ( SELECT id FROM music ) ;
ORDER BY 1, 2
4
class
Outer Join
name id type
class name id type
1A Aaron 9812 Piano
1A Bobby 9811 Flute 1A Aaron 9812 Piano
1A Gigi 9824 Recorder 1A Bobby 9811 Flute
1A Jill 9820 Piano 1A Gigi 9824 Recorder
1A Johnny 9803 Violin 1A Jill 9820 Piano
1A Luke 9810 Piano 1A Johnny 9803 Violin
1A Mary 9802 Flute 1A Luke 9810 Piano
: : : : 1A Mandy 9821
Natural Join 1A Mary 9802 Flute
1A Peter 9801 Piano
class name id
1A Mandy 9821
1A Ron 9813 Guitar empty
1B Eddy 9815 Piano
1B Kenny 9814
1B Tobe 9805 1B Janet 9822 Guitar
1C Edmond 9818 1B Kenny 9814
1C George 9817 1B Kitty 9806 Recorder
: : : : : : :
No Match Outer Join