Professional Documents
Culture Documents
Chapter 10
1
WHERE Clause
• WHERE clause is optional
– If used, it directly follows the FROM clause
SELECT *
FROM EMPLOYEES
WHERE department = 'IT';
• Restricts or limits rows returned from the table
• Contains a condition that must be met for a row to be returned
department = 'IT'
2
WHERE Clause
• The syntax for the WHERE clause is:
3
Comparison Operators in the WHERE Clause
• Comparison operators can be used to compare one expression
to a value or expression:
= equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
<> not equal to (or != or ^=)
4
WHERE clause
• All character searches are case-sensitive
• What will probably be returned with
5
Using WHERE clause for
Simple Search Condition
6
Using WHERE clause for
Simple Search Condition
The example might return any of the following results:
• No rows—If there are no customers in Chicago
• One row—If there is exactly one customer in Chicago
• Multiple rows—If there are two or more customers in Chicago
7
Using WHERE clause with a Primary Key
8
A Second Look: Restricting Columns and Rows
9
Column-List
10
WHERE Clause
11
Calculated Value in a WHERE Clause
12
Alias Name
Cannot be used in WHERE Clause
13
Processing Order of SQL Statement
14
Comparison Operators in the WHERE Clause
• Additional SQL comparison operators that add functionality for
retrieving specific sets of data:
16
BETWEEN…AND Operator
17
BETWEEN…AND Operator
• Both examples below are the same
• No performance benefit
• Simplicity in reading the code
18
NOT Keyword
with BETWEEN…AND Operator
19
IN Operator
• Returns rows that match a value in a specified list
• List must be in brackets/parentheses
• Values are separated by commas
20
IN Operator
21
Using OR
Instead of IN Operator
22
LIKE Operator
• Two symbols, called wildcard characters, are used to perform
string pattern matching
– Underscore (_) symbol represents one character
– Percent (%) symbol represents a match to a string of any length of
zero or more characters
• Allows the selection of rows that match either characters,
dates, or number patterns
23
LIKE Operator
Percent (%) Symbol
24
LIKE Operator
Percent (%) Symbol
25
LIKE Operator
Percent (%) Symbol
26
LIKE Operator
Underscore(_) Symbol
27
LIKE Operator
• Underscore (_) symbol represents one character
• Percent (%) symbol represents a string of zero or more
characters
• Return all employees with last names beginning with any letter
followed by an "o" and then followed by any other number of
letters
28
LIKE Operator Example
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
• Which of the following last names would be returned from the
above query?
– 1. Sommersmith
– 2. Oog
– 3. Fong
– 4. Mo
29
LIKE Operator Example
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
• Which of the following last names would be returned from the
above query?
– 1. Sommersmith
– 2. Oog
– 3. Fong
– 4. Mo
• If you said ALL 4, you are correct!
30
LIKE Operator
ESCAPE option
• What if the % or _ symbols are not a wildcard but part of the
value being searched
• The ESCAPE option is used to indicate that the _ or % symbol is
part of the value, not a wildcard value
• The ESCAPE option character can be any character, but
backslash (\) is recommended
31
LIKE Operator
ESCAPE option
32
LIKE Operator
ESCAPE option: An Example
• Retrieve JOB_IDs from the employees table that contain the
pattern _R
33
LIKE Operator
ESCAPE option: An Example
• Without the ESCAPE option, all employees that have an R in
JOB_ID are returned
34
LIKE Operator
ESCAPE option: An Example
• With the ESCAPE option, only employees that have an _R in
JOB_ID are returned
35
Returning NULL Values
36
IS NULL, IS NOT NULL Operators
• IS NULL condition tests for unknown data; that is there is no data
and the value is unknown
• The IS NOT NULL condition tests for data; that is the existence of
data
37
NULL Column Value in WHERE Clause
• When comparing for a NULL value, the equal (=) or not equal
(<>) operators cannot be used
SELECT customer_name,
credit_limit
FROM customers
WHERE credit_limit = NULL; - will not work
• The query will run, but it does not return a result set as the
actual value of NULL is unknown
38
IS NULL Operator
39
IS NOT NULL Operator
40
Remember: NULL Values
A common error is using
= NULL
which does not raise an error but also does not return any rows
41
Logical Operators
42
AND Operator
• Returns the Boolean value true if both conditions are true and
returns false if one or both of the conditions are false
43
AND Operator
44
OR Operator
• The result set contains rows that satisfy either one of the OR
conditions
45
OR Operator
46
Order of Evaluation with Logical Operators
47
Order of Operations – What Happens First
• In what order are the expressions evaluated?
48
Order of Operations – What Happens First
• First, the AND condition is evaluated, so all employees with
job_id ST_CLERK or MK_REP, AND who have a first name
starting with "P" are returned
• The OR clause is then evaluated and returns employees whose
last name contains "z"
49
Order of Operations – What Happens First
50
Order of Operations – What Happens First
• In this example, the order of the OR and AND have been
reversed from the previous slide
• The order of operations is:
1. first_name starts with "P" AND last_name contains "z". Both these
conditions must be met to be returned
2. Any instance of employees with job_id ST_CLERK or MK_REP will be
returned
51
Order of Operations – What Happens First
• Adding brackets/parenthesis changes the way the WHERE clause
is evaluated, and the rows returned
• The order of operations is:
1. The values in the brackets are selected
2. All instances of the values in the brackets AND contain the letter "z" in
their last_name are returned
52
Order of Evaluation
of Logical Operators
53
NOT Operator to Negate a Condition
• The NOT operator returns rows that do NOT satisfy the condition
in the WHERE clause
54
NOT Operator to
Negate a Condition
55
NOT IN Operator
56
Logical Operators and NULL Values
• A logical condition is true, false, or unknown for a given row
• A condition is unknown if it involves a comparison and one or
both of the values being compared is NULL
• The result set from a SELECT statement contains only those rows
for which the WHERE clause search condition is true
• If the search condition is false or unknown, the row is not
returned
57
Logical Operators
and NULL Values
58
WHERE Clause Rules
• Character strings and dates are enclosed in single quotation
marks ( ' ' )
• Numbers are not enclosed in single quotation marks
• All character searches are case-sensitive
• An alias cannot be used in the WHERE clause – Why?
59
Sorting Rows with the ORDER BY Clause
ORDER BY Clause
• Follows the FROM clause
• Last clause of the SQL statement
• Sort Sequence:
– Ascending sequence is the default and is optional
– DESC keyword is specified following the column name that is to be
sorted in descending sequence
61
ORDER BY Clause – Character Values
• Character values are sorted in alphabetical order
62
ORDER BY Clause – Numeric Values
• Numeric values are sorted lowest to highest
63
ORDER BY Clause – Date Values
• Date values are displayed with the earliest value first
64
ORDER BY Clause – Descending Sequence
65
Sorting on One Column
Ascending Sequence
66
Sorting on One Column
Descending Sequence
67
Sorting on Multiple Columns
68
Sorting on Multiple Columns
with DESC
69
Sorting with Multiple Columns
SELECT first_name, last_name, manager_id, department_id
FROM employees
ORDER BY manager_id, department_id, last_name;
70
Sorting with Multiple Columns
Another Way to Say It
SELECT first_name, last_name, manager_id, department_id
FROM employees
ORDER BY manager_id, department_id, last_name;
71
Sorting by
Relative Column Number
72
Sorting by Relative Column Number
Without Column Alias Column
73
Sorting by Relative Column Number
Using an Alias Column Name
• To sort on the credit_limit – balance column on the ORDER BY
clause, it would have to be specified as one of the following:
– ORDER BY city, credit_limit – balance DESC
– ORDER BY city, 3 DESC where the number 3 represents the relative
column number of the calculated column
74
Sorting by Relative Column Number
Using an Alias Column Name
75
Sorting on Other Columns
• Sort output by a column that is not listed in the SELECT clause
76
ORDER BY Clause – NULL Values
• NULL values are displayed last in ascending order and first in
descending order
77
ORDER BY Clause – NULL Values
78
Order of Execution – Example 1
• Basic SELECT
79
Order of Execution Example 2
• Give employees a 2.5% raise
• Set employees with a new salary > 10000
– Use alias on WHERE and ORDER BY Clauses
– What happens?
80
Order of Execution – Example 3
• Alias can be used on ORDER BY clause, but not WHERE clause
– Why?
• Use the expression on the WHERE clause
81
SQL Statement Order of Execution
2 WHERE Filters or restricts rows from the result set based on the criteria specified
3 SELECT Filters or restricts columns from the result set based on the column-list
82
83