Beruflich Dokumente
Kultur Dokumente
2
Objectives of SQL
3
Objectives of SQL
4
Objectives of SQL
5
Objectives of SQL
6
Objectives of SQL
7
Writing SQL Commands
8
Writing SQL Commands
9
Writing SQL Commands
10
Literals
11
SELECT Statement
12
SELECT Statement
13
SELECT Statement
14
Example 5.1 All Columns, All Rows
SELECT *
FROM Staff;
15
Example 5.1 All Columns, All Rows
16
Example 5.2 Specific Columns, All Rows
17
Example 5.2 Specific Columns, All Rows
18
Example 5.3 Use of DISTINCT
SELECT propertyNo
FROM Viewing;
19
Example 5.3 Use of DISTINCT
20
Example Use of DISTINCT
Aberdeen House
London Flat
Glasgow Flat
Glasgow Flat
Glasgow House
Glasgow Flat
21
Example Use of DISTINCT
Aberdeen House
London Flat
Glasgow Flat
Glasgow House
22
Example 5.4 Calculated Fields
23
Example 5.5 Comparison Search Condition
24
Example 5.6 Compound Comparison Search
Condition
25
Example 5.7 Range Search Condition
26
Example 5.7 Range Search Condition
27
Example 5.7 Range Search Condition
28
Example 5.8 Set Membership
29
Example 5.8 Set Membership
30
Example 5.9 Pattern Matching
Find all owners with the string 'Glasgow' in their
address.
SELECT ownerNo, fName, lName, address, telNo
FROM PrivateOwner
WHERE address LIKE '%Glasgow%';
31
Example 5.9 Pattern Matching
32
Example 5.10 NULL Search Condition
33
Example 5.10 NULL Search Condition
34
Example 5.11 Single Column Ordering
35
Example 5.11 Single Column Ordering
36
Example 5.12 Multiple Column Ordering
37
Example 5.12 Multiple Column Ordering
38
Example 5.12 Multiple Column Ordering
39
Example 5.12 Multiple Column Ordering
40
INSERT
41
INSERT
42
Example 5.35 INSERT VALUES
43
Example 5.36 INSERT using Defaults
44
UPDATE
UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2...]
[WHERE searchCondition]
TableName can be name of a base table or an
updatable view.
SET clause specifies names of one or more
columns that are to be updated.
45
UPDATE
46
Example 5.38/39 UPDATE All Rows
UPDATE Staff
SET salary = salary*1.05
WHERE position = 'Manager';
47
Example 5.40 UPDATE Multiple Columns
UPDATE Staff
SET position = 'Manager', salary = 18000
WHERE staffNo = 'SG14';
48
DELETE
49
Example 5.41/42 DELETE Specific Rows
50
Lecture Further Objectives
51
SELECT Statement - Aggregates
52
SELECT Statement - Aggregates
53
SELECT Statement - Aggregates
54
SELECT Statement - Aggregates
55
Example 5.13 Use of COUNT(*)
56
Example 5.14 Use of COUNT(DISTINCT)
57
Example 5.15 Use of COUNT and SUM
58
Example 5.16 Use of MIN, MAX, AVG
59
SELECT Statement - Grouping
60
SELECT Statement - Grouping
61
Example 5.17 Use of GROUP BY
Find number of staff in each branch and their total
salaries.
SELECT branchNo,
COUNT(staffNo) AS count,
SUM(salary) AS sum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;
62
Example 5.17 Use of GROUP BY
63
Example 5.17 Use of GROUP BY
Find number of assistants in each branch and the
total salary bill for all assistants
SELECT branchNo,
COUNT(staffNo) AS count,
SUM(salary) AS sum
FROM Staff
WHERE position = Assistant
GROUP BY branchNo
ORDER BY branchNo;
64
Example 5.17 Use of GROUP BY
65
Restricted Groupings HAVING clause
66
Example 5.18 Use of HAVING
For each branch with more than 1 member of staff,
find number of staff in each branch and sum of their
salaries.
SELECT branchNo,
COUNT(staffNo) AS count,
SUM(salary) AS sum
FROM Staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;
67
Example 5.18 Use of HAVING
68
Subqueries
69
Example 5.19 Subquery with Equality
70
Example 5.19 Subquery with Equality
71
Example 5.19 Subquery with Equality
72
Example 5.20 Subquery with Aggregate
73
Example 5.20 Subquery with Aggregate
74
Example 5.20 Subquery with Aggregate
75
Subquery Rules
76
Subquery Rules
77
Multi-Table Queries
Can use subqueries provided result columns come from
same table.
If result columns come from more than one table must use a
join.
78
Multi-Table Queries
79
Example 5.24 Simple Join
80
Example 5.24 Simple Join
Only those rows from both tables that have
identical values in the clientNo columns
(c.clientNo = v.clientNo) are included in result.
Equivalent to equi-join in relational algebra.
81
Alternative JOIN Constructs
SQL provides alternative ways to specify joins:
82
Example 5.25 Sorting a join
83
Example 5.25 Sorting a join
84
Example 5.26 Three Table Join
85
Example 5.26 Three Table Join
86
Example 5.27 Multiple Grouping Columns
87
Example 5.27 Multiple Grouping Columns
88
Computing a Join
89
Computing a Join
90
Outer Joins
91
Outer Joins
The (inner) join of these two tables:
92
Outer Joins
93
Example 5.28 Left Outer Join
94
Example 5.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.
95
Example 5.29 Right Outer Join
96
Example 5.29 Right Outer Join
Right Outer join includes those rows of second
(right) table that are unmatched with rows from
first (left) table.
Columns from first table are filled with NULLs.
97
Example 5.30 Full Outer Join
98
Example 5.30 Full Outer Join
99
EXISTS and NOT EXISTS
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.
100
EXISTS and NOT EXISTS
(SELECT * ...)
101
Example 5.31 Query using EXISTS
102
Example 5.31 Query using EXISTS
103
Example 5.31 Query using EXISTS
Note, search condition s.branchNo = b.branchNo is
necessary to consider correct branch record for each
member of staff.
If omitted, would get all staff records listed out because
subquery:
104
Example 5.31 Query using EXISTS
105