Query Formulation With SQL
Query Formulation With SQL
Example 2 (Access)
SELECT *
FROM Faculty
WHERE FacSSN = '543210987'
Example 3
SELECT FacFirstName, FacLastName, FacSalary
FROM Faculty
Example 4
SELECT FacFirstName, FacLastName, FacSalary
FROM Faculty
WHERE FacSalary > 65000 AND FacRank = 'PROF'
Using Expressions
Example 5 (Access)
SELECT FacFirstName, FacLastName, FacCity,
FacSalary*1.1 AS IncreasedSalary,
FacHireDate
FROM Faculty
WHERE year(FacHireDate) > 1991
Example 5 (Oracle)
SELECT FacFirstName, FacLastName, FacCity,
FacSalary*1.1 AS IncreasedSalary,
FacHireDate
FROM Faculty
WHERE to_number(to_char(FacHireDate, 'YYYY'))
> 1991
Inexact Matching
• Match against a pattern: LIKE operator
• Use meta characters to specify patterns
– Wildcard (* or %)
– Any single character (? or _)
Example 6 (Access)
SELECT *
FROM Offering
WHERE CourseNo LIKE 'IS*'
Example 6 (Oracle)
SELECT *
FROM Offering
WHERE CourseNo LIKE 'IS%'
Using Dates
• Dates are numbers
• Date constants and functions are not standard
Example 7 (Access)
SELECT FacFirstName, FacLastName, FacHireDate
FROM Faculty
WHERE FacHireDate BETWEEN #1/1/1994#
AND #12/31/1995#
Example 7 (Oracle)
SELECT FacFirstName, FacLastName, FacHireDate
FROM Faculty
WHERE FacHireDate BETWEEN '1-Jan-1994'
AND '31-Dec-1995'
Other Single Table Examples
Example 8: Testing for null values
SELECT OfferNo, CourseNo
FROM Offering
WHERE FacSSN IS NULL AND OffTerm = 'SUMMER'
AND OffYear = 2003
Faculty
FacSSN FacName
111-11-1111 joe Natural Join of Offering and
222-22-2222 sue Faculty
333-33-3333 sara FacSSN FacName OfferNo
111-11-1111 joe 1111
Example 10 (Access)
SELECT OfferNo, CourseNo, FacFirstName,
FacLastName
FROM Offering, Faculty
WHERE OffTerm = 'FALL' AND OffYear = 2002
AND FacRank = 'ASST' AND CourseNo LIKE 'IS*'
AND Faculty.FacSSN = Offering.FacSSN
Join Operator Style
• Use INNER JOIN and ON keywords
• FROM clause contains join operations
Example 11 (Access)
SELECT OfferNo, CourseNo, FacFirstName,
FacLastName
FROM Offering INNER JOIN Faculty
ON Faculty.FacSSN = Offering.FacSSN
WHERE OffTerm = 'FALL' AND OffYear = 2002
AND FacRank = 'ASST' AND CourseNo LIKE 'IS*'
Name Qualification
Ambiguous column reference
– More than one table in the query contains a
column referenced in the query
– Ambiguity determined by the query not the
database
Use column name alone if query is not
ambiguous
Qualify with table name if query is
ambiguous
Summarizing Tables
Row summaries important for decision-making tasks
Row summary
– Result contains statistical (aggregate) functions
– Conditions involve statistical functions
SQL keywords
– Aggregate functions in the output list
– GROUP BY: summary columns
– HAVING: summary conditions
GROUP BY Examples
Example 12: Grouping on a single column
SELECT FacRank, AVG(FacSalary) AS AvgSalary
FROM Faculty
GROUP BY FacRank
Example 14: List the number of students enrolled in each fall 2003
offering.
SELECT Offering.OfferNo,
COUNT(*) AS NumStudents
FROM Enrollment, Offering
WHERE Offering.OfferNo = Enrollment.OfferNo
AND OffYear = 2003
GROUP BY Offering.OfferNo
Conceptual Evaluation Process
FROM Tables:
Cross Product and 1
Join Operations
Restriction
on WHERE 2
Conditions
Compute
Sort on Aggregates Restriction
GROUP
GROUP BY and Reduce on HAVING
BY? Yes Columns Each Group Conditions
to 1 Row
No 3 5
4
ORDER
BY?
Yes Sort
Columns in 6
No ORDER BY
Project 7
Columns in
SELECT
Finish
Conceptual Evaluation Lessons
Row operations before group operations
– FROM and WHERE before GROUP BY and
HAVING
– Check row operations first
Grouping occurs only one time
Use small sample tables
Conceptual Evaluation Problem
• Important practice
•Use the university database tables in Chapter 3
Problem
Problem Database Database Language
Statemen
Statemen Representation Statement
tt
Critical Questions
What tables?
– Columns in output
– Conditions to test (including join conditions)
How to combine the tables?
– Usually join PK to FK
– More complex ways to combine
Individual rows or groups of rows?
– Aggregate functions in output
– Conditions with aggregate functions
Efficiency Considerations
Little concern for efficiency
Intelligent SQL compilers
Correct and non redundant solution
– No extra tables
– No unnecessary grouping
– Use HAVING for group conditions only
Advanced Problems
Joining multiple tables
Self joins
Grouping after joining multiple tables
Traditional set operators
Joining Three Tables
Example 16: List Leonard Vince’s teaching schedule in fall 2002.
For each course, list the offering number, course number, number of
units, days, location, and time.
A UNION B
A INTERSECT B
A MINUS B
Union Compatibility
Requirement for the traditional set
operators
Strong requirement
– Same number of columns
– Each corresponding column is compatible
– Positional correspondence
Apply to similar tables by removing
columns first
SQL UNION Example
Example 21: Retrieve basic data about all university people
UPDATE Student
SET StdMajor = 'ACCT',
StdClass = 'SO'
WHERE StdFirstName = 'HOMER'
AND StdLastName = 'WELLS'
DELETE Example
Example 26: Delete all IS majors who are seniors.