Professional Documents
Culture Documents
Chap - 7 - Fundamentals of Database Systems Summary
Chap - 7 - Fundamentals of Database Systems Summary
• DIVISION Operation
• The division operation is applied to two relations
• R(Z) S(X), where X subset Z. Let Y = Z - X (and hence Z = X Y); that is, let Y be
the set of attributes of R that are not attributes of S.
• The result of DIVISION is a relation T(Y) that includes a tuple t if tuples tR appear in R
with tR [Y] = t, and with
• tR [X] = ts for every tuple ts in S.
• For a tuple t to appear in the result T of the DIVISION, the values in t must appear in
R in combination with every tuple in S.
USE OF NOT EXISTS
To achieve the “for all” (universal quantifier- see Ch.8) effect, we
use double negation this way in SQL:
Query: Retrieve the names of employees who work on all the
projects that ‘John Smith’ works on.
ALTERNATE SYNTAX:
SELECT E.Lname , S.Lname
FROM EMPLOYEE E, EMPLOYEE S
WHERE E.Super_ssn + = S.Ssn
Multiway JOIN in the FROM clause
• FULL OUTER JOIN – combines result if LEFT and RIGHT OUTER JOIN
• Can nest JOIN specifications for a multiway join:
• Example: An outer union can be applied to two relations whose schemas are
STUDENT(Name, SSN, Department, Advisor) and INSTRUCTOR(Name, SSN,
Department, Rank).
• Tuples from the two relations are matched based on having the same combination of
values of the shared attributes— Name, SSN, Department.
• If a student is also an instructor, both Advisor and Rank will have a value; otherwise, one
of these two attributes will be null.
• The result relation STUDENT_OR_INSTRUCTOR will have the following attributes:
STUDENT_OR_INSTRUCTOR (Name, SSN, Department, Advisor, Rank)
Slide 6- 52
Slide 6- 53
Slide 6- 54
Slide 6- 55
Slide 6- 56
Slide 6- 57
Aggregate Functions in SQL
• Used to summarize information from multiple tuples into a single-
tuple summary
• Built-in aggregate functions
• COUNT, SUM, MAX, MIN, and AVG
• Grouping
• Create subgroups of tuples before summarizing
• To select entire groups, HAVING clause is used
• Aggregate functions can be used in the SELECT clause or in a
HAVING clause
Renaming Results of Aggregation
• Following query returns a single row of computed values from EMPLOYEE table:
Q19: SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
FROM EMPLOYEE;
• The result can be presented with new names:
• INCORRECT QUERY:
SELECT Dno, COUNT (*)
FROM EMPLOYEE
WHERE Salary>40000
GROUP BY Dno
HAVING COUNT (*) > 5;
Combining the WHERE and the HAVING
Clause (continued)
Correct Specification of the Query:
• Note: the WHERE clause applies tuple by tuple whereas HAVING
applies to entire group of tuples
Use of WITH
• The WITH clause allows a user to define a table that will only be used
in a particular query (not available in all SQL implementations)
• Used for convenience to create a temporary “View” and use that
immediately in a query
• Allows a more straightforward way of looking a step-by-step query
Example of WITH
• See an alternate approach to doing Q28:
R5:
CREATE TRIGGER SALARY_VIOLATION
BEFORE INSERT OR UPDATE OF Salary, Supervisor_ssn ON
EMPLOYEE