Relational Data Retrieval: SQL
Advanced Database Systems
Chapter Objectives
Describe SQL as a relational data manipulation
language.
Explain that you can create and update relational
tables using SQL.
Write SQL SELECT commands to retrieve relational
data using a variety of operators, including GROUP
BY, ORDER BY, and the built-in functions of AVG,
SUM, MAX, MIN, and COUNT.
4-2
Chapter Objectives
Write SQL SELECT commands that join relational
tables.
Write SQL SELECT subqueries.
Describe a strategy for writing SQL SELECT
statements.
Describe the principles of how a relational query
optimizer works.
4-3
Data Management
Data Definition
Data Manipulation
4-4
Data Management:
Data Definition
Operationalized with a data definition language (DDL).
Instructs the DBMS software on what tables will be in the
database, what attributes will be in the tables, which
attributes will be indexed, etc.
4-5
Data Management:
Data Manipulation
Refers to the four basic operations that can and must
be performed on data stored in any DBMS.
Data retrieval
Data update
Insertion of new records
Deletion of existing records
Requires data manipulation language (DML)
4-6
SQL
Structured Query Language
Incorporates both DDL and DML features.
Very heavily used in practice today.
4-7
Building the Data Structure
Base tables - actual physical tables in which the data
will be stored on the disk.
Created using the CREATE TABLE command.
Deleted using the DROP TABLE command.
4-8
Data Manipulation Operations
UPDATE - used for updating existing data.
INSERT - used for inserting new rows in tables.
DELETE - used for deleting existing rows in tables.
4-9
Introduction: SQL SELECT
Used for data retrieval.
You specify what data you are looking for rather than
provide a logical sequence of steps that guide the
system in how to find the data.
Can be run in either a query or an embedded mode.
Command will work with Oracle, MS Access, SQL
Server, DB2, Informix, etc.
4-10
SELECT Statement
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM TableName [alias] [, ...]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
Slide 21 (of 116)
The Basic SQL SELECT
SELECT <columns>
FROM <table>
WHERE <predicates identifying rows to be included>;
4-12
SELECT Statement
SELECT Specifies which columns are to
appear in output.
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.
ORDER BY Specifies the order of the output.
Slide 22 (of 116)
General Hardware Company Database
(Modified)
4-14
General Hardware Company SQL Query
Example
“Find the commission SELECT COMMPERCT, YEARHIRE FROM
percentage and year of hire of SALESPERSON
salesperson number 186.” WHERE SPNUM=186;
The desired attributes are listed in the SELECT clause.
The required table is listed in the FROM clause.
The restriction (predicate) indicating which row(s) is involved is shown in
the WHERE clause in the form of an equation.
4-15
General Hardware Company SQL Query
Example, *
“Retrieve the entire record for SELECT *
salesperson 186.” FROM SALESPERSON
WHERE SPNUM=186;
The “*” indicates that all attributes of the selected row are to be
retrieved.
4-16
General Hardware Company SQL Query
Example
“List the salesperson numbers SELECT SPNUM, SPNAME
and salesperson names of those FROM SALESPERSON
salespersons who have a WHERE COMMPERCT=10;
commission percentage of 10.”
The search argument is nonunique in this query.
4-17
General Hardware Company SQL Query
Example, No WHERE
“List the salesperson number SELECT SPNUM, SPNAME
and salesperson name of all of FROM SALESPERSON;
the salespersons.”
For a Relational Algebra Project operation, there is no need
for a WHERE clause to limit which rows of the table are
included.
4-18
General Hardware Company SQL Query
Example, *
“Retrieve all of the SELECT *
Salespersons.” FROM SALESPERSON;
Retrieves an entire table, that is, the query places no
restrictions on either the rows or the attributes.
4-19
Comparisons
In addition to equal (=), the standard comparison
operators can be used in the WHERE clause.
Greater than (>)
Less than (<)
Greater than or equal to (>=)
Less than or equal to (<=)
Not equal to (<>)
4-20
General Hardware Company SQL Query
Example, <
“List the salesperson numbers, SELECT SPNUM, SPNAME, COMMPERCT
salesperson names, and FROM SALESPERSON
commission percentages of the WHERE COMMPERCT < 12;
salespersons whose commission
percentage is less than 12.”
4-21
General Hardware Company SQL Query
Example, >=
“List the customer numbers SELECT CUSTNUM, HQCITY
and headquarters cities of the FROM CUSTOMER
customers that have a customer WHERE CUSTNUM >= 1700;
number of at least 1700.”
4-22
General Hardware Company SQL Query
Example: AND
“List the customer numbers, SELECT CUSTNUM, CUSTNAME, HQCITY
customer names, and FROM CUSTOMER
headquarters cities of the WHERE HQCITY=’New York’
customers that are headquartered AND CUSTNUM>1500;
in New York and that have a
customer number higher than
1500.”
¨ With the AND operator, both conditions
have to be satisfied to be included in the
result.
4-23
General Hardware Company SQL Query
Example: OR
“List the customer numbers, SELECT CUSTNUM, CUSTNAME, HQCITY
customer names, and FROM CUSTOMER
headquarters cities of the WHERE HQCITY=’New York’
customers that are headquartered OR CUSTNUM>1500;
in New York or that have a
customer number higher than
1500.”
¨ The OR operator really means one
or the other or both.
4-24
General Hardware Company SQL Query
Example: AND, OR
¨ AND is said to be “List the customer numbers,
customer names, and
“higher in headquarters cities of the
customers that are headquartered
precedence” than in New York or that satisfy the two
OR. conditions of having a customer
number higher than 1500 and
being headquartered in Atlanta.”
¨ So all ANDs are
considered before
any ORs are
considered.
4-25
General Hardware Company SQL Query
Example: AND, OR
“List the customer numbers, SELECT CUSTNUM, CUSTNAME, HQCITY
customer names, and FROM CUSTOMER
headquarters cities of the WHERE HQCITY=’New York’
customers that are headquartered OR CUSTNUM>1500
in New York or that satisfy the two AND HQCITY=’Atlanta’;
conditions of having a customer
number higher than 1500 and
being headquartered in Atlanta.”
4-26
General Hardware Company SQL Query
Example: AND, OR
¨ If you really SELECT CUSTNUM, CUSTNAME, HQCITY
FROM CUSTOMER
wanted the OR WHERE (HQCITY=’New York’
OR CUSTNUM>1500)
to be AND HQCITY=’Atlanta’;
considered
first, you could
force it by
writing the
query as:
4-27
General Hardware Company SQL Query
Example: BETWEEN
SELECT *
“Find the customer records for
FROM CUSTOMER
those customers whose customer
WHERE (CUSTNUM>=1000 AND
numbers are between 1000 and
CUSTNUM<=1700);
1700, inclusive.”
SELECT *
FROM CUSTOMER
WHERE CUSTNUM BETWEEN 1000 AND 1700;
¨ Allows you to specify a range of
numeric values in a search.
4-28
General Hardware Company SQL Query
Example: IN
“Find the customer records for SELECT *
those customers headquartered FROM CUSTOMER
in Atlanta, Chicago, or WHERE (HQCITY=’Atlanta’
Washington.” OR HQCITY=’Chicago’
OR HQCITY=’Washington’);
SELECT *
FROM CUSTOMER
WHERE HQCITY IN (‘Atlanta’, ‘Chicago’,
‘Washington’);
4-29
General Hardware Company SQL Query
Example: LIKE
“Find the customer records for SELECT *
those customers whose names FROM CUSTOMER
begin with the letter “A”.” WHERE CUSTNAME LIKE ‘A%’;
¨ “%” character used as a “wildcard” to
represent any string of characters.
4-30
General Hardware Company SQL Query
Example: LIKE
“Find the customer records for SELECT *
those customers whose names FROM CUSTOMER
have the letter “a” as the second WHERE CUSTNAME LIKE ‘_a%’;
letter of their names.”
¨ The single “_” character in the operator
LIKE “_a%” specifies that there will be one
character followed by “a.”
4-31
Example: NULL Search Condition
List details of all viewings on property PG4 where a
comment has not been supplied.
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;
Slide 46 (of 116)
Example: NULL Search Condition
•Negated version (IS NOT NULL) can test for non-
null values.
Slide 47 (of 116)
General Hardware Company SQL Query
Example: DISTINCT
“Which cities serve as SELECT HQCITY
headquarters cities for General FROM CUSTOMER;
Hardware customers?”
SELECT DISTINCT HQCITY
FROM CUSTOMER;
¨ Eliminate duplicate
rows in a query
result.
4-34
Example: Calculated Fields
Produce a list of monthly salaries for all staff, showing
staff number, first and last names, and salary details.
SELECT staffNo, fName, lName, salary/12
FROM Staff;
Slide 30 (of 116)
Example: Calculated Fields
Slide 31 (of 116)
General Hardware Company SQL Query
Example: ORDER BY
“Find the customer numbers, SELECT CUSTNUM, CUSTNAME, HQCITY
customer names, and headquarters FROM CUSTOMER
cities of those customers with WHERE CUSTNUM>1000
customer numbers greater than ORDER BY HQCITY;
1000. List the results in alphabetic
order by headquarters cities.”
¨ Orders the results of an SQL query by one
or more specified attributes.
4-37
General Hardware Company SQL Query
Example: ORDER BY
The default order for ORDER BY is ascending.
The clause can include the term ASC at the end to make
ascending explicit.
The clause can include DESC for descending order.
4-38
General Hardware Company SQL Query
Example: AVG
“Find the average number of SELECT AVG(QUANTITY)
units of the different products FROM SALES
that Salesperson 137 has sold WHERE SPNUM=137;
(i.e., the average of the quantity
values in the first three records of
the SALES table).”
¨ AVG is a built-in function of SQL.
4-39
General Hardware Company SQL Query
Example: SUM
“Find the total number of all SELECT SUM(QUANTITY)
products that Salesperson 137 FROM SALES
has sold.” WHERE SPNUM=137;
¨ SUM is a built-in function of SQL.
4-40
General Hardware Company SQL Query
Example: MAX
“What is the largest number of SELECT MAX(QUANTITY)
units of Product Number 21765 FROM SALES
that any individual salesperson WHERE PRODNUM=21765;
has sold?”
¨ MAX is a built-in function of SQL.
4-41
General Hardware Company SQL Query
Example: MIN
“What is the smallest number of SELECT MIN(QUANTITY)
units of Product Number 21765 FROM SALES
that any individual salesperson WHERE PRODNUM=21765;
has sold?”
¨ MIN is a built-in function of SQL.
4-42
General Hardware Company SQL Query
Example: COUNT
“How many salespersons have SELECT COUNT(*)
sold Product Number 21765?” FROM SALES
WHERE PRODNUM=21765;
¨ COUNT counts the number of rows that
satisfy a set of criteria.
4-43
General Hardware Company SQL Query
Example: GROUP BY
“Find the total number of units SELECT SPNUM, SUM(QUANTITY) FROM
of all products sold by each SALES
salesperson.” GROUP BY SPNUM;
¨ Used when calculations are to be made on
several different groups of rows.
4-44
General Hardware Company SQL Query
Example: HAVING
“Find the total number of units of SELECT SPNUM, SUM(QUANTITY) FROM
all products sold by each SALES
salesperson whose salesperson WHERE SPNUM>=150
number is at least 150. Only include GROUP BY SPNUM
salespersons whose total number of HAVING SUM(QUANTITY)>=5000;
units sold is at least 5000.”
¨ HAVING limits the results of a GROUP BY
based on the values calculated for each
group with the built-in functions.
4-45
The Join
SQL SELECT allows you to join two or more tables.
Two specifications must be made in the SELECT statement.
The tables to be joined must be listed in the FROM clause.
The join attributes in the tables being joined must be declared and
matched to each other in the WHERE clause.
A table name qualifier is required when different tables have an
attribute with the same name.
4-46
General Hardware Company SQL Query
Example: Join
“Find the name of the salesperson responsible for Customer Number 1525.”
SELECT SPNAME
FROM SALESPERSON, CUSTOMER
WHERE SALESPERSON.SPNUM=CUSTOMER.SPNUM AND
CUSTNUM=1525;
4-47
General Hardware Company SQL Query
Example: Join
“List the names of the products of which salesperson Adams has sold more than 2,000
units.”
SELECT PRODNAME
FROM SALESPERSON, PRODUCT, SALES
WHERE SALESPERSON.SPNUM=SALES.SPNUM
AND SALES.PRODNUM=PRODUCT.PRODNUM
AND SPNAME=’Adams’
AND QUANTITY>2000;
4-48
Subqueries
One SELECT statement is “nested” within another.
Nesting can go on through several levels of SELECT
statements with each successive SELECT statement
contained in a pair of parentheses.
The innermost SELECT statement is executed first,
and its results are then provided as input to the
SELECT statement at the next level up.
4-49
General Hardware Company SQL Query
Example: Subquery
“Find the name of the salesperson responsible for Customer Number 1525.”
SELECT SPNAME
FROM SALESPERSON
WHERE SPNUM=
(SELECT SPNUM
FROM CUSTOMER
WHERE CUSTNUM=1525);
¨ Subquery as an alternative to join.
4-50
General Hardware Company SQL Query
Example: Subquery
“Which salespersons with salesperson numbers greater than 200 have the lowest
commission percentage of any such salesperson?” (We’ll identify salespersons by their
salesperson number.)
SELECT SPNUM ¨ A subquery is
FROM SALESPERSON
WHERE SPNUM>200
required.
AND COMMPERCT=
(SELECT MIN(COMMPERCT)
FROM SALESPERSON)
WHERE SPNUM>200);
4-51
A Strategy for Writing SQL SELECT
Commands
Determine what the result of the query is to be and write the
needed attributes and functions in the SELECT clause.
Determine which tables of the database will be needed for the
query and write their names in the FROM clause.
If the query involves a join, begin constructing the WHERE
clause by equating the join attributes from the tables that are in
the FROM clause.
Continue filling in the details of the WHERE clause, the
GROUP BY clause, and any subqueries.
4-52
Example - Good Reading Bookstores
4-53
Sample Queries
“Find the book number, book name, and number of pages of all of the books published
by London Publishing Ltd. List the results in order by book name.”
SELECT BOOKNUM, BOOKNAME, PAGES FROM BOOK
WHERE PUBNAME=’London Publishing Ltd.’ ORDER BY
BOOKNAME;
4-54
Sample Queries
“How many books of at least 400 pages does Good Reading Bookstores carry that
were published by publishers based in Paris, France?”
SELECT COUNT(*)
FROM PUBLISHER, BOOK
WHERE PUBLISHER.PUBNAME=BOOK.PUBNAME AND CITY=’Paris’
AND COUNTRY=’France’
AND PAGES>=400;
4-55
Sample Queries
“List the publishers in Belgium, Brazil, and Singapore that publish books written by
authors who were born before 1920.”
SELECT DISTINCT PUBNAME
FROM PUBLISHER, BOOK, WRITING, AUTHOR
WHERE PUBLISHER.PUBNAME=BOOK.PUBNAME
AND BOOK.BOOKNUM=WRITING.BOOKNUM
AND WRITING.AUTHORNUM=AUTHOR.AUTHORNUM AND COUNTRY IN
(‘Belgium’, ‘Brazil’, ‘Singapore’)
AND YEARBORN<1920;
4-56
Sample Queries
“How many books did each publisher in Oslo, Norway; Nairobi, Kenya; and
Auckland, New Zealand, publish in 2001?”
SELECT PUBNAME, CITY, COUNTRY, COUNT(*)
FROM PUBLISHER, BOOK
WHERE PUBLISHER.PUBNAME=BOOK.PUBNAME
AND ((CITY=’Oslo’ AND COUNTRY=’Norway’)
OR (CITY=’Nairobi’ AND COUNTRY=’Kenya’)
OR (CITY=’Auckland’ AND COUNTRY=’New Zealand’)) AND PUBYEAR=2001
GROUP BY PUBNAME;
4-57
Sample Queries
“Which publisher published the book that has the earliest publication year among all
of the books that Good Reading Bookstores carries?”
SELECT DISTINCT PUBNAME
FROM BOOK
WHERE PUBYEAR=
(SELECT MIN(PUBYEAR)
FROM BOOK);
4-58
Example - World Music Association
4-59
Sample Queries
“What is the total annual salary cost for all of the violinists of the Berlin Symphony
Orchestra?”
SELECT SUM(ANNSALARY)
FROM MUSICIAN
WHERE ORCHNAME=’Berlin Symphony Orchestra’
AND INSTRUMENT=’Violin’;
4-60
Sample Queries
“Make a single list, in alphabetic order of all of the universities attended by the
cellists of India.”
SELECT DISTINCT UNIVERSITY
FROM ORCHESTRA, MUSICIAN, DEGREE
WHERE ORCHESTRA.ORCHNAME=MUSICIAN.ORCHNAME
AND MUSICIAN.MUSNUM=DEGREE.MUSNUM
AND INSTRUMENT=’Cello’
AND COUNTRY=’India’
ORDER BY UNIVERSITY;
4-61
Sample Queries
“What is the total annual salary cost for all of the violinists of each orchestra located
in Canada? Only include in the result those orchestras whose total annual salary for
its violinists is in excess of $150,000.”
SELECT ORCHNAME, SUM(ANNSALARY)
FROM ORCHESTRA, MUSICIAN
WHERE ORCHESTRA.ORCHNAME=MUSICIAN.ORCHNAME AND
COUNTRY=’Canada’
AND INSTRUMENT=’Violin’
GROUP BY ORCHNAME
HAVING SUM(ANNSALARY)>150,000;
4-62
Sample Queries
“What is the name of the most highly paid pianist?”
SELECT MUSNAME
FROM MUSICIAN
WHERE INSTRUMENT=’Piano’
AND ANNSALARY=
(SELECT MAX(ANNSALARY)
FROM MUSICIAN
WHERE INSTRUMENT=’Piano’);
4-63
Sample Queries
“What is the name of the most highly paid pianist of any orchestra in Australia?”
SELECT MUSNAME
FROM MUSICIAN, ORCHESTRA
WHERE MUSICIAN.ORCHNAME=ORCHESTRA.ORCHNAME
AND INSTRUMENT=’Piano’
AND COUNTRY=’Australia’
AND ANNSALARY=
(SELECT MAX(ANNSALARY)
FROM MUSICIAN, ORCHESTRA
WHERE MUSICIAN.ORCHNAME=ORCHESTRA.ORCHNAME
AND INSTRUMENT=’Piano’
4-64
AND COUNTRY=’Australia’);
Example - Lucky Rent-A-Car
4-65
Sample Queries
“List the manufacturers whose names begin with the letter C or the letter D and that
are located in Japan.”
SELECT MANUFNAME
FROM MANUFACTURER
WHERE (MANUFNAME LIKE ‘C%’
OR MANUFNAME LIKE ‘D%’)
AND COUNTRY=’Japan’;
4-66
Sample Queries
“What was the average mileage of the cars that had tune-ups in August 2003?”
SELECT AVG(MILEAGE)
FROM MAINTENANCE
WHERE PROCEDURE=’Tune-Up’
AND DATE BETWEEN ‘AUG-01-2003’ AND ‘AUG-31-2003’;
4-67
Sample Queries
“How many different car models do manufacturers in Italy make?”
SELECT COUNT(DISTINCT MODEL)
FROM MANUFACTURER, CAR
WHERE MANUFACTURER.MANUFNAME=CAR.MANUFNAME
AND COUNTRY=’Italy’;
4-68
Sample Queries
“How many repairs were performed on each car manufactured by Superior Motors
during the month of March 2004? Only include cars in the result that had at least
three repairs.”
SELECT CAR.CARNUM, COUNT(*)
FROM CAR, MAINTENANCE
WHERE CAR.CARNUM=MAINTENANCE.CARNUM
AND MANUFNAME=’Superior Motors’
AND DATE BETWEEN ‘MAR-01-2004’ AND ‘MAR-31-2004’ GROUP BY
CAR.CARNUM
HAVING COUNT(*)>=3;
4-69
Sample Queries
“List the cars of any manufacturer that had an oil change in January 2004 and had at
least as many miles as the highest mileage car manufactured by Superior Motors that
had an oil change that same month.”
SELECT MAINTENANCE.CARNUM
FROM MAINTENANCE
WHERE PROCEDURE=’Oil Change’
AND DATE BETWEEN ‘JAN-01-2004’ AND ‘JAN-31-2004’
AND MILEAGE>=
(SELECT MAX(MILEAGE)
FROM CAR, MAINTENANCE
WHERE CAR.CARNUM, MAINTENANCE.CARNUM
AND PROCEDURE=’Oil Change’
AND DATE BETWEEN ‘JAN-01-2004’ AND ‘JAN-31-2004
AND MANUFNAME=’Superior Motors’);
4-70
Q&A
5-71