The WHERE clause is one of the most frequently used features in SQL. It allows you to filter records and retrieve only the data that matches a specific condition.
Without the WHERE clause, a query returns all rows from a table. By using WHERE, you can search for particular records, making your queries faster, more accurate, and more useful.
Whether you’re building reports, analyzing data, or developing applications, the WHERE clause is an essential SQL skill. SQL WHERE Clause practice questions with solutions help to understand the concepts.
What is the SQL WHERE Clause?
The WHERE clause filters records based on one or more conditions.
Basic Syntax
SELECT column_name
FROM table_name
WHERE condition;
Sample Table Used Throughout This Chapter
students
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Why Use the WHERE Clause?
The WHERE clause is used to:
- Retrieve specific records
- Filter unwanted data
- Improve query performance
- Generate customized reports
- Search for matching values
- Work with numeric, text, and date data
Comparison Operators Used with WHERE
| Operator | Meaning |
|---|---|
| = | Equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| <> or != | Not equal to |
Example
SELECT *
FROM students
WHERE marks > 90;
1. SQL Query to Display Students from Delhi
Problem Statement
Write an SQL query to display all students whose city is Delhi.
SQL Solution
SELECT *
FROM students
WHERE city = 'Delhi';
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
Explanation
The WHERE clause filters only those rows where the city column is equal to 'Delhi'.
Rows that do not satisfy the condition are excluded from the result.
Concepts Covered
- WHERE Clause
- Equality Operator (
=) - Text Filtering
2. SQL Query to Display Students with Marks Greater Than 90
Problem Statement
Write an SQL query to display students who scored more than 90 marks.
SQL Solution
SELECT *
FROM students
WHERE marks > 90;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
Explanation
The greater-than (>) operator returns rows where the marks value is greater than 90.
Only matching records appear in the output.
Concepts Covered
- WHERE Clause
- Greater Than Operator (
>) - Numeric Filtering
3. SQL Query to Display Students Aged 21
Problem Statement
Write an SQL query to display students whose age is 21.
SQL Solution
SELECT *
FROM students
WHERE age = 21;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
The equality operator (=) compares the age column with the value 21.
Only students who are exactly 21 years old are returned.
Concepts Covered
- WHERE Clause
- Equality Operator
- Numeric Comparison
4. SQL Query to Display Students Studying Python
Problem Statement
Write an SQL query to display all students enrolled in the Python course.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
SQL Solution
SELECT *
FROM students
WHERE course = 'Python';
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
Explanation
The WHERE clause filters only those rows where the course column contains the value Python.
All students enrolled in other courses are excluded.
Concepts Covered
- WHERE Clause
- String Comparison
- Equality Operator (
=)
5. SQL Query to Display Students from Gurgaon
Problem Statement
Write an SQL query to display all students whose city is Gurgaon.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
SQL Solution
SELECT *
FROM students
WHERE city = 'Gurgaon';
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 104 | Priya | 23 | Python | Gurgaon | 84 |
Explanation
This query retrieves only the rows where the city column is equal to Gurgaon.
If no records match the condition, SQL returns an empty result set.
Concepts Covered
- WHERE Clause
- Text Filtering
- Equality Operator
6. SQL Query to Display Students with Marks Less Than 90
Problem Statement
Write an SQL query to display students whose marks are less than 90.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
SQL Solution
SELECT *
FROM students
WHERE marks < 90;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
Explanation
The less than (<) operator returns only those rows where the value in the marks column is below 90.
Rows with marks equal to or greater than 90 are excluded.
Concepts Covered
- WHERE Clause
- Less Than Operator (
<) - Numeric Filtering
7. SQL Query to Display Students with Marks Greater Than or Equal to 90
Problem Statement
Write an SQL query to display students whose marks are greater than or equal to 90.
SQL Solution
SELECT *
FROM students
WHERE marks >= 90;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
The greater than or equal to (>=) operator includes rows where the value is either 90 or greater.
This makes it useful when you want to include a boundary value in your search.
Concepts Covered
- WHERE Clause
- Greater Than or Equal To (
>=) - Numeric Comparison
8. SQL Query to Display Students with Age Less Than 22
Problem Statement
Write an SQL query to display students whose age is less than 22.
SQL Solution
SELECT *
FROM students
WHERE age < 22;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
This query filters students whose age is 21 or below.
The less than (<) operator excludes students who are exactly 22 years old.
Concepts Covered
- WHERE Clause
- Less Than Operator
- Numeric Filtering
9. SQL Query to Display Students with Age Greater Than 20
Problem Statement
Write an SQL query to display students whose age is greater than 20.
SQL Solution
SELECT *
FROM students
WHERE age > 20;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
The greater than (>) operator retrieves students older than 20 years.
Students who are exactly 20 years old are not included.
Concepts Covered
- WHERE Clause
- Greater Than Operator
- Numeric Filtering
10. SQL Query to Display Students Enrolled in Java
Problem Statement
Write an SQL query to display all students enrolled in the Java course.
SQL Solution
SELECT *
FROM students
WHERE course = 'Java';
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
The WHERE clause compares the course column with the text value Java.
Only the records matching the specified course are returned.
Concepts Covered
- WHERE Clause
- Text Comparison
- Equality Operator (
=)
11. SQL Query Using != (Not Equal To)
Problem Statement
Write an SQL query to display all students except those enrolled in the Python course.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
SQL Solution
SELECT *
FROM students
WHERE course != 'Python';
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
The != operator means not equal to.
Only records where the course is not Python are returned.
Note: Some databases prefer the
<>operator. MySQL supports both.
Concepts Covered
- WHERE Clause
- Not Equal Operator (
!=) - Text Filtering
12. SQL Query Using <> (Not Equal To)
Problem Statement
Write an SQL query to display students who do not belong to Delhi.
SQL Solution
SELECT *
FROM students
WHERE city <> 'Delhi';
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
The <> operator also means not equal to.
It is the ANSI SQL standard operator and works across most relational database systems.
Concepts Covered
- WHERE Clause
- ANSI SQL
- Not Equal Operator (
<>)
13. SQL Query Using AND
Problem Statement
Write an SQL query to display students who belong to Delhi and have marks greater than 90.
SQL Solution
SELECT *
FROM students
WHERE city = 'Delhi'
AND marks > 90;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 103 | Neha | 20 | SQL | Delhi | 95 |
Explanation
The AND operator combines two or more conditions.
A record is returned only if every condition is true.
In this example:
- City must be Delhi
- Marks must be greater than 90
Since only Neha satisfies both conditions, only one row is returned.
Concepts Covered
- WHERE Clause
- AND Operator
- Multiple Conditions
14. SQL Query Using OR
Problem Statement
Write an SQL query to display students who belong to Delhi or are enrolled in the Java course.
SQL Solution
SELECT *
FROM students
WHERE city = 'Delhi'
OR course = 'Java';
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
The OR operator returns rows where at least one condition is true.
A student will appear in the result if:
- They belong to Delhi, or
- They study Java.
Concepts Covered
- WHERE Clause
- OR Operator
- Logical Operators
15. SQL Query Using Multiple Conditions
Problem Statement
Write an SQL query to display students who:
- Study Java
- Have marks greater than or equal to 90
- Are older than 20 years
SQL Solution
SELECT *
FROM students
WHERE course = 'Java'
AND marks >= 90
AND age > 20;
Sample Output
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 102 | Amit | 22 | Java | Noida | 91 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
Explanation
This query combines three conditions using the AND operator.
A record is returned only when all three conditions are satisfied.
Such queries are very common in real-world business applications.
Examples include:
- Employees in a specific department with a salary above a certain amount.
- Customers from a particular city who placed recent orders.
- Students enrolled in a course with marks above a cutoff.
Concepts Covered
- WHERE Clause
- AND Operator
- Multiple Condition Filtering
- SQL Logic
Chapter Summary
In this chapter, you learned how to use the SQL WHERE clause to filter records based on specific conditions. Instead of retrieving every row from a table, the WHERE clause helps you return only the data that matches your requirements.
You explored different comparison operators such as =, >, <, >=, <=, !=, and <>, along with logical operators like AND and OR. These operators allow you to create powerful SQL queries for searching, reporting, and analyzing data.
The WHERE clause is one of the most frequently used features in SQL and is essential for building efficient database queries.
Throughout this chapter, you covered:
- Introduction to the
WHEREClause - Equality (
=) - Greater Than (
>) - Less Than (
<) - Greater Than or Equal To (
>=) - Less Than or Equal To (
<=) - Not Equal To (
!=) - ANSI Standard Not Equal (
<>) - Logical
AND - Logical
OR - Multiple Condition Filtering
These concepts are the building blocks for more advanced SQL operations such as ORDER BY, GROUP BY, HAVING, and JOIN.
Key Takeaways
- The
WHEREclause filters records based on conditions. =is used to find exact matches.>and<compare numeric values.>=and<=include boundary values.!=and<>are used to find records that do not match a value.ANDrequires all conditions to be true.ORrequires at least one condition to be true.- Multiple conditions can be combined to create powerful queries.
- Filtering data improves performance and generates meaningful reports.
- The
WHEREclause is one of the most commonly used SQL features.
Frequently Asked Questions (FAQs)
1. What is the SQL WHERE clause?
The WHERE clause filters rows based on a condition.
Example:
SELECT *
FROM students
WHERE city = 'Delhi';
2. Can I use multiple conditions in a WHERE clause?
Yes.
Example:
SELECT *
FROM students
WHERE city = 'Delhi'
AND marks > 90;
3. What is the difference between AND and OR?
AND
All conditions must be true.
WHERE age > 20
AND marks > 80;
OR
At least one condition must be true.
WHERE city = 'Delhi'
OR city = 'Noida';
4. What is the difference between != and <>?
Both operators mean Not Equal To.
WHERE course != 'Python';
WHERE course <> 'Python';
The <> operator is part of the ANSI SQL standard and is supported by most database systems.
5. Can WHERE be used with text values?
Yes.
Example:
SELECT *
FROM students
WHERE course = 'Java';
6. Can WHERE be used with numbers?
Yes.
Example:
SELECT *
FROM students
WHERE marks >= 90;
7. Why is the WHERE clause important?
The WHERE clause helps you:
- Filter unnecessary records
- Improve query performance
- Generate accurate reports
- Search specific data
- Analyze business information efficiently
8. Where is the WHERE clause used in real-world applications?
The WHERE clause is used in:
- Banking Applications
- Hospital Management Systems
- Employee Management Systems
- Student Management Systems
- E-commerce Websites
- Inventory Management
- CRM Software
- ERP Systems
- Reporting Dashboards
- Business Intelligence Tools
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
