Part-2 (Basic Filtering)
Part-2 (Basic Filtering)
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
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).
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
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.
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
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:
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'.