Introductory SQL 2
Introductory SQL 2
Composite SQL Queries Using UNION, INTERSECT and EXCEPT (book sec. 3.5)
Demo Exercises & Exercises
3. Introductory SQL Contents 2
University Database, revisited
Database example used throughout the textbook and in this course!
Department
Classroom
Examples
SET @TeachingHistory = CONCAT('CS-101', ', ' , 'CS-315' , ', ' , 'CS-347'); # @ defines a variable
SELECT @TeachingHistory;
SELECT Student,
COUNT(Score) AS n,
SUM(Score) AS Total,
AVG(Score) AS Average,
MIN(Score) AS Lowest,
MAX(Score) AS Highest
FROM Testscores GROUP BY Student;
• Note: Predicates in the HAVING clause are applied after the formation
of groups, whereas predicates in the WHERE clause are applied before
forming groups.
So, WHERE is working on each Row and HAVING on each Group result.
Meaning:
1. Calculate the relation r represented by r1, r2, ..., rm and remove
rows from r not satisfying P1.
2. Arrange the selected rows into groups having the same values
for group-spec and remove groups not satisfying P2.
3. For each group calculate the attributes: this gives one
tuple/row for each group.
4. Order the rows according to the order-spec.
2. Introductory SQL 23
Subqueries (section 3.8)
What:
• A SELECT statement is said to be a subquery, if it occurs nested
inside another statement.
Where:
• A SELECT statement can be used to represent a relation in the
WHERE, FROM and HAVING clauses of another (outer) SELECT
statement.
Examples:
• Will be shown on the following slides.
Purpose:
• They provide alternative ways to perform operations that would
otherwise require complex joins and unions.
InstName
Wu
Einstein
Gold
Katz
3. Introductory SQL 26
Conditions Using ALL and SOME (section 3.8.2)
3. Introductory SQL 27
SOME
Find names of instructors with salary greater than that of
some (at least one) instructor in the Finance department.
SELECT InstName FROM Instructor
WHERE Salary > SOME (SELECT Salary FROM Instructor
WHERE DeptName = 'Finance');
An instance of this corresponds to: Salary
SELECT InstName FROM WHERE Salary > SOME 90000
80000
InstName
Wu
Which gives: Einstein
Gold
Brandt
3. Introductory SQL 28
SOME conditions
A op SOME r v r : (A op v )
Where r is a relation with one attribute and
op can be:
0
(5 < SOME 5 ) = true
6
0
(5 < SOME 5 ) = false
0
(5 = SOME 5 ) = true
0
(5 SOME 5 ) = true (since 0 5)
(= SOME) IN
However, ( SOME) NOT IN
3. Introductory SQL 29
ALL conditions
A op ALL r v r : (A op v)
0
(5 < ALL 5 ) = false
6
6
(5 < ALL 10 ) = true
4
(5 = ALL 5 ) = false
4
(5 ALL 6 ) = true
( ALL) NOT IN
But: (= ALL) IN
3. Introductory SQL 30
Conditions Using EXISTS and NOT EXISTS (section 3.8.3)
3. Introductory SQL 31
Compound Queries Using UNION, INTERSECT & EXCEPT (section 3.5)
Set Operations UNION, INTERSECT & EXCEPT have
• Arguments: two SELECT statements (denoting two relations R and S).
• R and S must be compatible: Number and types of the attributes of R and S must be the same.
• Result: is the relation consisting of the union/intersection and set difference of the
tuples in R and S.
• UNION and INTERSECT removes duplicates in the result.
• EXCEPT removes duplicates in its arguments before the operation is done.
Readings
• In Database Systems Concepts please complete reading Chapter 3
• Pay special attention to the Summary
MySQL Exercises
Solutions are available at 11:45
This shows that an “A” corresponds to 4 3.2.9 Find the Total Grade-Points Average
points, an “A-“ to 3.7 points, and so on. (GPA)
for each student who has taken a course, that
is, the total grade-points divided by the total
credits for the associated courses. Order the
students by falling averages.
Hint: use GROUP BY.