SQL Server · Querying

WHERE

WHERE decides which rows make it into your result. Mastering it is the key to answering real questions with SQL.

Beginner 5 min Lesson 6 of 31Examples run on SQL Server 2022 and Azure SQL Database.
Before you start:SELECT
0/31 in SQL Server
On this page · 5 min · Beginner

In simple words#

WHERE is the filter on the clerk’s search: “…only students from Class 10 whose fees are pending.” Rows that don’t match are left out.

WHERE
Filter rows by a condition.
AND / OR
Combine conditions.
LIKE / IN / BETWEEN
Pattern, list and range filters.

Learning objectives#

  • Filter with comparison operators
  • Combine conditions with AND, OR and parentheses
  • Use IN, BETWEEN and LIKE
  • Test for NULL correctly

Explanation#

WHERE keeps rows for which the condition is true. Operators include =, <> (not equal), <, >, <= and >=.

IN (a, b, c) matches any value in a list; BETWEEN x AND y is inclusive at both ends; LIKE matches patterns where % means “any characters” and _ means “one character”.

NULL means “unknown”. Comparisons with NULL are never true, so use IS NULL and IS NOT NULL.

AND binds tighter than OR, so use parentheses when mixing them.

Syntax#

SQL Illustrative
SELECT ... FROM ... WHERE condition1 AND (condition2 OR condition3);

Code example: Mid-priced furniture and stationery#

SQLMid-priced furniture and stationery Illustrative
SELECT Name, Category, Price
FROM Products
WHERE Category IN ('Furniture', 'Stationery')
  AND Price BETWEEN 50 AND 5000;


SELECT Name FROM Products WHERE Name LIKE 'N%';
Expected output
Name      Category    Price
Chair     Furniture   4500.00
Notebook  Stationery  60.00

Name
Notebook

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. IN keeps only Furniture and Stationery products (Desk, Chair, Pen, Notebook).
  2. BETWEEN 50 AND 5000 removes Desk (12000) and Pen (20).
  3. LIKE 'N%' matches names that start with N.

Real-world use case#

Search filters on a shopping site — category, price range and “name contains” — become WHERE conditions.

Common mistakes#

WHERE Column = NULL

This is never true. Write WHERE Column IS NULL.

Mixing AND/OR without parentheses

WHERE Category = 'A' OR Category = 'B' AND Price < 100 applies the price filter only to B.

Leading wildcards on large tables

LIKE '%phone' cannot use an index seek. Prefer LIKE 'phone%' or full-text search.

Best practices#

  • Use parentheses to make intent explicit
  • Use IS NULL / IS NOT NULL
  • Keep columns un-wrapped by functions so indexes can be used
  • Pass values as parameters from applications

Remember this#

WHERE filters rows before anything else.

= NULL never works — use IS NULL.

LIKE 'A%' = starts with A.

Practice exercise#

Your task: Find Electronics products that cost less than 1000.

Show solution
SQL Illustrative
SELECT Name, Price FROM Products WHERE Category = 'Electronics' AND Price < 1000;  -- Mouse 700.00

Mini quiz#

WHERE quizMultiple choice Question 1 of 2

Is BETWEEN 50 AND 100 inclusive?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.