Structured Query Language (SQL)
Structured Query Language (SQL)
Structured Query Language (SQL)
(SQL)
Topics of the Talk
Purpose and importance of SQL.
How to retrieve data from database using SELECT a
nd:
2
Topics of the Talk
3
Objectives of SQL
Ideally, database language should allow user to:
– create the database and relation structures;
– perform insertion, modification, deletion of data
from relations;
– perform simple and complex queries.
Must perform these tasks with minimal user effort
and command structure and syntax must be easy to
learn.
It must be portable.
4
Characteristics of SQL
5
Characteristics of SQL
6
Characteristics of SQL
Consists of standard English words:
7
Characteristics of SQL
8
History of SQL
9
History of SQL
10
History of SQL
11
Importance of SQL
12
Importance of SQL
13
Writing SQL Commands
14
Writing SQL Commands
16
SELECT Statement
17
SELECT Statement
18
SELECT Statement
19
Example 13.1 All Columns, All Rows
SELECT *
FROM staff;
20
Example 13.1 All Columns, All Rows
21
Example 13.2 Specific Columns, All Rows
22
Example 13.2 Specific Columns, All Rows
23
Example 13.3 Use of DISTINCT
SELECT pno
FROM viewing;
24
Example 13.3 Use of DISTINCT
Use DISTINCT to eliminate duplicates:
25
Example 13.4 Calculated Fields
26
Example 13.4 Calculated Fields
27
Example 13.5 Comparison Search Condition
28
Example 13.5 Comparison Search Condition
29
Example 13.6 Compound Comparison Search
Condition
30
Example 13.6 Compound Comparison Search
Condition
31
Example 13.7 Range Search Condition
32
Example 13.7 Range Search Condition
33
Example 13.7 Range Search Condition
Also a negated version NOT BETWEEN.
BETWEEN does not add much to SQL's expressive
power Could also write:
34
Example 13.8 Set Membership
35
Example 13.8 Set Membership
36
Example 13.8 Set Membership
37
Example 13.9 Pattern Matching
38
Example 13.9 Pattern Matching
39
Example 13.9 Pattern Matching
40
Example 13.10 NULL Search Condition
41
Example 13.10 NULL Search Condition
42
Example 13.11 Single Column Ordering
43
Example 13.11 Single Column Ordering
44
Example 13.12 Multiple Column Ordering
45
Example 13.12 Multiple Column Ordering
46
Example 13.12 Multiple Column Ordering
47
Example 13.12 Multiple Column Ordering
48
SELECT Statement - Aggregates
ISO standard defines five aggregate functions:
49
SELECT Statement - Aggregates
50
SELECT Statement - Aggregates
51
Example 13.13 Use of COUNT(*)
53
Example 13.14 Use of COUNT(DISTINCT)
54
Example 13.15 Use of COUNT and SUM
55
Example 13.16 Use of MIN, MAX, AVG
56
SELECT Statement - Grouping
Use GROUP BY clause to get sub-totals.
Each item in a result table must be single-valued per
group (see table 13.17 also).
A SELECT clause may only contain:
– Column names.
– Aggregate functions.
– Constants.
– An expression involving combinations of the above.
57
SELECT Statement - Grouping
58
Example 13.17 Use of GROUP BY
59
Example 13.17 Use of GROUP BY
60
Restricted Grouping
61
Example 13.18 Use of HAVING
62
Example 13.18 Use of HAVING
63
Subqueries
64
Example 13.19 Subquery with Equality
65
Example 13.19 Subquery with Equality
67
Example 13.20 Subquery with Aggregate
68
Example 13.20 Subquery with Aggregate
70
Subquery Rules
71
Example 13.21 Nested subquery: use of IN
List properties handled by staff at '163 Main St'.
74
ANY and ALL
ANY and ALL may be used with subqueries that pro
duce a single column of numbers. (numerical)
75
ANY and ALL
76
Example 13.22 Use of ANY/SOME
77
Example 13.22 Use of ANY/SOME
78
Example 13.23 Use of ALL
79
Example 13.23 Use of ALL
80
Multi-Table Queries
81
Multi-Table Queries
82
Example 13.24 Simple Join
83
Example 13.24 Simple Join
84
Example 13.24 Simple Join
85
Alternative JOIN Constructs
SQL2 provides alternative ways to specify joins:
86
Example 13.25 Sorting a join
87
Example 13.25 Sorting a join
88
Example 13.26 Three Table Join
89
Example 13.26 Three Table Join
90
Example 13.26 Three Table Join
91
Example 13.27 Multiple Grouping Columns
92
Example 13.27 Multiple Grouping Columns
93
Outer Joins
With a join, if one row of a table is unmatched, that r
ow is omitted from result table.
The outer join operations retain rows that do not sati
sfy the join condition.
Consider following two simplified tables:
BRANCH1 PROPERTY_FOR_RENT1
bnocity pno pcity
B3 Glasgow PA14 Aberdeen
B4 Bristol PL94 London
B2 London PG4 Glasgow
97
Outer Joins
The (inner) join of these two tables:
98
Outer Joins
Result table has two rows where the cities are the
same.
There are no rows corresponding to branches in
Bristol and Aberdeen.
To include unmatched rows in result table, use an
outer join.
99
Example 13.28 Left Outer Join
100
Example 13.28 Left Outer Join
Includes those rows of first (left) table unmatched
with rows from second (right) table.
Columns from second table are filled with NULLs.
101
Example 13.29 Right Outer Join
102
Example 13.29 Right Outer Join
Right outer join includes those rows of second (rig
ht) table that are unmatched with rows from first (
left) table.
Columns from first table are filled with NULLs.
103
Example 13.30 Full Outer Join
104
Example 13.30 Full Outer Join
105
EXISTS and NOT EXISTS
106
EXISTS and NOT EXISTS
107
Example 13.31 Query using EXISTS
108
Example 13.31 Query using EXISTS
109
Example 13.31 Query using EXISTS
111
Union, Intersect, and Difference (Except)
Can use normal set operations of union, intersection,
and difference to combine results of two or more queries
into a single result table.
Union of two tables, A and B, is table containing all rows
in either A or B or both.
Intersection is table containing all rows common to both
A and B.
Difference is table containing all rows in A but not in B.
Two tables must be union compatible.
112
Union, Intersect, and Difference (Except)
113
Union, Intersect, and Difference (Except)
114
Example 13.32 Use of UNION
(SELECT area
FROM branch
WHERE area IS NOT NULL) UNION
(SELECT area
FROM property_for_rent
WHERE area IS NOT NULL);
115
Example 13.32 Use of UNION
– Or
(SELECT *
FROM branch
WHERE area IS NOT NULL)
UNION CORRESPONDING BY area
(SELECT *
FROM property_for_rent
WHERE area IS NOT NULL);
116
Example 13.32 Use of UNION
Produces result tables from both queries and
merges both tables together.
117
Example 13.33 Use of INTERSECT
118
Example 13.33 Use of INTERSECT
Or
119
Example 13.33 Use of INTERSECT
Produces result tables from both queries and
creates single result table consisting of those rows
that are common to both result tables.
120
Example 13.33 Use of INTERSECT
SELECT city
FROM branch b property_for_rent p
WHERE b.city = p.city;
121
Example 13.33 Use of INTERSECT
Or
123
Example 13.34 Use of EXCEPT
Or
124
Example 13.34 Use of EXCEPT
Produces result tables from both queries and then
creates single result table consisting of those rows
appearing in first result table but not in second.
125
Example 13.34 Use of EXCEPT
126
Example 13.34 Use of EXCEPT
Or
127
INSERT
column_list is optional.
If omitted, SQL assumes a list of all columns in
their original CREATE TABLE order.
128
INSERT
129
Example 13.35 INSERT … VALUES
130
Example 13.36 INSERT using Defaults
131
Example 13.36 INSERT using Defaults
Or
132
INSERT … SELECT
133
Example 13.37 INSERT … SELECT
134
Example 13.37 INSERT … SELECT
136
UPDATE
UPDATE table_name
SET column_name1 = data_value1
[, column_name2 = data_value2...]
[WHERE search_condition]
table_name can be name of a base table or an upda
table view.
SET clause specifies names of one or more columns
that are to be updated.
137
UPDATE
138
Example 13.38 UPDATE All Rows
UPDATE staff
SET salary = salary*1.03;
139
Example 13.39 UPDATE Specific Rows
UPDATE staff
SET salary = salary*1.05
WHERE position = 'Manager';
140
Example 13.40 UPDATE Multiple Columns
UPDATE staff
SET position = 'Manager', salary = 18000
WHERE sno = 'SG14';
141
DELETE
142
Example 13.41 DELETE Specific Rows
143
Example 13.42 DELETE All Rows
144
ISO SQL Data Types
145
Data Definition
146
Data Definition
147
CREATE TABLE (Basic)
148
CREATE TABLE (Basic)
149
Example 13.43 CREATE TABLE
CREATE TABLE staff(
sno VARCHAR(5) NOT NULL,
fname VARCHAR(15)NOT NULL,
lname VARCHAR(15) NOT NULL,
address VARCHAR(50),
tel_no VARCHAR(13),
position VARCHAR(10) NOT NULL,
sex CHAR,
dob DATETIME,
salary DECIMAL(7,2) NOT NULL,
nin CHAR(9),
bno VARCHAR(3) NOT NULL);
150
Example 13.43 CREATE TABLE
CREATE TABLE property_for_rent(
pno VARCHAR(5) NOT NULL,
street VARCHAR(25)NOT NULL,
area VARCHAR(15),
city VARCHAR(15) NOT NULL,
pcode VARCHAR(8),
type CHAR(1) NOT NULL,
rooms SMALLINT NOT NULL,
rent DECIMAL(6,2) NOT NULL,
ono VARCHAR(5) NOT NULL,
sno VARCHAR(5),
bno VARCHAR(3) NOT NULL);
151
DROP TABLE
152