Welcome back! 👋
In the last lesson, we learned how to retrieve data using SELECT, filter with WHERE, sort with ORDER BY, and use basic aggregate functions.
In this lesson, we will explore more advanced filtering techniques that are extremely useful in real-world SQL queries.
By the end of this lesson, you’ll be able to:
- Use multiple conditions with
AND,OR,NOT - Use
IN,BETWEEN, andLIKEfor filtering - Handle
NULLvalues correctly - Practice advanced filtering with real examples
Combining Multiple Conditions
You can combine multiple conditions using logical operators:
AND→ both conditions must be trueOR→ at least one condition must be trueNOT→ negates a condition
Example 1: Customers in Pakistan AND city is Lahore
SELECT full_name, city, country FROM customers WHERE country = 'Pakistan' AND city = 'Lahore'; Copy
Example 2: Customers in Pakistan OR USA
SELECT full_name, country FROM customers WHERE country = 'Pakistan' OR country = 'USA'; Copy
Example 3: Customers NOT from Pakistan
SELECT full_name, country FROM customers WHERE NOT country = 'Pakistan'; Copy
Using IN for Multiple Values
IN is a shortcut for multiple OR conditions.
Example: Customers from Pakistan, USA, or UK
SELECT full_name, country FROM customers WHERE country IN ('Pakistan', 'USA', 'UK'); Copy
✅ Much cleaner than writing country='Pakistan' OR country='USA' OR country='UK'.
Using BETWEEN for Ranges
BETWEEN filters values between a range (inclusive).
Example: Customers who signed up in February 2023
SELECT full_name, signup_date FROM customers WHERE signup_date BETWEEN '2023-02-01' AND '2023-02-28'; Copy
Example: Products priced between $50 and $100
SELECT product_name, price FROM products WHERE price BETWEEN 50 AND 100; Copy
Using LIKE for Pattern Matching
LIKE is used to filter based on patterns.
%→ any number of characters_→ single character
Example 1: Customers whose name starts with ‘A’
SELECT full_name FROM customers WHERE full_name LIKE 'A%'; Copy
Example 2: Customers whose name ends with ‘Khan’
SELECT full_name FROM customers WHERE full_name LIKE '%Khan'; Copy
Example 3: Customers whose name contains ‘a’
SELECT full_name FROM customers WHERE full_name LIKE '%a%'; Copy
Handling NULL Values
NULL means unknown or missing value. It’s not the same as 0 or empty string.
- Use
IS NULLorIS NOT NULL
Example: Find customers with missing city
SELECT full_name, city FROM customers WHERE city IS NULL; Copy
Example: Customers with city filled
SELECT full_name, city FROM customers WHERE city IS NOT NULL; Copy
Important: You cannot use
= NULLor!= NULL. Always useIS NULLorIS NOT NULL.
Combining Everything
Let’s do a real-world example:
Find all customers from Pakistan or USA whose name contains ‘a’ and city is not NULL.
SELECT full_name, city, country FROM customers WHERE (country IN ('Pakistan', 'USA')) AND full_name LIKE '%a%' AND city IS NOT NULL; Copy
✅ This is exactly how you would filter data in real projects.
Practice Queries
- Find products in the ‘Electronics’ category priced between $50 and $1000.
- Find all orders where total_amount > 1000.
- Find customers whose name starts with ‘S’.
- Find all customers who signed up after March 1, 2023, and are not from Pakistan.
Try these queries in your sample database — practice is key to mastering SQL.
Lesson Summary
AND,OR,NOTcombine multiple conditionsINsimplifies multiple OR conditionsBETWEENfilters a range of valuesLIKEis used for pattern matchingNULLrequiresIS NULLorIS NOT NULL- Combining these tools lets you filter data exactly how you want
In Lesson 5, we’ll cover:
- Sorting data with
ORDER BY - Grouping data with
GROUP BY - Filtering grouped data with
HAVING - Using aggregate functions (
COUNT,SUM,AVG) in practical scenarios