0% found this document useful (0 votes)
8 views21 pages

ITM220 W04 Chapter4

Uploaded by

hunterfirefist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

ITM220 W04 Chapter4

Uploaded by

hunterfirefist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CIT 225

Learning SQL
Chapter 4 - Filtering

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Topics Covered
• Learn how to use equality comparison operators.
• Learn how to use non-equality comparison operators.
• Learn how to use range comparison operators.
• Learn how to use subqueries in comparison operators.
• Learn how to use wildcards in comparison operators.
• Learn how to use NULL values in comparison operators.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Condition Evaluation
A where clause may contain operators, the and and or. If conditions are separated by the and
both must evaluate to true in order for the row to be included in the final result. With or, only
one condition must evaluate to true to be included in the final result.

Where first_name = 'STEVEN' AND create_date > '2006-01-01'

This statement would show a table that only includes the first_name as Steven, and the
create_date being after January 1, 2006.
Intermediate Result Finale Result
WHERE true OR true True
WHERE true OR false True
WHERE false OR true True
WHERE false OR false False

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Using Parentheses
If your where clause includes 3 or more conditions, use
parentheses to make your code clear both to the servers and
others who may read your code.
WHERE (first_name = 'STEVEN' OR last_name = 'YOUNG’)
AND create_date > '2006-01-01';
For there to b a final result set, either the first or second condition
(or both) must evaluate to true.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
The Not Operator
WHERE NOT (first_name = 'STEVEN' OR last_name = 'YOUNG')
AND create_date > '2006-01-01’;
In this case, this statement is retrieving only data as long as the first
name is not 'STEVEN' or last_name is not 'YOUNG', and as long as the
create_date is after January 1st, 2006. To make it more legible, it can be
rewritten as:
WHERE (first_name <> 'STEVEN' OR last_name <> ‘YOUNG’)
AND create_date > '2006-01-01';

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Equal Conditions
Most of the conditions you do will be equality expressions.
mysql> SELECT c.email
-> FROM customer c
-> INNER JOIN rental r
-> ON c.customer_id = r.customer_id
-> WHERE date(r.rental_date) = '2005-06-14';.
The WHERE clause is checking if the date is equal to '2005-06-14'.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Inequality Conditions
mysql> SELECT c.email
-> FROM customer c
-> INNER JOIN rental r
-> ON c.customer_id = r.customer_id
-> WHERE date(r.rental_date) <> '2005-06-14';.
This query is almost exactly the same as the previous one, except it
wants the date to NOT be '2005-06-14', as opposed to the previous
query.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Data Modification Using Equality/Inequality

Equality and inequality conditions are frequently used with deletions.


For example: if movie theatre has a policy to delete old account rows
after 1 year, this is a way that it might be done:
DELETE FROM rental
WHERE year(rental_date) = 2004;

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Range Conditions

It is also possible to check whether an expression


is within a specific range.

mysql> SELECT customer_id, rental_date


-> FROM rental
-> WHERE rental_date < ‘2005-05-26’;

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
The Between Operator

When you have both an upper and lower limit, you can use the
between operator. Always put the lower limit first, and then the higher
limit.
mysql> SELECT customer_id, rental_date
-> FROM rental
-> WHERE rentl_date BETWEEN ‘2005-06-14’ AND ‘2005-06-16’;

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
The In Operator

What if you needed to access something that was not just a single
value or a range, but rather many values?
SELECT title, rating
FROM film
WHERE rating IN ('G', 'PG');
You can add many other values within the parentheses.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Using Subqueries
It is also possible to create a subquery that will generate a set for you. If
you wanted to assume that any title with the name “pet” in it would be
safe for family viewing, you could execute a subquery against the film
table to retrieve all ratings associated with these films and then retrieve
all films having any of these ratings:
mysql> SELECT title, rating
-> FROM film
-> WHERE rating IN (SELECT rating
-> FROM film WHERE title LIKE '%PET%');

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Using Not In

Sometimes you may want to see whether an expression does not exist
within a set. For these queries, the NOT IN operator is available.
SELECT title, rating
FROM film
WHERE rating NOT IN ('PG-13', 'R', 'NC-17');
This would return all films with the rating of not PG-13, R, or NC-17.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Using Wildcards
Wildcards allow you to search for:
• Strings beginning/ending with a certain character
• Strings beginning/ending with a substring
• Strings containing a certain character anywhere within the string
• Strings containing a substring anywhere within the string
• Strings with a specific format, regardless of individual characters
The underscore takes place of a single character, while the percent sign can take the
place of any number of characters.
Wildcard character Matches
_ Exactly one character
% Any number of characters (including 0)

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Using Wildcards
When building conditions that utilize search expressions, you use the like
operator:
mysql> SELECT last_name, first_name
-> FROM customer
-> WHERE last_name LIKE '_A_T%S';
last_name first_name
MATTHEWS ERICA
WALTERS CASSANDRA
WATTS SHELLY

'_A_T%S' has an A in the second position, a T in the fourth position,


followed by any number of characters, with an S at the end.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Using Wildcards
Search expression Interpretation
F% Strings beginning with F
%t Strings ending with t
%bas% Strings containing the substring ‘bas’
_ _t_ Four-character string with a t in the third position
_ _ _-_ _ -_ _ _ _ 11-character strings with dashes in the fourth and seventh positions.

If you need a more sophisticated search, you can use multiple search
expressions.
mysql> SELECT last_name, first_name
-> FROM customer
-> WHERE last_name LIKE ‘Q%’ OR last_name LIKE ‘Y%’;
This query results in showing all last names that begin with Q, and also all
last names that begin with Y.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Null
Null is the absence of a value. For example, before an employee is
terminated, the employee’s end_date in the employee table would
be null.
There are other variations of null as well.
Not applicable: The employee ID column for a transaction that took
place at an ATM machine
Value not yet known: Such as when the federal ID is not known at the
time a customer row is created
Value undefined: Such as when an account is crated for a product that
has not yet been added to the database.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Null
When working with null, you should remember: rental_id customer_id
11496 155
• An expression can be null, but can never equal null. 11541 335
• Two nulls are never equal to each other. 11563 83
To test whether an expression is null, use the is null operator. 11577 219
11593 99
mysql> SELECT rental_id, customer_id
-> FROM rental 15867 505
-> WHERE return_date IS NULL; 15966 374
This query finds all film rentals that were never returned.
Be aware, if the last statement was WHERE return_date = NULL;
The server would not return anything, nor would it alert you of your error.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Null

If you want to see whether a value has been assigned to a column, you
can use the is not null operator.
mysql> SELECT rental_id, customer_id, return_date
-> FROM rental
-> WHERE return_date IS NOT NULL;
This query returns all movie rentals that were returned.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Null
Suppose that you have been asked to find all rentals that were not returned during May
through August of 2005. Your first instinct is to do this:

mysql> SELECT rental_id, customer_id, return_date


-> FROM rental
-> WHERE return_date NOT BETWEEN '2005-05-01' AND '2005-09-01';

Rental_id Customer_id Return_date


15365 327 2005-09-01
15388 50 2005-09-01
… … …
16040 195 2005-09-02

Notice that none of the return dates are null.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Null
Instead, this expression should be written like this:

mysql> SELECT rental_id, customer_id, return_date


-> FROM rental
-> WHERE return_date IS NULL
-> OR return_date NOT BETWEEN '2005-05-01' AND '2005-09-01';

This now includes the rentals that were returned outside of the May to August
window, along with the rentals that were never returned.

When working with tables that you are unfamiliar with, it is a good idea to
familiarize yourself with which columns in the table allow null.

Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.

You might also like