Lecture 03.3 SQL - QueryOneTableWithFilters2 - 28
Lecture 03.3 SQL - QueryOneTableWithFilters2 - 28
Information
SQL BASICS – ADDING FILTERS TO SQL QUERIES
1 / 28
Filtering Information Using WHERE Clause
• Use a WHERE clause in a SELECT statement to filter the data the
statement draws from a table
• The WHERE clause contains a search condition that it uses as the filter
• This search condition provides the mechanism needed to select only the rows
you need or exclude the ones you don’t want
• Your database system applies the search condition to each row in the logical
table defined by the FROM clause
• A search condition contains one or more predicates, each of which is an
expression that tests one or more value expressions and returns a true,
false, or unknown answer
• You can combine multiple predicates into a search condition using AND or OR
Boolean operators
• When the entire search condition evaluates to true for a particular row, you
will see that row in the final result set.
• Note that when a search condition contains only one predicate, the terms
search condition and predicate are synonymous
• When you construct a predicate, you will typically include at least one value
expression that refers to a column from the tables you specify in the FROM
clause
• The simplest and perhaps most commonly used predicate compares one value
expression (a column) to another (a literal)
2 / 28
Predicates in a WHERE clause
Consider SQL: SELECT CustLastName FROM Customers WHERE CustLastName = 'Smith’
• The predicate in the WHERE clause is equivalent to asking this question for each row in the Customers table: “Does the
customer last name equal ‘Smith’?”
• When the answer to this question is yes (true) for any given row in the Customers table, that row appears in the result set
Five Basic Predicates in the SQL Standard:
1. COMPARISON: Use one of the six comparison operators to compare one value expression to another value expression. The six
operators and their meanings are
• = equal to
• <> not equal to
• < less than
• > greater than
• <= less than or equal to
• >= greater than or equal to
2. BETWEEN (RANGE): Enables you to test whether the value of a given value expression falls within a specified range of values.
You specify the range using two value expressions separated by the AND keyword.
3. IN (MEMBERSHIP): Enables you to test whether the value of a given value expression matches an item in a given list of values
4. LIKE (PATTERN MATCH): Enables you to test whether a character string value expression matches a specified character string
pattern
5. IS NULL: Enables you to determine whether a value expression evaluates to Null
• There are 13 other predicates defined by the SQL Standard:
• Similar, Regex, Unique, Normalized, Match, Overlaps, Distinct, Member, Submultiset, Set, and Type – they aren’t been used
commercially/widely
• Quantified is used rarely
• EXISTS is widely used and will be discussed later
3 / 28
Using a WHERE Clause
• Consider a request: What are the names of our customers who live in the state of Washington?
• When composing a translation statement for this type of request, you must try to indicate the information you want
to see in the result set as explicitly and clearly as possible
• You’ll expend more effort to rephrase a request than you’ve been accustomed to so far, but the results will be well worth the extra
work
• Translation: Select first name and last name from the customers table for those customers who live in Washington State
• Perform cleanup as usual, also perform two extra tasks
• First, look for any words or phrases that indicate or imply some type of restriction. Dead giveaways are the words “where,” “who,” and “for.”
• “. . . who live in Bellevue.”
• “. . . for everyone whose ZIP Code is 98125.”
• “. . . who placed orders in May.”
• “. . . for Mike Hernandez.”
• Study the phrase, and try to determine which column is going to be tested, what value that column is going to be tested against, and
how the column is going to be tested - this will help formulate the search condition for the WHERE clause
• You need to be familiar with the structure of the table you’re using to answer the request; if necessary, have a copy of the table
structure handy before you begin to answer these questions
• Which column is going to be tested? State
• What value is it going to be tested against? ‘WA’
• How is the column going to be tested? Using the “equal to” operator
• Sometimes the answers to these questions are evident, and other times the answers are implied
4 / 28
Using a WHERE Clause, continued…
• Create the appropriate search condition using the answers
• Next, cross out the original restriction, and replace it with the word WHERE and the search condition you
just created
• Translation: Select first name and last name from the customers table for those customers who live in
Washington State
• Clean up: Select first name and last name from the customers table for those customers who live in where state is
equal to = ‘WA’ Washington State
• The result set of the completed SELECT statement will display only those customers who live in the state of
Washington
• It’s simply a matter of creating the appropriate search condition and placing it in the WHERE clause; the real work,
however, is in defining the search conditions
5 / 28
Defining Search Conditions (Predicates)
• COMPARISON:
• The most common type of condition is one that uses a comparison predicate to compare two value expressions to each other
• Use one of the six comparison operators to compare one value expression to another value expression. The six operators and
their meanings are:
• = equal to
• <> not equal to
• < less than
• > greater than
• <= less than or equal to
• >= greater than or equal to
• Comparing strings are a bit trickier than comparing numeric or datetime data
• For example, you might not get the results you expect when you compare two seemingly similar strings such as “Mike” and “MIKE”
• The determining factor for all character string comparisons is the collating sequence used by your database system. The collating sequence also
determines how character strings are sorted and impacts how you use other comparison conditions as well.
• SQL Standard does not define any default collating sequence for character string sorting or comparison
• How characters are sorted from “lowest” to “highest” depends on the database software you are using and, in many cases, how the software
was installed
• Many database systems use the ASCII collating sequence, which places numbers before letters and all uppercase letters before all
lowercase letters. If your database supports the ASCII collating sequence, the characters are in the following sequence from lowest value
to highest value:
• . . . 0123456789 . . . ABC . . . XYZ . . . abc . . . xyz . . .
• Check your database system’s documentation to determine how it collates uppercase letters, lowercase letters, and numbers
6 / 28
Comparison Predicates (Continued..)
Equality and Inequality
• Consider: Show me the first and last names of all the agents who were hired on March 14, 1977
• Because we are going to search for a specific hire date, we can use an equality comparison condition with an “equal to”
operator to retrieve the appropriate information
• Translation: Select first name and last name from the agents table for all agents hired on March 14, 1977
• Clean Up:
• SQL: SELECT AgtFirstName, AgtLastName FROM Agents WHERE DateHired = '1977-03-14'
• We executed an inclusive process—a given row in the Agents table will be included in the result set only if the current value of
the DateHired column for that row matches the specified date
• Consider: Give me a list of vendor names and phone numbers for all our vendors, with the exception of those here in
Bellevue
• We will need to exclude those vendors based in Bellevue and that you’ll use a “not equal to” condition for the task
• The phrase “with the exception of” provides a clear indication that the “not equal to” condition is appropriate
• Translation: Select vendor name and phone number from the vendors table for all vendors except those based in ‘Bellevue’
• Clean Up:
• SQL: SELECT VendName, VendPhone FROM Vendors WHERE VendCity <> 'Bellevue'
7 / 28
Comparison Predicates (Continued..)
Less Than and Greater Than
• Used you want rows returned where a particular value in a column is smaller or larger than the comparison value
• The type of data you compare determines the relationship between those values
• Look for phrases such as ‘earlier than’, ‘more than’, ‘since’, ‘before’, ‘after’, ‘equal to or less’, etc.
• Replace those phrases with the appropriate ‘Less Than’ or ‘Greater Than’ predicate for comparison
• Character Strings Comparison: Determines whether the value of the first value expression precedes (<) or follows (>)
the value of the second value expression in your database system’s collating sequence. For example, you can
interpret a < c as “Does a precede c?”
• Numbers Comparison: Determines whether the value of the first value expression is smaller (<) or larger (>) than the
value of the second value expression. For example, you can interpret 10 > 5 as “Is 10 larger than 5?”
• Dates/Times Comparison: Determines whether the value of the first value expression is earlier (<) or later (>) than
the value of the second value expression. For example, you can interpret ‘2007-05-16’ < ‘2007-12-15’ as “Is May 16,
2007, earlier than December 15, 2007?”
• Dates and times are evaluated in chronological order
8 / 28
The ‘Between’ Predicate
• Test the value of a value expression against a specific range of values with a range condition using
BETWEEN …AND…
• Predicate defines the range by using the value of the second value expression as the start point and the value of the
third value expression as the end point
• Both the start point and end point are part of the range. A row is included in the result set only if the value of the
first value expression falls within the specified range
• If start value expression is ‘greater than’ end value expression, some database software can error out
• For translation, look for words or phrases relating to range, or nouns relating to time (month, day, year, etc.), ‘begin
with’, ‘end with’
9 / 28
Membership (IN) Predicate
• Use the membership condition to test the value of a value expression against a list of explicitly defined values
• The membership condition uses the IN predicate to determine whether the value of the first value expression
matches any value within a parenthetical list of values defined by one or more value expressions
• Although theoretically you can include an almost limitless number of value expressions in the list, you can use the
membership condition most effectively when you define a finite list of values
• Consider: Which entertainers do we represent in Seattle, Redmond, and Bothell?
• This type of request lends itself to a membership condition because it focuses on searching for a specific set of values. If the
request were not so explicit, you would most likely use a range condition instead
• Translation: Select stage name from the entertainers table for all entertainers based in ‘Seattle’, ‘Redmond’, or ‘Bothell’
• Clean Up:
• SQL: SELECT EntStageName FROM Entertainers WHERE EntCity IN (‘Seattle', ‘Redmond', ‘Bothell')
• Notice that we used the word “or” in the translation statement’s list of cities instead of “and” as it appears in the original
request
• There is only one entry in the EntCity column for a given entertainer. A given row can’t contain Seattle and Redmond and Bothell all at the same time, but a
single row could contain Seattle or Redmond or Bothell
• Using the proper words and phrases helps to clarify your Translation and Clean Up statements and ensures that you define the most appropriate SELECT
statement for your request
10 / 28
Pattern Matching using LIKE Predicate
• Useful when you need to find values that are similar to a given pattern string or when you have only a partial
piece of information to use as a search criterion
• Takes the value of a value expression and uses the LIKE predicate to test whether the value matches a defined
pattern string
• A pattern string can consist of any logical combination of regular string characters and two special wildcard characters: the
percent sign (%) and the underscore (_)
• The percent sign represents zero or more arbitrary regular characters, and the underscore represents a single arbitrary regular character
• The manner in which you define the pattern string determines which values are retrieved from the value expression
• Although you can search for any pattern string using the appropriate wildcard characters, you’ll run into a
problem if the values you want to retrieve include a percent sign or an underscore character
• For example, you will have a problem trying to retrieve the value MX_445 because it contains an underscore character.
• You can circumvent this potential dilemma by using the ESCAPE option of the LIKE predicate
• Consider: “Show me a list of products that have product codes beginning with ‘G_00’ and ending in a single number or
letter.
• SQL: SELECT ProductName, ProductCode FROM Products WHERE ProductCode LIKE 'G\_00_' ESCAPE '\'
11 / 28
Pattern Matching using LIKE Predicate
• Give me a list of customers whose last names begin with
‘Mar’.
• Requests such as this one typically use phrases that indicate
the need for a pattern match condition
• “. . . begin with ‘Her’.”
• “. . . start with ‘Ba’.”
• “. . . include the word ‘Park’.”
• “. . . contain the letters ‘han’.”
• “. . . have ‘ave’ in the middle of it.”
• “. . . with ‘son’ at the end.”
• “. . . ending in ‘ez’.”
• String comparison is case sensitive (generally)
• Translation: Select last name and first name from the
customers table where the last name begins with ‘Mar’
• Clean up:
12 / 28
Searching for Unknown/Missing Values
• A Null does represent a missing or unknown value
• It does not represent a zero, a character string of one or more blank spaces, or a zero-length character string (a
character string that has no characters in it) because each of these items can be meaningful in a variety of
circumstances
• To retrieve Null values from a value expression, you use the Null condition
• This condition takes the value of the value expression and determines whether it is Null using the IS NULL predicate
• Consider: “Give me a list of customers who didn’t specify what county they live in.”
• Translation: Select first name and last name as Customer from the customers table where the county name is unspecified
• Clean Up: Select first name || ' ' || and last name as Customer from the customers table where the county name is null unspecified
• SQL: SELECT CustFirstName || ' ' || CustLastName AS Customer FROM Customers WHERE CustCounty IS NULL
• You must use the Null condition to search for Null values within a value expression
• A condition such as <ValueExpression> = Null is invalid because the value of the value expression cannot be compared to something that is, by
definition, unknown
• In fact, using Null in any comparison predicate yields “unknown,” and because unknown is not “true,” the comparison will fail
13 / 28
Searching for Unknown/Missing Values
• Consider: “Which engagements do not yet have a contract price?”
• Translation: Select engagement number and contract price from the engagements table for any engagement that does
not have a contract price
• Clean Up: Select engagement number and contract price from the engagements table for any engagement that does
not have a where contract price is null
• SQL: SELECT EngagementNumber, ContractPrice FROM Engagements WHERE ContractPrice IS NULL
• On the surface, this seems like a straightforward request—you’ll just search for any engagement that has 0 as
the contract price.
• But looks can be deceiving, and they can lull you into making incorrect assumptions
• If the entertainment agency in this example uses 0 as the contract price for any promotional engagement, then zero is
a valid, meaningful value
• Therefore, any contract price that is yet to be determined or negotiated is indeed (or should be) Null
14 / 28
An Important Point About Working with
Data
• You do need to understand your data in order to make meaningful, accurate requests to the
database
• If you execute a SELECT statement and then think that the information you see in a result set is
erroneous, don’t panic
• Your first impulse will probably be to rewrite the entire SELECT statement because you believe you’ve
made some disastrous mistake in the syntax
• Before you do anything drastic, review the data you’re working with, and make certain you have a
clear idea of how it’s being used
• After you have a better understanding of the data, you’ll often find that you need to make only minor
changes to your SELECT statement in order for it to retrieve the proper information
15 / 28
Excluding Rows with ‘NOT’
• Exclude rows from a result set using ‘NOT’ operator
• This operator is an optional component of the BETWEEN, IN,
LIKE, and IS NULL predicate
• A SELECT statement will disregard any rows that meet the
condition expressed by any of these predicates when you
include the NOT operator
• The rows that will be in the result set instead are those that
did not meet the condition
• The types of phrases you’ll encounter are similar to those
listed here:
• “. . . that don’t begin with ‘Her’.”
• “. . . that aren’t in the Administrative or Personnel departments.”
• “. . . who have a fax number.”
• “. . . who were hired before June 1 or after August 31.”
• Sometimes, exclusion is implied
• For eg.: exclude everyone who does not have a fax number
16 / 28
The NOT operator: An Example
• Consider: “Show me a list of all the orders we’ve taken, except for those posted in October”
• Translation Select order # and order date from the orders table where the order date does not fall between
October 1, 2012, and October 31, 2012
• Clean Up: Select order # and order date from the orders table where the order date does not fall between
October 1, 2012, ‘2012-10-01’ and October 31, 2012 ‘2012-10-31’
• SQL: SELECT OrderNumber, OrderDate FROM Orders WHERE OrderDate NOT BETWEEN '2012-10-01‘ AND
'2012- 10-31'
• This SELECT statement produces a result set that will not contain any orders posted between
October 1, 2012, and October 31, 2012
• It will, however, contain every other order in the Orders table
• You can further restrict the rows sent to the result set to only those orders taken in 2012 by using multiple
conditions – we will discuss this later
• Excluding rows from a result set becomes a relatively straightforward process after you get
accustomed to analyzing and rephrasing your requests as the situation dictates
• The real key is in being able to determine the type of condition you need to answer a given request
17 / 28
Using Multiple Search Conditions: ‘AND’
• You can combine two or more conditions by using the AND and OR operators
• The combined set of conditions constitutes a single search condition
• AND operator
• Use this operator when all the conditions in the combined set must be met in order for a row to be included in a result set
• Consider: “Give me the first and last names of customers who live
in Seattle and whose last names start with the letter ‘H’”
• Translation: Select first name and last name from the customers table
where the city is ‘Seattle’ and the last name begins with ‘H’
• Clean up: Select first name, last name from customers where city =
‘Seattle’ and last name like ‘H%’
18 / 28
Using ‘OR’ Can Be Tricky
• Determining whether to use an AND operator to combine conditions is relatively easy and straightforward
• However, determining whether to use an OR operator can be tricky
• Consider: “Show me a list of vendor names and phone numbers for all vendors based in Washington and
California”
• First impulse might be to use an AND operator because the condition seems obvious—you want vendors in Washington
and California
• However, a vendor will be based in either Washington or California because you can enter only one state value in the state
column for that vendor
• Translation: Select name, phone number, and state from the vendors table where the state is ‘WA’ or ‘CA’
• Clean up: Select name, phone number, state from vendors where state is ‘WA’ or ‘CA’
• SQL: SELECT VendName, VendPhoneNumber, VendState FROM Vendors WHERE VendState = 'WA' OR VendState = 'CA‘
• You cannot omit any clause, keyword, or defined term from the syntax unless it is explicitly defined as an optional item
• Thus, a condition such as WHERE VendState = 'WA' OR 'CA' is completely invalid because the DBMS evaluates the expression in strict left-to-right sequence
• VendState = ‘WA’ will be true or false; this result will be ‘ORed’ with literal value ‘CA’, resulting in an error (or incorrect result)
• You could use a membership condition such as WHERE VendState IN ('WA', 'CA') to answer this request
19 / 28
Using Multiple Search Conditions: ‘OR’
• You can combine two or more conditions by using the AND and OR operators
• The combined set of conditions constitutes a single search condition
• OR operator
• Use this operator when either of the conditions in the combined set can be met in order for a row to be included in a result set
• Consider: “I need the name, city, and state of every staff member
who lives in Seattle or is from the state of Oregon”
• Translation: Select first name, last name, city, and state from the staff
table where the city is ‘Seattle’ or the state is ‘OR’
• Clean up: Select first name, last name, city, and state from staff where
city = ‘Seattle’ or state = ‘OR’
20 / 28
Using ‘AND’ and ‘OR’ Together
• Consider: “I need the name and title of every professor or associate professor who was hired
on May 16, 1989”
• Translation: Select first name, last name, title, and date hired from the staff table where the title is
‘professor’ or ‘associate professor’ and the date hired is May 16, 1989
• Clean up: Select first name, last name, title, date hired from staff where title = ‘professor’ or title =
‘associate professor’ and date hired = ‘1989-05-16’
• SQL: SELECT StfFirstName, StfLastName, Title, DateHired FROM Staff WHERE (Title = 'Professor' OR
Title = 'Associate Professor') AND DateHired = '1989-05-16‘
• The two conditions combined with the OR operator are being treated as a single search
condition
• The key is in making sure to enclose the search condition within parentheses to make it
perfectly clear how the comparisons should be processed
21 / 28
Order of Precedence For Evaluating
Multiple Conditions
• By default, the database evaluates conditions from left to right
• When a search condition contains various types of single conditions, the database evaluates them in
a specific order based on the operator used in each condition
• Consider: SELECT CustFirstName, CustLastName, CustState,
CustZipCode FROM Orders WHERE CustLastName =
'Patterson‘ AND CustState = 'CA‘ OR CustZipCode LIKE '%9‘
• It’s difficult to determine the true intent of the search
condition because there are two ways you can interpret it.
• You’re looking for everyone named Patterson in the
state of California or anyone with a ZIP Code that
ends with a 9
• You’re specifically looking for everyone named
Patterson and anyone who lives in California or has a
ZIP Code that ends with a 9
• the first way is correct because the system should evaluate
AND before OR, but is hard to remember
• Avoid this ambiguity and make the search condition clearer
by using parentheses to combine and prioritize certain
conditions
• WHERE (CustLastName = 'Patterson' AND CustState =
'CA') OR CustZipCode LIKE '%9'
22 / 28
Tip: Less is better than More
• Select only those columns you need to fulfill the request, and make the search condition as specific
as you can so that your database processes the fewest rows possible
• When you need to use multiple conditions, make certain that the condition that excludes the most
rows from the result set is processed first so that your database can potentially find the answer
faster
SELECT CustomerID, OrderDate, ShipDate SELECT CustomerID, OrderDate, ShipDate
FROM Orders FROM Orders
WHERE ShipDate = OrderDate WHERE CustomerID = 1001
AND CustomerID = 1001 AND ShipDate = OrderDate
23 / 28
Tip: Check for Overlapping Ranges
• Consider: Show me the engagements that occur during the week of November 12, 2012, through
November 18, 2012
• Translation: Select engagement number, start date, and end date from the engagements table where start date is between
November 12, 2012, and November 18, 2012 and end date is between November 12, 2012, and November 18, 2012
• Cleanup:
24 / 28
Tip: Search Predicates and NULL values
• A predicate that evaluates a Null value can never be true; it can’t be false either!
The result of combining two predicate expressions with the The result of combining two predicate expressions with the OR
AND operator when either expression is Null (unknown) operator when either expression is Null (unknown)
If anyone is FALSE, result is FALSE If anyone is TRUE, result is TRUE
25 / 28
Tip: Using NOT (A=B) when A or B is
Unknown
◦ For a simple comparison predicate, A=B, If either A or B for a given row is the Null value, then the result of the
comparison is unknown
◦ Because the result is not true, the row won’t be selected.
◦ If A = B is not true, you might expect that NOT (A = B) would be true. No! This is unknown also
◦ Consider: Let me see the names and phone numbers of King County residents
whose last names are Hernandez
◦ SELECT CustFirstName, CustLastName, CustPhoneNumber FROM Customers
WHERE CustCounty = 'King‘ AND CustLastName = 'Hernandez'
◦ Consider: Show me the names of all staff members who are graduate counselors or
were hired on September 1, 2007
◦ SELECT StfLastName, StfFirstName FROM Staff
WHERE Title = 'Graduate Counselor‘ OR DateHired = '2007-09-01
◦ NULLs don’t necessarily have the same effect on conditions combined with OR, AND
26 / 28
Expressing Conditions in Different Ways
“Give me the name of every employee who Give me a list of customers whose last name begins with ‘H’
was hired in October 2007” CustLastName >= 'H' AND CustLastName <= 'HZ'
DateHired BETWEEN '2007-10-01' AND '2007-10-31' CustLastName BETWEEN 'H' AND 'HZ'
DateHired >= '2007-10-01' AND DateHired <= '2007-10-31’ CustLastName LIKE 'H%'
MONTH(DateHired) = 10 AND YEAR(DateHired) = 2007
“Show me the vendors who are based in Show me all the students who do not live in Seattle or
California, Oregon, or Washington” Redmond
VendState IN ('CA', 'OR', 'WA') StudCity <> 'Seattle' AND StudCity <> 'Redmond'
VendState = 'CA' OR VendState = 'OR' OR VendState = 'WA' StudCity NOT IN ('Seattle', 'Redmond')
NOT (StudCity = 'Seattle' OR StudCity = 'Redmond')
27 / 28
Sample SQL!
Show me all the orders for customer number 1001
Show me an alphabetized list of products with names that begin with ‘Dog’
Show me an alphabetical list of entertainers based in Bellevue, Redmond, or Woodinville
Show me all the engagements that run for four days
Show me an alphabetical list of all the staff members and their salaries if they make between
$40,000 and $50,000 a year
Show me a list of students whose last name is ‘Kennedy’ or who live in Seattle
List the ID numbers of the teams that won one or more of the first ten matches in Game 3
List the bowlers in teams 3, 4, and 5 whose last names begin with the letter ‘H’
List the recipes that have no notes
Show the ingredients that are meats (ingredient class is 2) but that aren’t chicken
28 / 28