← Back to MySQL Course | Chapter 6: Filtering & Sorting | Lesson 1 of 8

WHERE Clause

WHERE is like a sieve that only lets through the rows fitting your rule, such as all students older than ten.
Syntax
sql
SELECT column1, column2
FROM table_name
WHERE condition;

Introduction to Filtering

The WHERE clause filters a result set down to only the rows matching a specified condition, turning a query that would return an entire table into one that returns exactly the records you actually need.

Example: Introduction to Filtering

sql
SELECT * FROM users WHERE age > 18;

Filtering with Numbers

Numeric filtering uses standard comparison operators like >, <, and >= directly against a column, which is the typical way to query things like 'all products under $50' or 'users older than 18'.

Example: Filtering with Numbers

sql
SELECT * FROM products WHERE price < 20;

Filtering with Text

Text values in a WHERE condition must be wrapped in single quotes, or MySQL will misinterpret the bare word as a column name instead of a literal string and raise an error.

Example: Filtering with Text

sql
SELECT * FROM users WHERE name = 'Amit';

Filtering with Dates

Date literals also need to be quoted like text, written in the YYYY-MM-DD format MySQL expects, since an unquoted date would similarly be parsed as an invalid column reference.

Example: Filtering with Dates

sql
SELECT * FROM orders WHERE order_date = '2024-03-05';

Using Inequality Operators

The != or <> operator excludes rows matching a specific value rather than selecting for it, which is the standard way to express 'everything except this one case' in a filter.

Example: Using Inequality Operators

sql
SELECT * FROM users WHERE status != 'inactive';
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting quotes around text, like WHERE city = Patna, so Patna is read as a column name.
  2. Writing a date in another format like '14/05/2024' instead of 2024-05-14.
  3. Using = NULL instead of IS NULL, which never matches.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.