Filtering is one of the most powerful features of SQL. It allows you to sift through millions of rows and extract only the records that meet specific criteria. In this chapter, we'll explore comparison operators, logical operators, and pattern matching.
The simplest way to filter is using the equals sign (=).
-- Find all orders that have been delivered
SELECT *
FROM orders
WHERE order_status = 'delivered'
LIMIT 10;SQL supports standard comparison operators:
>(greater than)<(less than)>=(greater than or equal to)<=(less than or equal to)
-- Find items where freight is more expensive than the product
SELECT
order_id,
price,
freight_value
FROM order_items
WHERE freight_value > price
LIMIT 10;Both != and <> represent "not equal to". <> is the ANSI standard and is generally preferred for cross-database compatibility.
SELECT
order_id,
order_status
FROM orders
WHERE order_status <> 'delivered'
LIMIT 10;Use AND when all conditions must be true.
-- Find delivered orders from RJ (Rio de Janeiro)
SELECT
o.order_id,
o.order_status,
c.customer_state
FROM orders o, customers c
WHERE o.customer_id = c.customer_id
AND o.order_status = 'delivered'
AND c.customer_state = 'RJ'
LIMIT 10;Use OR when at least one condition must be true.
-- Find orders that are either shipped or delivered
SELECT
order_id,
order_status
FROM orders
WHERE order_status = 'shipped'
OR order_status = 'delivered'
LIMIT 10;IN is a cleaner way to write multiple OR conditions for the same column.
SELECT
order_id,
order_status
FROM orders
WHERE order_status IN ('shipped', 'delivered', 'invoiced')
LIMIT 10;You can also use NOT IN to exclude specific values.
BETWEEN filters values within a specific range (inclusive).
-- Find items priced between 100 and 500 BRL
SELECT
order_id,
product_id,
price
FROM order_items
WHERE price BETWEEN 100 AND 500
LIMIT 10;LIKE allows for flexible searching using wildcards:
%matches any number of characters._matches exactly one character.
-- Find cities starting with 'rio'
SELECT DISTINCT customer_city
FROM customers
WHERE customer_city LIKE 'rio%'
LIMIT 10;NULL represents missing or unknown data. = with NULL. You must use IS NULL or IS NOT NULL.
-- Find orders that have NOT been delivered yet
SELECT
order_id,
order_status,
order_delivered_customer_date
FROM orders
WHERE order_delivered_customer_date IS NULL
LIMIT 10;- Find all orders with status 'canceled'. How many are there?
- Find all products where
product_weight_g > 10000(heavier than 10kg). - Find all payments made via 'boleto' with
payment_value > 200. - Find all customers from states 'SP', 'RJ', or 'MG'. Count them.
- Find all reviews with
review_scoreof 1 that also have areview_comment_message(not NULL). - Find all seller cities that start with 'sao'.
Solutions
-- Exercise 1
SELECT COUNT(*) FROM orders WHERE order_status = 'canceled';
-- Exercise 2
SELECT product_id, product_weight_g FROM products WHERE product_weight_g > 10000;
-- Exercise 3
SELECT order_id, payment_type, payment_value FROM order_payments
WHERE payment_type = 'boleto' AND payment_value > 200;
-- Exercise 4
SELECT COUNT(*) FROM customers WHERE customer_state IN ('SP', 'RJ', 'MG');
-- Exercise 5
SELECT review_id, review_score, review_comment_message FROM order_reviews
WHERE review_score = 1 AND review_comment_message IS NOT NULL AND review_comment_message != '';
-- Exercise 6
SELECT DISTINCT seller_city FROM sellers WHERE seller_city LIKE 'sao%';