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 Values SQL INSERT INTO SELECT SQL Insert multiple rows

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 Difference between Delete, Drop and Truncate in SQL

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 Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

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 How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples.

The WHERE clause in SELECT query displays those records as per the conditions returned in the queries. The WHERE clause filters the table data based on the expression in the given queries.

We can use comparison, logic, and other operation with the WHERE clause in the queries.

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.

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

Assume the following table, which has certain data.

Table Name 1- D_Students

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vineeta Sharma938885859592905
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Amit Sonar858382869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758375788076825
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485854
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078801
202224Aakash Jain807572748580832
202225Akshay Agarwal858378889082893
202226Shwetali Bhagwat908385889080861
202227Priya Wagh808385808285874
202228Saurabh Sangale858380908484895
202229Manthan Koli857584788280862
202230Mayur Jain808887909290881

Table Name 2- Department

Department_IdDepartment_Name
1Computer Engineering
2Information Technology
3Mechanical Engineering
4Automobile Engineering
5Civil Engineering
6Electrical Engineering
7Electronics and Tele-Communication Engineering

SELECT WHERE Clause with the Comparison Operators

Example 1: Execute a query to retrieve the information from the table where the second_semester percentage is 75.

SELECT Student_Id, Student_Name, First_Semester, Second_Semester, Third_Semester, Fourth_Semester, Fifth_Semester, Sixth_Semester, Total, Department_Id FROM D_Students WHERE Second_Semester = 75;  

In the above SQL SELECT WHERE clause example, we have displayed the student's information from the D_Students table whose second_semester percentage is 75. Here, we have used an equal comparison operator in the query.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202116Meena Mishra787580748577833
202223Anushka Sen707571748078801
202224Aakash Jain807572748580832
202229Manthan Koli857584788280862
SQL SELECT WHERE Clause

Example 2: Execute a query to retrieve the information from the table where the third_semester percentage is greater than 83.

SELECT Student_Id, Student_Name, First_Semester, Second_Semester, Third_Semester, Fourth_Semester, Fifth_Semester, Sixth_Semester, Total, Department_Id FROM D_Students WHERE Third_Semester > 83;   

In the above SQL SELECT WHERE clause example, we have displayed the student's information from the table whose third_semester percentage is greater than 83. Here, we have used a greater than comparison operator in the query.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vineeta Sharma938885859592905
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485854
202222Venkatesh Iyer908987909291903
202226Shwetali Bhagwat908385889080861
202227Priya Wagh808385808285874
202229Manthan Koli857584788280862
202230Mayur Jain808887909290881
SQL SELECT WHERE Clause

Example 3: Execute a query to retrieve the information from the table where the fourth_semester percentage is less than equal to 85.

SELECT Student_Id, Student_Name, First_Semester, Second_Semester, Third_Semester, Fourth_Semester, Fifth_Semester, Sixth_Semester, Total, Department_Id FROM D_Students WHERE Fourth_Semester <= 85;  

In the above SQL SELECT WHERE clause example, we have displayed the student's information from the table whose fourth_semester percentage is less than equal to 85. Here, we have used less than equal to the comparison operator in the query.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vineeta Sharma938885859592905
202112Vaibhav Lokhande859092808582862
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758375788076825
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485854
202223Anushka Sen707571748078801
202224Aakash Jain807572748580832
202227Priya Wagh808385808285874
202229Manthan Koli857584788280862
SQL SELECT WHERE Clause

SELECT WHERE Clause with the Logical Operators

Example 1: Execute a query to retrieve the information from the table where fourth_semester is less than 80 OR fifth_semester is greater than 85.

SELECT Student_Id, Student_Name, First_Semester, Second_Semester, Third_Semester, Fourth_Semester, Fifth_Semester, Sixth_Semester, Total, Department_Id FROM D_Students WHERE Fourth_Semester < 80 OR Fifth_Semester > 85;

In the above SQL SELECT WHERE clause example, we have displayed the student's information from the table whose fourth_semester percentage is less than 80 OR fifth_semester percentage is greater than 85.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vineeta Sharma938885859592905
202114Sonali Patole959092889290914
202115Amit Sonar858382869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758375788076825
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202220Prajwal Lokhande808585757880864
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078801
202224Aakash Jain807572748580832
202225Akshay Agarwal858378889082893
202226Shwetali Bhagwat908385889080861
202229Manthan Koli857584788280862
202230Mayur Jain808887909290881
SQL SELECT WHERE Clause

Example 2: Execute a query to retrieve the information from the table where fourth_semester is greater than 80 and fifth_semester is greater than 85.

SELECT Student_Id, Student_Name, First_Semester, Second_Semester, Third_Semester, Fourth_Semester, Fifth_Semester, Sixth_Semester, Total, Department_Id FROM D_Students WHERE Fourth_Semester > 80 AND Fifth_Semester > 85;

In the above SQL SELECT WHERE clause example, we have displayed the student's information from the table whose fourth_semester percentage is greater than 80 and fifth_semester percentage is greater than 85.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vineeta Sharma938885859592905
202114Sonali Patole959092889290914
202115Amit Sonar858382869284851
202222Venkatesh Iyer908987909291903
202225Akshay Agarwal858378889082893
202226Shwetali Bhagwat908385889080861
202230Mayur Jain808887909290881
SQL SELECT WHERE Clause

Example 3: Execute a query to retrieve the information from the table where the Total field starts with the number 8.

SELECT Student_Id, Student_Name, First_Semester, Second_Semester, Third_Semester, Fourth_Semester, Fifth_Semester, Sixth_Semester, Total, Department_Id FROM D_Students WHERE Total LIKE '8%';

In the above SQL SELECT WHERE clause example, we have displayed the student's information from the table where total student percentages start with the number 8.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202115Amit Sonar858382869284851
202117Mahesh Kumbhar758375788076825
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485854
202223Anushka Sen707571748078801
202224Aakash Jain807572748580832
202225Akshay Agarwal858378889082893
202226Shwetali Bhagwat908385889080861
202227Priya Wagh808385808285874
202228Saurabh Sangale858380908484895
202229Manthan Koli857584788280862
202230Mayur Jain808887909290881
SQL SELECT WHERE Clause

Example 4: Write a sub-query to retrieve the information from the table where department name is ‘Computer Engineering’, ‘Information Technology’, and Mechanical Engineering.

SELECT Student_Id, Student_Name, First_Semester, Second_Semester, Third_Semester, Fourth_Semester, Fifth_Semester, Sixth_Semester, Total, Department_Id FROM D_Students WHERE Department_Id IN (SELECT Department_Id FROM Department WHERE Department_Name IN ('Computer Engineering', 'Information Technology', 'Mechanical Engineering'));

In the above SQL SELECT WHERE clause example, we have displayed the student's information from the table where the student department name is 'Computer Engineering', 'Information Technology', and 'Mechanical Engineering'.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202115Amit Sonar858382869284851
202223Anushka Sen707571748078801
202226Shwetali Bhagwat908385889080861
202230Mayur Jain808887909290881
202112Vaibhav Lokhande859092808582862
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202224Aakash Jain807572748580832
202229Manthan Koli857584788280862
202113Yash Dhull908894878590893
202116Meena Mishra787580748577783
202222Venkatesh Iyer908987909291903
202225Akshay Agarwal858378889082893
SQL SELECT WHERE Clause

Example 5: Write a query to retrieve the information of those students from the table whose student name didn't start with the letter 'M'.

SELECT Student_Id, Student_Name, First_Semester, Second_Semester, Third_Semester, Fourth_Semester, Fifth_Semester, Sixth_Semester, Total, Department_Id FROM D_Students WHERE NOT Student_Name LIKE 'M%';

In the above SQL SELECT WHERE clause example, we have displayed the student's information of those students from the table whose student's name didn't start with the letter 'M'.

The following output is as follows:

Student_IdStudent_NameFirst_ SemesterSecond_ SemesterThird_ SemesterFourth_ SemesterFifth_ SemesterSixth_ SemesterTotalDepartment_ Id
202111Vineeta Sharma938885859592905
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Amit Sonar858382869284851
202118Sakshi Patil807874788077832
202119Sopan Bhore706875758080802
202220Prajwal Lokhande808585757880864
202221Anuja Wanare858886828485854
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078801
202224Aakash Jain807572748580832
202225Akshay Agarwal858378889082893
202226Shwetali Bhagwat908385889080861
202227Priya Wagh808385808285874
202228Saurabh Sangale858380908484895
SQL SELECT WHERE Clause