×

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with the queries to filter the data from billions or trillions of records. This clause is an optional part of a SELECT query, the DELETE query, and the UPDATE query.

For example, you have thousands of records on the college record track from college, and you want a final year students list of computer department, then you will use a search bar to list out computer department final year students. In the same way, the database uses the WHERE clause to search the records from the tables.

The WHERE clause syntax with the SELECT statement is as follows:

SELECT * FROM Table_Name WHERE conditions;

The Where clause in the SELECT statement is used to fetch the records from the table, but if you want to change specific records or eliminate the specific records from the table, you can use the WHERE clause in the UPDATE statement and DELETE statement.

The WHERE clause syntax with the UPDATE statement is as follows:

UPDATE Table_Name SET Column_Name = values WHERE conditions;

The WHERE clause syntax with the DELETE statement is as follows:

DELETE FROM Table_Name WHERE conditions;  

Let’s understand the SQL WHERE Clause with the help of examples.

Assume the following table, which has certain data.

Table Name: D_Students

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881

Example 1: Write a query to fetch student information from the D_Students table where the First_Semester percentage is 85:

SELECT * FROM D_Students WHERE First_Semester = 85;

In the above WHERE clause example, we have displayed the student's information from the table whose first_semester percentage is 85.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202112Vaibhav Lokhande859092808582862
202115Axar Patel858082869284851
202221Anuja Wanare858886828485857
202225Akshay Agarwal858078889082847
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
SQL WHERE Clause

Example 2: Write a query to fetch student information from the D_Students table where First_Semester percentage is 85 or Sixth_Semester percentage is greater than 80:

SELECT * FROM D_Students WHERE First_Semester = 85 OR Sixth_Semester > 80;

In the above WHERE clause example, we have displayed the student's information from the table whose first_semester percentage is 85 or sixth_semester percentage is greater than 80.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202225Akshay Agarwal858078889082847
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
SQL WHERE Clause

Example 3: Write a query to modify the student’s information from the D_Students table where the student total percentage is less than 85.

UPDATE D_Students SET Total = Total + 5 WHERE Total < 85;

In the above WHERE clause example, we have modified the student’s information from the table whose total percentage is less than 85.

We will verify whether the record is successfully modified or not by executing the below query:

SELECT * FROM D_Students;

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577833
202117Mahesh Kumbhar758075788076825
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078801
202224Aakash Jain807572748580837
202225Akshay Agarwal858078889082897
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285874
202228Utkarsh Rokade858080908484895
202229Manthan Koli857584788280862
202230Mayur Jain808887909290881
SQL WHERE Clause

Example 4: Write a query to modify the student's information from the D_Students table where the student Second_Semester percentage is 80.

UPDATE D_Students SET Second_Semester = 83 WHERE Second_Semester = 80;

In the above WHERE clause example, we have modified the student’s information from the table whose second_semester percentage is 80.

We will verify whether the record is successfully modified or not by executing the below query:

SELECT * FROM D_Students;

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858382869284851
202116Meena Mishra787580748577833
202117Mahesh Kumbhar758375788076825
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078801
202224Aakash Jain807572748580837
202225Akshay Agarwal858378889082897
202226Shwetali Bhagwat908385889080866
202227Mayuri Wagh808385808285874
202228Utkarsh Rokade858380908484895
202229Manthan Koli857584788280862
202230Mayur Jain808887909290881
SQL WHERE Clause

Example 5: Write a query to remove the student's information from the D_Students table where the student Second_Semester percentage is 83.

DELETE FROM D_Students WHERE Second_Semester = 83;

In the above WHERE clause example, we have deleted the student’s information from the table whose second_semester percentage is 83.

We will verify whether the record is successfully removed or not by executing the below query:

SELECT * FROM D_Students;

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202116Meena Mishra787580748577833
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078801
202224Aakash Jain807572748580837
202229Manthan Koli857584788280862
202230Mayur Jain808887909290881
SQL WHERE Clause

Example 6: Write a query to remove the student's information from the D_Students table where the student department id is 2.

DELETE FROM D_Students WHERE Department_Id = 2;

In the above WHERE clause example, we have deleted the student’s information from the table whose department id is 2.

We will verify whether the record is successfully removed or not by executing the below query:

SELECT * FROM D_Students;

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vaishnavi Patil949188859592916
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202116Meena Mishra787580748577833
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078801
202224Aakash Jain807572748580837
202230Mayur Jain808887909290881
SQL WHERE Clause

Related Topics

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.