×

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

Related Topics

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

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.

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 CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 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 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 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 Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 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.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 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.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.