Skip to main content
SQL Fundamentals
CHAPTER 08 Beginner

SQL WHERE Clause | Filter Database Records

Updated: May 16, 2026
15 min read

# CHAPTER 8

Filtering Data with WHERE Clause

1. Introduction

Running SELECT * FROM users; on an enterprise database will attempt to download 50 million rows to your computer, crashing both the database and your application. In the real world, you rarely want *all* the data. You want *specific* data. You want the user who just logged in. You want the products that cost less than $20. To achieve this, we introduce the WHERE clause—the fundamental mechanism for filtering and slicing Relational Databases.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the syntax and placement of the WHERE clause.
  • Filter data using exact text and numeric matches.
  • Utilize mathematical comparison operators (>, <, >=, <=, !=).
  • Prevent massive data dumps by targeting specific rows.

3. The WHERE Clause Syntax

The WHERE clause is placed immediately *after* the FROM table declaration. It acts as a strict gateway: SQL examines every single row in the table; if the row meets the condition in the WHERE clause (evaluates to True), it is returned. If not, it is ignored.
sql
1234
-- Retrieve ALL columns from the users table, 
-- BUT ONLY return rows where the first_name is exactly &#039;Alice'
SELECT * FROM users 
WHERE first_name = &#039;Alice';

*(Remember: Just like in INSERT statements, text/string values MUST be wrapped in single quotes!)*

4. Numeric Filtering

When filtering by numbers (Integers or Decimals), you do *not* use quotes.
sql
123
-- Find the specific user with ID number 42
SELECT * FROM users 
WHERE id = 42;

5. Mathematical Comparison Operators

SQL is incredibly powerful at math. You are not limited to exact equals (=). You can use standard comparison operators to find ranges of data.
  • = (Equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)
  • != or <> (Not equal to)
sql
1234567
-- Find all products that cost MORE than 50 dollars
SELECT product_name, price FROM products 
WHERE price > 50;

-- Find all employees who do NOT work in the &#039;HR' department
SELECT * FROM employees 
WHERE department != &#039;HR';

6. Filtering by Dates

Because we set our columns up with proper DATE data types (Chapter 5), SQL can perform chronological math! You can use greater than > to mean "after" and less than < to mean "before". *(Dates are treated like strings and must be wrapped in single quotes formatted as YYYY-MM-DD).*
sql
123
-- Find all orders placed AFTER January 1st, 2023
SELECT * FROM orders 
WHERE order_date > &#039;2023-01-01';

7. Mini Project: E-Commerce Inventory Slicing

Let's filter our store's inventory to find actionable business intelligence.
sql
1234567891011
-- 1. Which specific products are currently out of stock?
SELECT product_name FROM products 
WHERE in_stock = FALSE;

-- 2. Find premium items (Items priced at $500 or more)
SELECT product_name, price FROM products 
WHERE price >= 500;

-- 3. Find the exact receipt for Order #9928
SELECT * FROM receipts 
WHERE order_id = 9928;

8. Common Mistakes

  • Putting WHERE before FROM: SELECT * WHERE age > 18 FROM users; will cause a fatal syntax error. SQL is processed linearly. You must first declare the table (FROM), and *then* filter it (WHERE).
  • Forgetting Quotes on Text: WHERE city = London will crash because SQL thinks London is a column name. It must be WHERE city = 'London'.
  • Using == for Equality: Programmers coming from JavaScript or Python often type WHERE id == 5. In standard SQL, equality is evaluated using a single equals sign =.

9. Best Practices

  • Primary Key Filtering: If you are building a "User Profile" page on a website, you should always filter the query using the user's unique id (e.g., WHERE id = 5), rather than their name (WHERE name = 'John'). There might be 50 "Johns" in the database, but only one ID #5!

10. Exercises

  1. 1. What mathematical operator is used in SQL to denote "Not Equal To"?
  1. 2. Write the condition to find items where the quantity is less than or equal to 10.

11. SQL Challenges

Write a complete query to select the title and author columns from the books table, but strictly limit the results to books published *before* the year 2000.
sql
12
SELECT title, author FROM books 
WHERE published_year < 2000;

12. MCQ Quiz with Answers

Question 1

What is the fundamental architectural purpose of the WHERE clause in a SELECT statement?

Question 2

When writing a WHERE condition to filter rows chronologically (e.g., finding orders placed after a certain date), what is the correct strict formatting for the date string?

13. Interview Questions

  • Q: Explain why querying SELECT * FROM users WHERE lastname = 'Smith' might return 50 rows, while querying SELECT * FROM users WHERE id = 105 will mathematically guarantee a maximum of 1 row.
  • Q: A developer writes SELECT name FROM employees WHERE salary > 100,000; and the database throws a syntax error. What is wrong with the integer formatting in the WHERE clause? (Answer: No commas allowed in SQL integers).

14. FAQs

Q: Is the WHERE clause case-sensitive? A: It depends on your database engine! In PostgreSQL, WHERE name = 'alice' will NOT match "Alice". It is strictly case-sensitive. In MySQL, the default behavior is usually case-insensitive. Always assume text searches are case-sensitive to be safe!

15. Summary

You have unlocked surgical precision. By appending the WHERE clause to your SELECT statements, and wielding mathematical operators like >, <, and !=, you can sift through millions of rows of data to extract the exact singular document your application requires.

16. Next Chapter Recommendation

Our filters are currently very simple (e.g., "Price > 50"). But what if a user is searching an E-commerce store and wants a laptop that is "Price > 50" AND "Brand = Apple"? In Chapter 9: SQL Operators and Conditions, we will unlock advanced logical routing using AND, OR, IN, and LIKE.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·