Lecture 8-9 More SQL
Lecture 8-9 More SQL
Lecture 8-9 More SQL
2
NULLS in SQL Queries
• NULL is used to represent missing value with three different
interpretations:
– Unknown value ( a person’s DoB is not known)
– Unavailable or withheld value (a person has a home phone number
but does not want it to be listed)
– Not applicable attribute (college_degree attribute would be NULL for a
person who has no college degrees)
3
4
NULLS IN SQL QUERIES
• SQL allows queries that check if a value is NULL (missing or
undefined or not applicable)
• SQL uses IS or IS NOT to compare NULLs
• Query 18: Retrieve the names of all employees who do not
have supervisors.
Slide 8-5
Relational Database Schema
Slide 8-6
Populated Database
Slide 8-7
NULLS IN SQL QUERIES
• Query 18: Retrieve the names of all employees who do not
have supervisors.
Slide 8-8
NESTING OF QUERIES
• A complete SELECT query, called a nested query , can be specified within
the WHERE-clause of another query, called the outer query
• Query 1: Retrieve the name and address of all employees who work for
the 'Research' department.
Slide 8-9
NESTING OF QUERIES (cont.)
• The nested query selects the number of the 'Research'
department
Slide 8-11
CORRELATED NESTED QUERIES (cont.)
• In Q12, the nested query has a different result for each tuple in
the outer query
• A query written with nested SELECT... FROM... WHERE... blocks
and using the = or IN comparison operators can always be
expressed as a single block query. For example, Q12 may be
written as in Q12A
Slide 8-12
THE EXISTS FUNCTION
• EXISTS is used to check whether the result of a correlated
nested query is empty (contains no tuples) or not
• We can formulate Query 12 in an alternative form that uses
EXISTS as Q12B below
Query 12: Retrieve the name of each employee who has a
dependent with the same first name as the employee.
Slide 8-13
THE NOT EXISTS FUNCTION
• Query 6: Retrieve the names of employees who have no
dependents.
Slide 8-14
EXPLICIT SETS
• It is also possible to use an explicit (enumerated) set of
values in the WHERE-clause rather than a nested query
Slide 8-15
Joined Tables in SQL
Slide 8-16
Joined Tables in SQL and Outer Joins
• Q1: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND DNUMBER=DNO
Slide 8-17
AGGREGATE FUNCTIONS
Slide 8-18
AGGREGATE FUNCTIONS (cont.)
Slide 8-19
AGGREGATE FUNCTIONS (cont.)
Slide 8-20
GROUPING
• In many cases, we want to apply the aggregate
functions to subgroups of tuples in a relation
• Each subgroup of tuples consists of the set of tuples
that have the same value for the grouping
attribute(s)
• The function is applied to each subgroup
independently
• SQL has a GROUP BY-clause for specifying the
grouping attributes, which must also appear in the
SELECT-clause
Slide 8-21
GROUPING (cont.)
• Query 20: For each department, retrieve the department number, the
number of employees in the department, and their average salary.
Slide 8-22
GROUPING (cont.)
• Query 21: For each project, retrieve the project number, project
name, and the number of employees who work on that project.
– In this case, the grouping and functions are applied after the joining of
the two relations
Slide 8-23
THE HAVING-CLAUSE
• Sometimes we want to retrieve the values of these functions for
only those groups that satisfy certain conditions
• Query 22: For each project on which more than two employees
work , retrieve the project number, project name, and the number
of employees who work on that project.
Slide 8-24
Constraints as Assertions in SQL
• General constraints: constraints that do not fit in the basic
SQL categories
Chapter 9-25
Assertions: An Example
• “The salary of an employee must not be greater than
the salary of the manager of the department that the
employee works for’’
Chapter 9-26
Using General Assertions
• Specify a query that violates the condition; include
inside a NOT EXISTS clause
• Query result must be empty
– if the query result is not empty, the assertion has been
violated
Chapter 9-27
SQL Triggers
• Objective: to monitor a database and take action
when a condition occurs
• Triggers are expressed in a syntax similar to
assertions and include the following:
– event (e.g., an update operation)
– condition
– action (to be taken when the condition is satisfied)
Chapter 9-28
SQL Triggers: An Example
• A trigger to compare an employee’s salary to his/her
supervisor during insert or update operations:
Chapter 9-29
Views in SQL
• A view is a “virtual” table that is derived from other tables
• Allows for limited update operations (since the table may not
physically be stored)
• Allows full query operations
• A convenience for expressing certain operations
Chapter 9-30
Specification of Views
• SQL command: CREATE VIEW
– a table (view) name
– a possible list of attribute names (for example, when
arithmetic operations are specified or when we want the
names to be different from the attributes in the base
relations)
– a query to specify the table contents
Chapter 9-31
SQL Views: An Example
• Specify a different WORKS_ON table
Chapter 9-32
Using a Virtual Table
• We can specify SQL queries on a newly create table
(view):
Chapter 9-33
Efficient View Implementation
• Query modification: present the view query in terms of a
query on the underlying base tables
– disadvantage: inefficient for views defined via complex
queries (especially if additional queries are to be applied
to the view within a short time period)
• View materialization: involves physically creating and keeping
a temporary table
– assumption: other queries on the view will follow
– concerns: maintaining correspondence between the base
table and the view when the base table is updated
– strategy: incremental update
Chapter 9-34
View Update
• Update on a single view without aggregate operations: update
may map to an update on the underlying base table
Chapter 9-35
Summary
• Additional features of SQL are discussed.
– Nested queries, joined tables, aggregate functions, and grouping.
36