Académique Documents
Professionnel Documents
Culture Documents
Chapter 5 - Objectives
Chapter 5 - Objectives
Join tables together.
Perform set operations (UNION, INTERSECT,
EXCEPT).
using
INSERT,
Objectives of SQL
Objectives of SQL
Objectives of SQL
Objectives of SQL
Objectives of SQL
History of SQL
History of SQL
10
History of SQL
11
Importance of SQL
12
Importance of SQL
13
14
15
16
Literals
17
SELECT Statement
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM
TableName [alias] [, ...]
[WHERE
condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
18
SELECT Statement
FROM
Specifies table(s) to be used.
WHERE Filters rows.
GROUP BY
Forms groups of rows with same
column value.
HAVING Filters groups subject to some
condition.
SELECT Specifies which columns are to
appear in output.
ORDER BY
Specifies the order of the output.
19
SELECT Statement
20
21
22
23
24
25
26
27
Pearson Education Limited 1995, 2005
28
29
Example 5.6
Condition
30
31
32
33
34
35
36
of
37
There are 2 viewings for property PG4, one with and one
without a comment.
Have to test for null explicitly using special keyword IS
NULL:
SELECT clientNo, viewDate
FROM Viewing
WHERE propertyNo = PG4 AND
comment IS NULL;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Pearson Education Limited 1995, 2005
53
column names
aggregate functions
constants
expression involving combinations of the above.
54
55
branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;
56
57
58
59
60
Subqueries
INSERT,
61
62
63
64
65
66
67
Subquery Rules
68
Subquery Rules
69
70
71
72
73
74
75
76
Multi-Table Queries
If result columns come from more than one table must use
a join.
77
Multi-Table Queries
78
79
80
81
82
83
84
85
86
87
Computing a Join
Procedure for generating results of a join are:
1. Form Cartesian product of the tables named in
FROM clause.
2. If there is a WHERE clause, apply the search
condition to each row of the product table, retaining
those rows that satisfy the condition.
3. For each remaining row, determine value of each
item in SELECT list to produce a single row in
result table.
Pearson Education Limited 1995, 2005
88
Computing a Join
4. If DISTINCT has been specified, eliminate any
89
Outer Joins
90
Outer Joins
91
Outer Joins
92
93
94
95
96
97
98
EXISTS and NOT EXISTS are for use only with subqueries.
True if and only if there exists at least one row in result table
returned by subquery.
99
As (NOT) EXISTS check only for existence or nonexistence of rows in subquery result table,
subquery can contain any number of columns.
100
101
102
103
104
105
106
107
108
Or
(SELECT *
FROM Branch
WHERE city IS NOT NULL)
UNION CORRESPONDING BY city
(SELECT *
FROM PropertyForRent
WHERE city IS NOT NULL);
Pearson Education Limited 1995, 2005
109
110
111
Or
(SELECT * FROM Branch)
INTERSECT CORRESPONDING BY city
(SELECT * FROM PropertyForRent);
112
Pearson Education Limited 1995, 2005
Or:
SELECT DISTINCT city FROM Branch b
WHERE EXISTS
(SELECT * FROM PropertyForRent p
WHERE p.city = b.city);
113
Or
114
Or
SELECT DISTINCT city FROM Branch b
WHERE NOT EXISTS
(SELECT * FROM PropertyForRent p
WHERE p.city = b.city);
115
INSERT
INSERT INTO TableName [ (columnList) ]
VALUES (dataValueList)
116
INSERT
117
118
Or
INSERT INTO Staff
VALUES (SG44, Anne, Jones, Assistant, NULL,
NULL, 8100, B003);
119
INSERT SELECT
120
Populate StaffPropCount
PropertyForRent tables.
using
Staff
and
121
122
123
UPDATE
UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2...]
[WHERE searchCondition]
124
UPDATE
125
126
to
UPDATE Staff
SET position = Manager, salary = 18000
WHERE staffNo = SG14;
127
DELETE
DELETE FROM TableName
[WHERE searchCondition]
128
129