DevLearningTools

LEARN · SQL

SQL WHERE

WHERE narrows a query down to only the rows you care about. Learn the comparison operators and how to filter a SELECT, UPDATE, or DELETE.

SELECT without a filter returns every row in the table. WHERE is how you cut that down to just the rows that match a condition, same employees table as before, but now you can ask for just the Engineering employees, or just the ones earning over $90,000.

Learning Objectives

  • Filter rows with comparison operators (=, >, <, etc.).
  • Use WHERE with SELECT, and know it works the same way with UPDATE and DELETE.
  • Understand why text values need quotes and numbers don't.

All Rows

Every row in the table

WHERE Condition

Checked one row at a time

Matching Rows

Only these are returned

Comparison Operators

OperatorMeaningExample
=equal todepartment = 'Engineering'
<> or !=not equal todepartment <> 'Research'
> or <greater than / less thansalary > 90000
>= or <=greater-or-equal / less-or-equalsalary >= 90000
MySQL / PostgreSQL / SQLite / SQL Server
SELECT first_name, salary
FROM employees
WHERE salary > 90000;

-- Result:
-- first_name | salary
-- Ada        | 95000
-- Grace      | 98000
NOTE

<> is the ANSI SQL standard form of "not equal," and works on every major database. != is a widely supported alias that MySQL, PostgreSQL, SQL Server, and SQLite all also accept.

Quoting Text Values

Text values are wrapped in single quotes, 'Engineering'. Numbers aren't quoted. This matters because WHERE department = Engineering (no quotes) isn't valid SQL, the database would try to interpret Engineering as a column or keyword, not a text value.

MySQL / PostgreSQL / SQLite / SQL Server
SELECT * FROM employees WHERE department = 'Engineering';
SELECT * FROM employees WHERE salary > 90000;

WHERE Isn't Just for SELECT

WHERE filters rows the exact same way in UPDATE and DELETE, covered in their own lessons later in this course, scoping which rows get changed or removed instead of which rows get returned.

Try It Yourself

Write a query that returns the first_name of every employee NOT in the Research department.

Answer
SELECT first_name
FROM employees
WHERE department <> 'Research';

Frequently Asked Questions

Can I filter on a column that isn't in my SELECT list?

Yes. WHERE operates on the table's columns directly, independent of which columns you chose to display in SELECT.

Why doesn't WHERE salary = NULL ever return rows?

NULL means "unknown value," and in SQL, unknown compared to anything, including another NULL, is never true. This is covered in full in the SQL NULL Values lesson.

Common Beginner Mistakes

Forgetting quotes around a text value

WHERE department = Engineering (no quotes) is a syntax error or gets misinterpreted, the database expects Engineering to be a column or keyword without quotes, not literal text.

Using = to compare against NULL

WHERE some_column = NULL never matches anything, even rows that are genuinely NULL. The correct form is IS NULL, covered in the next lessons.

Best Practices

  • Always wrap text and date values in single quotes, never numbers.
  • Use <> or != consistently within a codebase, rather than mixing both styles.
  • Preview an UPDATE or DELETE's WHERE condition as a SELECT first, to confirm exactly which rows it matches before changing or removing them.

Interview Questions

Write a query that finds employees earning at least 90000 in the Engineering department.

SELECT first_name, salary FROM employees WHERE department = 'Engineering' AND salary >= 90000; — AND is covered in the next module, but this combines two WHERE conditions, both of which individually use the operators from this lesson.

Why is WHERE salary = NULL always wrong, regardless of what's actually in the salary column?

Any comparison involving NULL, including =, evaluates to unknown rather than true or false, since NULL represents an unknown value, not a value that can be equal to anything. WHERE only keeps rows where the condition is true, so this always excludes every row.

Summary

This lesson covered filtering rows with comparison operators, quoting text values correctly, and how WHERE works identically across SELECT, UPDATE, and DELETE.

What's Next?

The next lesson covers ORDER BY, controlling what order the rows in a result come back in.

</>

Ready to Practice SQL?

Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.

SQLite / PostgreSQL

Fast, no server, no ads.

Open DB Fiddle ↗

MySQL / SQL Server

Switch databases with one dropdown.

Open SQL Fiddle ↗

Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.