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:

  1. Use multiple conditions with ANDORNOT
  2. Use INBETWEEN, and LIKE for filtering
  3. Handle NULL values correctly
  4. Practice advanced filtering with real examples

Combining Multiple Conditions

You can combine multiple conditions using logical operators:

  • AND → both conditions must be true
  • OR → at least one condition must be true
  • NOT → 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 NULL or IS 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 = NULL or != NULL. Always use IS NULL or IS 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

  1. Find products in the ‘Electronics’ category priced between $50 and $1000.
  2. Find all orders where total_amount > 1000.
  3. Find customers whose name starts with ‘S’.
  4. 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

  • ANDORNOT combine multiple conditions
  • IN simplifies multiple OR conditions
  • BETWEEN filters a range of values
  • LIKE is used for pattern matching
  • NULL requires IS NULL or IS 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 (COUNTSUMAVG) in practical scenarios

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts