Lesson 17 of 60 – WHERE
28%

WHERE Clause in SQL

The WHERE clause is used to filter records in a SQL query. It allows you to retrieve only the rows that satisfy a specific condition.

Note: The WHERE clause is commonly used with SELECT, UPDATE, and DELETE statements to work with specific records.

1. What is WHERE?

Suppose a Students table contains many records. If you want to find only students who are 20 years old, you can use WHERE.

SELECT *
FROM Students
WHERE age = 20;

Only records where the age is 20 will be returned.

2. Basic WHERE Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

The condition determines which records should be returned.

3. WHERE with SELECT *

SELECT *
FROM Students
WHERE age = 21;

This retrieves all columns for students whose age is 21.

4. WHERE with One Column

SELECT name
FROM Students
WHERE age = 21;

Only the names of students whose age is 21 are returned.

5. WHERE with Text

Text values are normally written inside single quotes.

SELECT *
FROM Students
WHERE course = 'Python';

This retrieves students enrolled in Python.

6. WHERE with Numbers

SELECT *
FROM Students
WHERE age = 20;

Numeric values normally do not require quotation marks.

7. Equal (=) Operator

The = operator checks whether two values are equal.

SELECT *
FROM Students
WHERE age = 20;

8. Greater Than (>)

SELECT *
FROM Students
WHERE age > 20;

This returns students whose age is greater than 20.

9. Less Than (<)

SELECT *
FROM Students
WHERE age < 20;

This returns students whose age is less than 20.

10. Greater Than or Equal To (>=)

SELECT *
FROM Students
WHERE age >= 20;

This returns students whose age is 20 or greater.

11. Less Than or Equal To (<=)

SELECT *
FROM Students
WHERE age <= 20;

This returns students whose age is 20 or less.

12. Not Equal (<>)

The <> operator means not equal.

SELECT *
FROM Students
WHERE course <> 'Python';

This returns students whose course is not Python.

13. Not Equal (!=)

In MySQL, != can also be used for not equal.

SELECT *
FROM Students
WHERE age != 20;

14. WHERE with AND

Use AND when all specified conditions must be true.

SELECT *
FROM Students
WHERE age > 18
AND course = 'Python';

Both conditions must be satisfied.

15. WHERE with OR

Use OR when at least one condition should be true.

SELECT *
FROM Students
WHERE course = 'Python'
OR course = 'Java';

16. WHERE with NOT

The NOT operator reverses a condition.

SELECT *
FROM Students
WHERE NOT course = 'Python';

This returns students whose course is not Python.

17. WHERE with BETWEEN

The BETWEEN operator checks whether a value is within a range.

SELECT *
FROM Students
WHERE age BETWEEN 18 AND 25;

The boundary values are included.

18. WHERE with IN

The IN operator allows multiple possible values.

SELECT *
FROM Students
WHERE course IN ('Python', 'Java', 'PHP');

19. WHERE with LIKE

The LIKE operator is used for pattern matching.

SELECT *
FROM Students
WHERE name LIKE 'A%';

This finds names beginning with the letter A.

20. WHERE with IS NULL

Use IS NULL to find records where a column has a NULL value.

SELECT *
FROM Students
WHERE mobile IS NULL;

21. WHERE with IS NOT NULL

SELECT *
FROM Students
WHERE mobile IS NOT NULL;

This returns records where the mobile column contains a non-NULL value.

22. WHERE with ORDER BY

WHERE can be combined with ORDER BY.

SELECT *
FROM Students
WHERE age > 18
ORDER BY age DESC;

First the records are filtered, then the result is sorted.

23. WHERE with LIMIT

SELECT *
FROM Students
WHERE course = 'Python'
LIMIT 5;

In MySQL, this returns up to five matching records.

24. WHERE with UPDATE

WHERE is very important when updating specific records.

UPDATE Students
SET fee = 18000
WHERE id = 1;

Only the student whose ID is 1 will be updated.

Warning: Always use a suitable WHERE condition when updating specific records. Without WHERE, an UPDATE statement can affect every row in the table.

25. WHERE with DELETE

WHERE can also be used to delete a specific record.

DELETE FROM Students
WHERE id = 5;

Only the record with ID 5 is targeted.

Warning: A DELETE statement without WHERE can delete all records from a table.

26. Multiple Conditions

You can combine several conditions.

SELECT *
FROM Students
WHERE age >= 18
AND course = 'Python'
AND city = 'Patna';

All three conditions must be satisfied.

27. Using Parentheses

Parentheses can make complex conditions easier to understand and control the logical grouping.

SELECT *
FROM Students
WHERE age > 18
AND (course = 'Python' OR course = 'Java');

This means the student must be older than 18 and must be enrolled in either Python or Java.

28. WHERE with Decimal Values

WHERE can also filter decimal values such as fees.

SELECT *
FROM Students
WHERE fee > 10000;

This returns students whose fee is greater than 10000.

29. WHERE with Dates

You can filter records using date values.

SELECT *
FROM Students
WHERE admission_date = '2026-09-20';

This returns records with the specified admission date.

30. Complete WHERE Example

SELECT name, course, fee
FROM Students
WHERE age >= 18
AND fee > 5000
AND course IN ('Python', 'Java')
ORDER BY fee DESC
LIMIT 10;

This query:

  • Selects the name, course, and fee columns.
  • Finds students aged 18 or above.
  • Filters students whose fee is greater than 5000.
  • Allows Python or Java courses.
  • Sorts the results by fee from highest to lowest.
  • Returns up to 10 records.

📌 Key Points

  • WHERE is used to filter records.
  • WHERE can be used with SELECT, UPDATE, and DELETE.
  • Use = to check equality.
  • Use >, <, >=, and <= for comparisons.
  • Use <> or != for not equal.
  • AND requires all conditions to be true.
  • OR requires at least one condition to be true.
  • BETWEEN checks a range.
  • IN checks multiple possible values.
  • LIKE is used for pattern matching.
  • IS NULL finds NULL values.
  • Always use a suitable WHERE condition when updating or deleting specific records.

🧠 Quick Quiz

Question: Which SQL clause is used to filter records according to a condition?