SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO SELECT

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL

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



ADVERTISEMENT
ADVERTISEMENT