Wicked Smart Data
LearnArticlesAbout
Sign InSign Up
LearnArticlesAboutContact
Sign InSign Up
Wicked Smart Data

The go-to platform for professionals who want to master data, automation, and AI — from Excel fundamentals to cutting-edge machine learning.

Platform

  • Learning Paths
  • Articles
  • About
  • Contact

Connect

  • Contact Us
  • RSS Feed

© 2026 Wicked Smart Data. All rights reserved.

Privacy PolicyTerms of Service
All Articles
Master SQL Data Filtering and Sorting: Complete Guide to WHERE, AND, OR, IN, BETWEEN, ORDER BY

Master SQL Data Filtering and Sorting: Complete Guide to WHERE, AND, OR, IN, BETWEEN, ORDER BY

SQL🌱 Foundation12 min readApr 3, 2026Updated Apr 3, 2026
Table of Contents
  • Prerequisites
  • Understanding the WHERE Clause: Your Data Filter
  • Comparison Operators: The Building Blocks
  • Combining Conditions with AND and OR
  • The AND Operator: All Conditions Must Be True
  • The OR Operator: At Least One Condition Must Be True
  • Combining AND and OR: Mastering Parentheses
  • Streamlining Filters with IN and BETWEEN
  • The IN Operator: Matching Against Lists
  • The BETWEEN Operator: Filtering Ranges
  • Combining IN and BETWEEN for Complex Filters
  • Organizing Results with ORDER BY

Master Data Filtering and Sorting in SQL: Your Complete Guide to WHERE, AND, OR, IN, BETWEEN, and ORDER BY

Picture this: You're analyzing customer data for an e-commerce company, and your manager asks you to find all customers from California or Texas who made purchases between $100 and $500 in the last month, sorted by purchase amount. Your database contains millions of records. Without proper filtering and sorting techniques, this seemingly simple request would be impossible to fulfill efficiently.

This is where SQL's filtering and sorting capabilities become your superpower. By the end of this lesson, you'll confidently use WHERE clauses to pinpoint exactly the data you need, combine multiple conditions with AND and OR operators, use shortcuts like IN and BETWEEN for common patterns, and organize your results with ORDER BY. These skills form the foundation of virtually every meaningful data analysis task you'll encounter.

What you'll learn:

  • How to filter data using WHERE clauses with precise conditions
  • When and how to combine multiple filters using AND and OR operators
  • Efficient techniques for filtering ranges and lists using BETWEEN and IN
  • How to sort results in ascending and descending order with ORDER BY
  • Best practices for writing readable and performant filtering queries

Prerequisites

This lesson assumes you understand basic SQL SELECT statements and can connect to a database. If you need a refresher on SELECT basics, complete the "Introduction to SELECT Statements" lesson first.

Understanding the WHERE Clause: Your Data Filter

Think of the WHERE clause as a bouncer at an exclusive club—it examines every row in your table and only lets through the ones that meet your specific criteria. Without WHERE, a SELECT statement returns every single row in a table, which is rarely what you want in real-world scenarios.

The basic syntax follows this pattern:

SELECT column1, column2
FROM table_name
WHERE condition;

Let's work with a realistic scenario. Imagine you're analyzing an orders table for an online retailer:

SELECT order_id, customer_name, order_total, order_date, shipping_state
FROM orders
WHERE order_total > 100;

This query filters out all orders with totals of $100 or less, showing only the higher-value purchases. The WHERE clause evaluates each row individually—if order_total > 100 is true for a row, it gets included in the results.

Comparison Operators: The Building Blocks

SQL provides several comparison operators for different filtering needs:

-- Equal to
SELECT * FROM orders WHERE shipping_state = 'CA';

-- Not equal to (two variations)
SELECT * FROM orders WHERE shipping_state != 'CA';
SELECT * FROM orders WHERE shipping_state <> 'CA';

-- Greater than and less than
SELECT * FROM orders WHERE order_total > 250;
SELECT * FROM orders WHERE order_total < 50;

-- Greater than or equal to, less than or equal to
SELECT * FROM orders WHERE order_total >= 100;
SELECT * FROM orders WHERE order_total <= 500;

Tip: When working with text values, always use single quotes. For numbers and dates, quotes are typically not needed, though date formats can vary by database system.

Combining Conditions with AND and OR

Real-world filtering often requires multiple conditions. This is where AND and OR operators transform simple filters into powerful, precise queries.

The AND Operator: All Conditions Must Be True

AND works like a strict interview process—every condition must be satisfied for a row to pass through:

SELECT order_id, customer_name, order_total, shipping_state
FROM orders
WHERE order_total > 100 
  AND shipping_state = 'CA';

This returns only California orders over $100. Both conditions must be true simultaneously.

Here's a more complex example targeting high-value recent orders:

SELECT order_id, customer_name, order_total, order_date
FROM orders
WHERE order_total >= 500
  AND order_date >= '2024-01-01'
  AND shipping_state IN ('CA', 'NY', 'TX');

The OR Operator: At Least One Condition Must Be True

OR is more flexible—it's like multiple entry gates where you only need to qualify for one:

SELECT order_id, customer_name, order_total, shipping_state
FROM orders
WHERE shipping_state = 'CA' 
   OR shipping_state = 'TX';

This retrieves all orders from either California or Texas.

Combining AND and OR: Mastering Parentheses

When mixing AND and OR, parentheses become crucial for controlling the logic. Without them, SQL might interpret your conditions differently than intended:

-- Without parentheses - potentially confusing logic
SELECT * FROM orders
WHERE order_total > 200 
  AND shipping_state = 'CA' 
   OR shipping_state = 'NY';

This might not work as expected because AND has higher precedence than OR. SQL evaluates this as: (order_total > 200 AND shipping_state = 'CA') OR shipping_state = 'NY'

This means it would return ALL New York orders, regardless of order total, plus California orders over $200.

Here's the corrected version with clear parentheses:

-- With parentheses - clear logic
SELECT * FROM orders
WHERE order_total > 200 
  AND (shipping_state = 'CA' OR shipping_state = 'NY');

Now it returns only orders over $200 from either California or New York.

Streamlining Filters with IN and BETWEEN

While you can accomplish any filtering task with basic comparison operators and AND/OR logic, SQL provides convenient shortcuts for common patterns.

The IN Operator: Matching Against Lists

Instead of chaining multiple OR conditions, IN lets you specify a list of values:

-- Instead of this verbose approach:
SELECT * FROM orders
WHERE shipping_state = 'CA' 
   OR shipping_state = 'NY' 
   OR shipping_state = 'TX' 
   OR shipping_state = 'FL';

-- Use this cleaner syntax:
SELECT * FROM orders
WHERE shipping_state IN ('CA', 'NY', 'TX', 'FL');

IN works with any data type. Here's an example with specific order IDs:

SELECT order_id, customer_name, order_total
FROM orders
WHERE order_id IN (1001, 1005, 1023, 1087);

The NOT IN operator does the opposite—it excludes rows that match any value in the list:

SELECT * FROM orders
WHERE shipping_state NOT IN ('AK', 'HI');

Warning: Be careful with NOT IN when the list might contain NULL values. NOT IN returns no results if any value in the list is NULL, which can be surprising.

The BETWEEN Operator: Filtering Ranges

BETWEEN provides an elegant way to filter ranges, particularly useful for dates, numbers, and even alphabetical ranges:

-- Instead of this:
SELECT * FROM orders
WHERE order_total >= 100 AND order_total <= 500;

-- Use this:
SELECT * FROM orders
WHERE order_total BETWEEN 100 AND 500;

BETWEEN is inclusive—it includes both boundary values. The previous example returns orders with totals of exactly $100, exactly $500, and everything in between.

Date ranges work beautifully with BETWEEN:

SELECT order_id, customer_name, order_date, order_total
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-03-31';

You can also use NOT BETWEEN to exclude ranges:

SELECT * FROM orders
WHERE order_total NOT BETWEEN 50 AND 200;

This returns orders under $50 or over $200.

Combining IN and BETWEEN for Complex Filters

These operators work seamlessly with AND and OR logic:

SELECT order_id, customer_name, order_total, shipping_state, order_date
FROM orders
WHERE order_total BETWEEN 100 AND 1000
  AND shipping_state IN ('CA', 'NY', 'TX')
  AND order_date >= '2024-01-01';

Organizing Results with ORDER BY

Finding the right data is only half the battle—presenting it in a meaningful order makes the difference between useful insights and confusing lists. The ORDER BY clause sorts your results, making patterns and relationships visible.

Basic Sorting: Ascending and Descending

By default, ORDER BY sorts in ascending order (smallest to largest, A to Z):

SELECT customer_name, order_total, order_date
FROM orders
WHERE order_total > 100
ORDER BY order_total;

This shows the smallest qualifying orders first. To reverse the order, add DESC:

SELECT customer_name, order_total, order_date
FROM orders
WHERE order_total > 100
ORDER BY order_total DESC;

Now the highest-value orders appear first—perfect for identifying your biggest customers.

Multi-Column Sorting: Creating Hierarchical Order

Real-world data often needs multiple sorting criteria. ORDER BY handles this elegantly:

SELECT customer_name, shipping_state, order_total, order_date
FROM orders
WHERE order_date >= '2024-01-01'
ORDER BY shipping_state, order_total DESC;

This sorts first by state alphabetically, then within each state by order total from highest to lowest. You'll see all Alabama orders together (sorted by decreasing total), followed by all Alaska orders (also sorted by decreasing total), and so on.

Each column in ORDER BY can have its own direction:

SELECT customer_name, shipping_state, order_total, order_date
FROM orders
ORDER BY shipping_state ASC, order_date DESC, order_total ASC;

This creates a sophisticated sorting hierarchy: states alphabetically, then within each state by newest orders first, then within each date by smallest orders first.

Sorting by Column Position

You can reference columns by their position number instead of name, which is handy for complex SELECT statements:

SELECT customer_name, order_total, order_date
FROM orders
WHERE shipping_state = 'CA'
ORDER BY 2 DESC, 3 DESC;

Here, 2 refers to the second column (order_total) and 3 refers to the third column (order_date). While this works, using column names is generally clearer and more maintainable.

Hands-On Exercise

Let's put these concepts together with a comprehensive exercise. We'll work with a customer orders scenario that mirrors real business questions.

Given this sample data structure for an orders table:

  • order_id (integer)
  • customer_name (text)
  • customer_email (text)
  • order_total (decimal)
  • order_date (date)
  • shipping_state (text)
  • product_category (text)

Exercise 1: Basic Filtering Write a query to find all orders from California with totals over $150:

SELECT order_id, customer_name, order_total, order_date
FROM orders
WHERE shipping_state = 'CA' 
  AND order_total > 150;

Exercise 2: Multiple Conditions with OR Find orders from either the West Coast (CA, OR, WA) or Texas with totals between $200 and $800:

SELECT order_id, customer_name, order_total, shipping_state
FROM orders
WHERE shipping_state IN ('CA', 'OR', 'WA', 'TX')
  AND order_total BETWEEN 200 AND 800;

Exercise 3: Complex Logic with Parentheses Find orders that are either high-value (over $500) from any state, or any order from New York or Florida:

SELECT order_id, customer_name, order_total, shipping_state
FROM orders
WHERE order_total > 500 
   OR shipping_state IN ('NY', 'FL')
ORDER BY order_total DESC;

Exercise 4: Date Filtering and Sorting Find all orders from the first quarter of 2024, excluding orders under $50, sorted by date (newest first) and then by total (highest first):

SELECT order_id, customer_name, order_total, order_date, shipping_state
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-03-31'
  AND order_total >= 50
ORDER BY order_date DESC, order_total DESC;

Challenge Exercise: Write a query to find customers who made orders in specific product categories (Electronics, Books, or Clothing) with totals not between $25 and $75, from states other than Alaska and Hawaii, sorted by customer name and then by order total:

SELECT customer_name, order_total, product_category, shipping_state, order_date
FROM orders
WHERE product_category IN ('Electronics', 'Books', 'Clothing')
  AND order_total NOT BETWEEN 25 AND 75
  AND shipping_state NOT IN ('AK', 'HI')
ORDER BY customer_name, order_total DESC;

Common Mistakes & Troubleshooting

Understanding what can go wrong helps you write better queries and debug issues quickly.

Mistake 1: Case Sensitivity in Text Comparisons

Different databases handle case sensitivity differently. This query might miss results:

-- Might not find 'california' or 'Ca'
SELECT * FROM orders WHERE shipping_state = 'CA';

Solution: Use UPPER() or LOWER() functions for consistent comparisons:

SELECT * FROM orders WHERE UPPER(shipping_state) = 'CA';

Mistake 2: Forgetting About NULL Values

NULL values can cause unexpected behavior with comparison operators:

-- This won't return rows where shipping_state is NULL
SELECT * FROM orders WHERE shipping_state != 'CA';

Solution: Handle NULLs explicitly when needed:

SELECT * FROM orders 
WHERE shipping_state != 'CA' OR shipping_state IS NULL;

Mistake 3: Incorrect Date Formats

Date format requirements vary by database. This might fail:

-- Might not work depending on database
WHERE order_date = '03/15/2024'

Solution: Use ISO format (YYYY-MM-DD) for maximum compatibility:

WHERE order_date = '2024-03-15'

Mistake 4: Parentheses Logic Errors

This query has unclear logic:

-- Confusing: what exactly does this return?
WHERE order_total > 100 AND shipping_state = 'CA' OR order_total > 500

Solution: Always use parentheses to make intentions clear:

WHERE (order_total > 100 AND shipping_state = 'CA') OR order_total > 500

Mistake 5: Performance Issues with Large Datasets

This query might be slow on large tables:

-- Potentially slow without proper indexes
WHERE UPPER(customer_name) LIKE '%SMITH%'

Solution: Use indexes on frequently filtered columns and avoid functions in WHERE clauses when possible.

Summary & Next Steps

You now have the fundamental tools for filtering and sorting data in SQL. The WHERE clause lets you precisely target the data you need, while AND, OR, IN, and BETWEEN operators help you express complex conditions clearly. ORDER BY transforms raw results into meaningful, organized information.

These skills form the foundation for virtually every SQL analysis task. Whether you're finding specific customer segments, analyzing sales trends, or preparing data for reports, you'll use these techniques constantly.

Key takeaways:

  • WHERE clauses filter rows based on conditions you specify
  • AND requires all conditions to be true; OR requires at least one to be true
  • IN simplifies multiple OR conditions; BETWEEN handles ranges elegantly
  • ORDER BY sorts results to reveal patterns and relationships
  • Parentheses ensure your complex logic works as intended
  • Always consider NULL values and case sensitivity in your filters

What's next: Now that you can filter and sort data, you're ready to learn about grouping and aggregating data with GROUP BY and functions like COUNT, SUM, and AVG. These tools will let you transform filtered data into powerful summaries and insights.

Practice these techniques with your own datasets. Start simple with single conditions, then gradually build complexity as your confidence grows. The combination of precise filtering and thoughtful sorting will quickly become second nature, opening the door to sophisticated data analysis.

Learning Path: SQL Fundamentals

Previous

Master Advanced SQL Filtering and Sorting: WHERE, AND, OR, IN, BETWEEN, ORDER BY

Next

Mastering SQL Data Filtering and Sorting: WHERE, AND, OR, IN, BETWEEN, ORDER BY

Related Articles

SQL⚡ Practitioner

Mastering SQL Data Filtering and Sorting: WHERE, AND, OR, IN, BETWEEN, ORDER BY

13 min
SQL🔥 Expert

Master Advanced SQL Filtering and Sorting: WHERE, AND, OR, IN, BETWEEN, ORDER BY

22 min
SQL⚡ Practitioner

Master SQL Filtering & Sorting: WHERE, AND, OR, IN, BETWEEN, ORDER BY

15 min

On this page

  • Prerequisites
  • Understanding the WHERE Clause: Your Data Filter
  • Comparison Operators: The Building Blocks
  • Combining Conditions with AND and OR
  • The AND Operator: All Conditions Must Be True
  • The OR Operator: At Least One Condition Must Be True
  • Combining AND and OR: Mastering Parentheses
  • Streamlining Filters with IN and BETWEEN
  • The IN Operator: Matching Against Lists
  • The BETWEEN Operator: Filtering Ranges
  • Basic Sorting: Ascending and Descending
  • Multi-Column Sorting: Creating Hierarchical Order
  • Sorting by Column Position
  • Hands-On Exercise
  • Common Mistakes & Troubleshooting
  • Mistake 1: Case Sensitivity in Text Comparisons
  • Mistake 2: Forgetting About NULL Values
  • Mistake 3: Incorrect Date Formats
  • Mistake 4: Parentheses Logic Errors
  • Mistake 5: Performance Issues with Large Datasets
  • Summary & Next Steps
  • Combining IN and BETWEEN for Complex Filters
  • Organizing Results with ORDER BY
  • Basic Sorting: Ascending and Descending
  • Multi-Column Sorting: Creating Hierarchical Order
  • Sorting by Column Position
  • Hands-On Exercise
  • Common Mistakes & Troubleshooting
  • Mistake 1: Case Sensitivity in Text Comparisons
  • Mistake 2: Forgetting About NULL Values
  • Mistake 3: Incorrect Date Formats
  • Mistake 4: Parentheses Logic Errors
  • Mistake 5: Performance Issues with Large Datasets
  • Summary & Next Steps