SQL WHERE
SQL

SQL WHERE — How to Filter Rows with Conditions

CHAPTER 5 of 30 SQL Tutorial — Free Course on Neotech Navigators
The SQL WHERE clause is how you filter data in SQL. Instead of returning every row in a table, WHERE lets you specify conditions so only the rows that match are included in the result. This is one of the most essential SQL concepts you will use in virtually every query.

The SQL WHERE Clause

The WHERE clause filters rows based on a specified condition. Only the rows where the condition evaluates to TRUE are included in the result set. If you are new to SQL, make sure you have completed Chapter 3: SQL SELECT first, as WHERE builds directly on SELECT queries.

SQL — WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;

We will use this Employees table for all examples in this chapter:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | Delhi     |
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | 72000  | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+

Filter by Text Value

To filter rows where a column matches a specific text value, wrap the value in single quotes:

SQL
SELECT Name, Department, Salary
FROM Employees
WHERE Department = 'IT';
✓ OUTPUT
+-----------+------------+--------+
| Name      | Department | Salary |
+-----------+------------+--------+
| Arun M.   | IT         | 78000  |
| Ravi S.   | IT         | 72000  |
+-----------+------------+--------+

Only employees in the IT department are returned. All other rows are excluded.

Filter by Numeric Value

Numeric conditions do not use quotes. You can use comparison operators like =, >, <, >=, and <=:

SQL — Salary Greater Than
SELECT Name, Salary
FROM Employees
WHERE Salary > 60000;
✓ OUTPUT
+-----------+--------+
| Name      | Salary |
+-----------+--------+
| John D.   | 62000  |
| Arun M.   | 78000  |
| Ravi S.   | 72000  |
+-----------+--------+

WHERE Comparison Operators

Operator Meaning Example
= Equal to WHERE City = 'Mumbai'
> Greater than WHERE Salary > 60000
< Less than WHERE Salary < 60000
>= Greater than or equal to WHERE Salary >= 55000
<= Less than or equal to WHERE Salary <= 55000
<> or != Not equal to WHERE Department <> 'Sales'

Example: Not Equal To

SQL
SELECT Name, Department
FROM Employees
WHERE Department <> 'Sales';
✓ OUTPUT
+-----------+------------+
| Name      | Department |
+-----------+------------+
| John D.   | Marketing  |
| Arun M.   | IT         |
| Ravi S.   | IT         |
| Sara L.   | HR         |
+-----------+------------+

Example: Less Than or Equal To

SQL
SELECT Name, Salary
FROM Employees
WHERE Salary <= 55000;
✓ OUTPUT
+-----------+--------+
| Name      | Salary |
+-----------+--------+
| Priya K.  | 55000  |
| Emma W.   | 51000  |
+-----------+--------+

WHERE with Text — Remember the Quotes

⚠️ Critical Rule

Text values in WHERE must be in single quotes: WHERE City = 'Mumbai'. Numbers must not have quotes: WHERE Salary > 60000. Mixing these up is the #1 beginner mistake. You learned this rule in Chapter 2: SQL Syntax.

WHERE with Specific ID

SQL
SELECT *
FROM Employees
WHERE ID = 4;
✓ OUTPUT
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
+----+-----------+------------+--------+-----------+

WHERE Is Not Just for SELECT

Statement What WHERE Does Example
SELECT ... WHERE Filters which rows to retrieve SELECT * FROM Emp WHERE ID = 1
UPDATE ... WHERE Filters which rows to modify UPDATE Emp SET Salary = 60000 WHERE ID = 1
DELETE ... WHERE Filters which rows to remove DELETE FROM Emp WHERE ID = 1

You will learn UPDATE in Chapter 10 and DELETE in Chapter 11.

💡 Important Warning

Running UPDATE or DELETE without a WHERE clause will affect every row in the table. Always double-check your WHERE condition before executing these statements.

Real-World WHERE Examples

Find employees in Mumbai:

SQL
SELECT Name, Salary, City
FROM Employees
WHERE City = 'Mumbai';
✓ OUTPUT
+-----------+--------+--------+
| Name      | Salary | City   |
+-----------+--------+--------+
| Priya K.  | 55000  | Mumbai |
| Ravi S.   | 72000  | Mumbai |
+-----------+--------+--------+

In the next chapter, you will learn how to combine multiple WHERE conditions using AND, OR, and NOT operators. For sorting your filtered results, see Chapter 7: SQL ORDER BY. For a complete reference of WHERE syntax and operators, see the official MySQL WHERE documentation.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write two queries:

  1. Find all employees whose salary is exactly 58,000
  2. Find all employees who do NOT work in Mumbai
ANSWER — Query 1
SELECT * FROM Employees
WHERE Salary = 58000;
ANSWER — Query 2
SELECT Name, City
FROM Employees
WHERE City <> 'Mumbai';

Expected Output (Query 2):

+-----------+-----------+
| Name      | City      |
+-----------+-----------+
| John D.   | London    |
| Arun M.   | Delhi     |
| Emma W.   | Sydney    |
| Sara L.   | New York  |
+-----------+-----------+

📝 What You Learned in This Chapter

  • The WHERE clause filters rows that match a specific condition
  • Text values must be in single quotes; numbers must not have quotes
  • Comparison operators: =, >, <, >=, <=, <>
  • WHERE works with SELECT, UPDATE, and DELETE statements
  • Always use WHERE with UPDATE and DELETE to avoid modifying all rows

Frequently Asked Questions

What does the WHERE clause do in SQL?
The WHERE clause filters rows based on a condition you specify. Only rows where the condition is TRUE are included in the output. It is used with SELECT, UPDATE, and DELETE statements.
Can I use multiple conditions in WHERE?
Yes. Combine conditions using AND, OR, and NOT operators. This is covered in detail in Chapter 6: SQL AND, OR, NOT.
Why do text values need single quotes?
Single quotes tell the database the value is a text string. Without quotes, the database tries to interpret it as a column name and returns an error.
What is the difference between = and == in SQL?
SQL uses a single equals sign (=) for comparison. There is no == in standard SQL. Use <> or != for not equal.
What happens if WHERE matches no rows?
The query returns an empty result set with zero rows. It does not cause an error — it simply means no data matched your condition.

📖 Chapter 5 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com