Structured Query Language
Structured Query Language
1
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)
2
1 Introduction to SQL
Concept of SQL
3
1 Introduction to SQL
How to involve SQL in FoxPro
– Before using SQL, the tables should be
opened.
– The SQL command can be entered directly
in the Command Window
– To perform exact matching, we should
SET ANSI ON
4
2 Basic structure of an SQL
query
General SELECT, ALL / DISTINCT, *,
Structure AS, FROM, WHERE
Union UNION
5
I General Structure
6
I General Structure
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHERE condition
– The query will select rows from the source tablename and
output the result in table form.
7
I General Structure
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHERE condition
– DISTINCT will eliminate duplication in the output
while ALL will keep all duplicated rows.
– condition can be :
• (1) an inequality, or
• (2) a string comparison
• using logical operators AND, OR, NOT.
8
I General Structure
Before using SQL, open the student file:
USE student
eg. 1 List all the student records.
SELECT * FROM student
9
I
eg. 2
General Structure
List the names and house code of 1A students.
Class Class
1A 1A
class="1A"
1A 1A
1A 1A
1B 1B
1B 1B
: :
10
I
eg. 2
General Structure
List the names and house code of 1A students.
11
I
eg. 3
General Structure
List the residential district of the Red House
members.
SELECT DISTINCT dcode FROM student ;
WHERE hcode="R"
dcode
Result
HHM
KWC
MKK
SSP
TST
YMT
12
II Comparison
13
II
eg. 9
Comparison
List the students whose names start with "T".
14
II Comparison
15
III Grouping
SELECT ...... FROM ...... WHERE condition ;
GROUP BY groupexpr [HAVING requirement]
Group functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
17
IV Display Order
eg. 16 List the boys of class 1A, order by their names.
18
IV Display Order
eg. 17 List the 2A students by their residential district.
SELECT name, id, class, dcode FROM student ;
WHERE class="2A" ORDER BY dcode
name id class dcode
Result Jimmy 9712 2A HHM
Tim 9713 2A HHM
Samual 9714 2A SHT
Rosa 9703 2A SSP
Helen 9702 2A TST
Joseph 9715 2A TSW
Paula 9701 2A YMT
Susan 9704 2A YMT
19
IV Display Order
eg. 18 List the number of students of each district
(in desc. order).
SELECT COUNT(*) AS cnt, dcode FROM student ;
GROUP BY dcode ORDER BY cnt DESC
cnt docode
Result 11 YMT
10 HHM
10 SSP
9 MKK
5 TST
2 TSW
1 KWC
1 MMK
1 SHT
20
IV Display Order
eg. 19 List the boys of each house order by the
classes. (2-level ordering)
21
IV Display Order
Result
name hcode class
Bobby B 1A
Blue
House Teddy B 1B Order
Joseph B 2A by
Zion B 2B class
Order
Leslie B 2C
by Johnny G 1A
hcode Luke G 1A
Kevin G 1C
Green
House George G 1C
: : :
:
:
22
3 Union, Intersection and
Difference of Tables
The union of A and B (AB)
A B
23
3 Union, Intersection and
Difference of Tables
The intersection of A and B (AB)
A B
24
3 Union, Intersection and
Difference of Tables
The difference of A and B (A–B)
A B
25
3 Union, Intersection and
Difference of Tables
Bridge [A] Chess [B]
id name sex class id name sex class
1 9812 Aaron M 1A 1 9802 Mary F 1A
2 9801 Peter M 1A 2 9801 Peter M 1A
3 9814 Kenny M 1B 3 9815 Eddy M 1B
4 9806 Kitty F 1B 4 9814 Kenny M 1B
5 9818 Edmond M 1C 5 9817 George M 1C
: : : : : : : :
27
3 Union, Intersection and
Difference of Tables
SELECT ...... FROM table1 ;
WHERE col IN ( SELECT col FROM table2 )
28
3 Union, Intersection and
Difference of Tables
SELECT ...... FROM table1 ;
WHERE col NOT IN ( SELECT col FROM table2 )
29
4 Multiple Tables:
• SQL provides a convenient operation to
retrieve information from multiple tables.
30
4 Multiple Tables:
field1 field2
A 1
field1 field2
A 2
A 1
A 3
B 2
B 1
3
B 2
B 3
31