Lesson01 Relational Data Retrieval SQL
Lesson01 Relational Data Retrieval SQL
4-2
Chapter Objectives
Write SQL SELECT commands that join relational
tables.
4-3
Data Management
Data Definition
Data Manipulation
4-4
Data Management:
Data Definition
Operationalized with a data definition language (DDL).
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
4-6
SQL
Structured Query Language
4-7
Building the Data Structure
Base tables - actual physical tables in which the data
will be stored on the disk.
4-8
Data Manipulation Operations
UPDATE - used for updating existing data.
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.
4-10
SELECT Statement
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM TableName [alias] [, ...]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
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.
4-14
General Hardware Company SQL Query
Example
4-15
General Hardware Company SQL Query
Example, *
The “*” indicates that all attributes of the selected row are to be
retrieved.
4-16
General Hardware Company SQL Query
Example
4-17
General Hardware Company SQL Query
Example, No WHERE
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, <
4-21
General Hardware Company SQL Query
Example, >=
4-22
General Hardware Company SQL Query
Example: AND
4-24
General Hardware Company SQL Query
Example: AND, OR
4-26
General Hardware Company SQL Query
Example: AND, OR
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;
4-28
General Hardware Company SQL Query
Example: IN
SELECT *
FROM CUSTOMER
WHERE HQCITY IN (‘Atlanta’, ‘Chicago’,
‘Washington’);
4-29
General Hardware Company SQL Query
Example: LIKE
¨ 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.
The clause can include the term ASC at the end to make
ascending explicit.
4-38
General Hardware Company SQL Query
Example: AVG
4-39
General Hardware Company SQL Query
Example: SUM
4-40
General Hardware Company SQL Query
Example: MAX
4-41
General Hardware Company SQL Query
Example: MIN
4-42
General Hardware Company SQL Query
Example: COUNT
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.
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);
“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.)
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.
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.”
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.”
4-56
Sample Queries
“How many books did each publisher in Oslo, Norway; Nairobi, Kenya; and
Auckland, New Zealand, publish in 2001?”
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?”
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.”
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.”
4-62
Sample Queries
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
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.”
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