Yandex

WHERE Clause
Filtering Data in SQL



Introduction

Imagine walking into a school library and asking for *every* book. Not practical, right? Instead, you specify — “Books by R.K. Narayan” or “Books for Class 10.” SQL works the same way. The WHERE clause lets you filter records and get only what matters.

Syntax of WHERE Clause

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Sample Table – Students

We’ll use this familiar table for examples:

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  age INT,
  city VARCHAR(30)
);

INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');

1. Simple WHERE Clause

Let’s fetch students from Class 9B:

SELECT name, class FROM students
WHERE class = '9B';
name           | class
----------------+-------
Diya Iyer      | 9B
Mehul Agarwal  | 9B

2. WHERE with Numeric Condition

Find students who are 15 years old:

SELECT name, age FROM students
WHERE age = 15;
name           | age
----------------+-----
Aarav Sharma   | 15
Rohit Menon    | 15

3. Using AND with WHERE

Retrieve students from Delhi in Class 9B:

SELECT name FROM students
WHERE city = 'Delhi' AND class = '9B';
name
------------
Mehul Agarwal

4. Using OR with WHERE

Get students from either Delhi or Pune:

SELECT name, city FROM students
WHERE city = 'Delhi' OR city = 'Pune';
name           | city
----------------+--------
Aarav Sharma   | Delhi
Sneha Patil    | Pune
Mehul Agarwal  | Delhi

5. Using NOT

Get students who are not from Delhi:

SELECT name, city FROM students
WHERE NOT city = 'Delhi';

6. Using BETWEEN

Find students aged between 14 and 15:

SELECT name, age FROM students
WHERE age BETWEEN 14 AND 15;

7. Using IN

Get students from specific cities:

SELECT name, city FROM students
WHERE city IN ('Delhi', 'Chennai', 'Kochi');

8. Using LIKE for Pattern Matching

Find students whose name starts with 'S':

SELECT name FROM students
WHERE name LIKE 'S%';
name
----------
Sneha Patil

9. WHERE with Comparison Operators

  • = (equal)
  • != or <> (not equal)
  • < (less than)
  • > (greater than)
  • <=, >=

Example: Students younger than 14

SELECT name, age FROM students
WHERE age < 14;
name          | age
----------------+-----
Sneha Patil   | 13

Best Practices

  • Use quotes for string values, but not for numbers.
  • Use parentheses to group conditions when mixing AND/OR.
  • Preview with SELECT before applying updates or deletes.

Summary

The WHERE clause is your precision tool in SQL. Whether you’re isolating top scorers, filtering students by class, or narrowing records by location — WHERE helps you focus on what truly matters.

What’s Next?

Now that you can filter your data confidently, it’s time to learn how to sort it — with the ORDER BY clause.

QUIZ

Question 1:What is the purpose of the WHERE clause in an SQL SELECT statement?

Question 2:The WHERE clause can be used with SELECT, UPDATE, and DELETE statements.

Question 3:Which of the following are valid WHERE clause usages in SQL?

Question 4:What does the following SQL query return?
SELECT name FROM students WHERE city = 'Hyderabad';

Question 5:The WHERE clause can include multiple conditions combined with AND and OR.

Question 6:Which of the following conditions are valid inside a WHERE clause?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M