×

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

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

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 COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 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 Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 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 SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.