0% found this document useful (0 votes)
5 views36 pages

Part-2 (Basic Filtering)

The document provides an overview of SQL basic filtering techniques, including the use of the WHERE clause, operators like AND/OR, and functions such as IN and LIKE. It includes various examples and challenges to practice SQL queries for filtering data based on specific conditions. The document aims to enhance understanding of data analytics and business intelligence through practical SQL applications.

Uploaded by

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

Part-2 (Basic Filtering)

The document provides an overview of SQL basic filtering techniques, including the use of the WHERE clause, operators like AND/OR, and functions such as IN and LIKE. It includes various examples and challenges to practice SQL queries for filtering data based on specific conditions. The document aims to enhance understanding of data analytics and business intelligence through practical SQL applications.

Uploaded by

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

Professional Diploma in Business

Intelligence and Data Analytics


SQL | Basic Filtering

by Matrix Institute of Professionals

“ Grow Professionals, Grow Together ”


Filter WHERE
ü Used to FILTER the data in the output

ü Always after FROM

SYNTAX
Use Operator WHERE
Example
SELECT
*
FROM payment
WHERE amount = 10.99
Example
SELECT
COUNT(*)
FROM payment
WHERE amount = 0
Example
SELECT
first_name,
last_name
FROM customer
WHERE first_name = 'ADAM'
Example
SELECT
*
FROM payment
WHERE amount < 10.99
Example
SELECT
*
FROM payment
WHERE amount < 10.99
ORDER BY amount DESC
Example

SELECT
*
FROM payment
WHERE amount <> 10.99
Example

SELECT
*
FROM payment
WHERE amount <= 10.99
ORDER BY amount DESC
Challenge-1
Result

How many payment were made by the


customer with customer_id = 100?

What is the last name of our customer


with first name 'ERICA'?

Write a SQL query to get the answers


SYNTAX

SELECT
first_name,
last_name
FROM customer
WHERE first_name is null
SELECT
first_name,
last_name
FROM customer
WHERE first_name is not null
Challenge-2
Result
The inventory manager asks you how rentals
have not been returned yet (return_date is null).

The sales manager asks you how for a list of all


the payment_ids with an amount less than or
equal to $2. Include payment_id and the
amount.
Write a SQL query to get the answers!
AND/OR
ü Used to connect two conditions

SYNTAX
SYNTAX
SELECT
*
FROM payment
WHERE amount = 10.99
AND customer_id = 426
SYNTAX
SELECT
*
FROM payment
WHERE amount = 10.99
OR amount = 9.99
AND customer_id = 426
SYNTAX
Amount Customer_id Will it be included? Reason
10.99 100 ✅ Yes Because amount = 10.99

10.99 426 ✅ Yes Because amount = 10.99

Because both amount =


9.99 426 ✅ Yes
9.99 AND customer_id = 426

9.99 100 ❌ No Because customer_id ≠ 426


Because amount ≠ 10.99 or
12.99 426 ❌ No
9.99

Type 1:
SELECT *FROM payment •Rows where amount = 10.99
WHERE amount = 10.99 OR (amount = 9.99 •No condition on customer — it could be any customer_id
AND customer_id = 426)
Type 2:
•Rows where amount = 9.99
•AND customer_id = 426
SYNTAX
If you want to select payments of 10.99 or 9.99 only for customer 426, you should use parentheses:

SELECT
*
FROM payment
WHERE (amount = 10.99
OR amount = 9.99)
AND customer_id = 426
Challenge-3
The manager asks you about a list of all the payment of the
customer 322, 346 and 354 where the amount is either less than
$2 or greater than $10.
It should be ordered by the customer first (ascending) and then
as second condition order by amount in a descending order.

Write a SQL query to get the answers!


Result
BETWEEN … AND …
ü Used to filter a range of values

SYNTAX
SYNTAX

SELECT
payment_id,
amount
'YYYY-MM-DD'
FROM payment
WHERE amount BETWEEN '2020-01-24' AND '2020-01-26'
IN
ü The IN function in SQL is used to simplify
multiple OR conditions in a WHERE clause

SYNTAX
SELECT
*
FROM customer
❗ Not IN: WHERE customer_id in (123,212,323,243,353)
You can also use NOT
IN to exclude values:
Challenge-4
There have been 6 complaints of customers about their
payments. customer_id: 12,25,67,93,124,234
The concerned payments are all the payments of these
customers with amounts 4.99, 7.99 and 9.99 in January 2020.
Write a SQL query to get a list of the concerned payments!

Result
It should be 7 payments!
LIKE

ü Used to filter by matching against a pattern

any single character


ü Use wildcards: _
(Underscore)
ü Use wildcards: % any sequence of characters
SYNTAX

SELECT
*
FROM actor
WHERE first_name LIKE 'A%'
SELECT
first_name
FROM actor
WHERE first_name LIKE 'a%'
SYNTAX
SELECT
first_name
FROM actor
WHERE first_name ILIKE 'a%'
Note!
ILIKE is case-IN sensitive
SYNTAX
SELECT
*
FROM actor
WHERE first_name LIKE '%A%'
SYNTAX
SELECT
*
FROM actor
WHERE first_name LIKE '_A%'
SYNTAX
SELECT
*
FROM actor
WHERE first_name LIKE ' A%'
SYNTAX
SELECT
*
FROM actor
WHERE first_name NOT LIKE '%A%'
You need to help the inventory manager to find out:

How many movies are there that contain the "Documentary" in


the description?

Write a SQL query to get the answers!

Result
1. How many movies are there that contain 'Saga'
in the description and where the title starts either
with 'A' or ends with 'R'?
Use the alias 'no_of_movies'.

2. Create a list of all customers where the first name contains


'ER' and has an 'A' a s the second letter.
Order the results by the last name descendingly.

3. How many payments are there where the amount is either 0


or is between 3.99 and 7.99 and in the same time has
happened on 2020-05-01.
`
Results

Result 1 Result 2 Result 3

You might also like