SQL WHERE Clause

SQL WHERE Clause

This is a detailed guide of the SQL Where Clause. To Filter the database records according to condition(s), WHERE Clause is used in the different SQL Statements.

Use of SQL WHERE Clause

Whenever we need to filter the database records according to one or more conditions then we make use of the WHERE Clause. It is used along with different SQL Statements including SELECT, UPDATE and DELETE statements. The conditions can be anything. When using a WHERE clause it only considers those records for the SQL statement to be executed for which the Condition is true and to check whether the condition is True or False, we make use of the SQL operators.

WHERE Clause Syntax

SQL Clause is used along with other SQL Statements like SELECT, UPDATE, DELETE, etc. We’ll use the SELECT statement for learning purposes but keep in mind you can use the same for other SQL statements as well.

The above SQL SELECT statement will select all the rows contained in the database table named as table_name for the columns col_1 and col_2 for which the condition is True.

WHERE Clause Operators

Before you start executing your SQL queries with the WHERE clause, make sure you know all the operators that can be used to check the conditions. Given below is the complete list of SQL WHERE clause operators in tabular form.

OperatorDescription
=Equal To
<Less Than
>Greater Than
>=Greater Than Or Equal To
<=Less Than Or Equal To
<> or !=Not Equal To
BETWEENIf Lies Between A Certain Range
LIKEIf matches with a Pattern
INChecks for multiple column value possibilities

SQL WHERE Example

Again for example for the demonstration of WHERE clause, we’ll use the SELECT statement to filter and display the records of the database table that satisfies our condition.

Given below is the screenshot of the database table on which we’ll perform the SELECT query along with the WHERE clause.

Demo Database Table

The table name is employees and there are 6 records in this database table.

Query 1.

Result 1.

The above query will select only those table rows where the value of the description column is exactly Punjabi Teacher. The screenshot of the result is given below.

SQL WHERE Clause Example

The following query is another WHERE clause example in which we’re fetching only that row WHERE the value of the dob column is 2009-05-14.

Query 2.

Result 2.

SQL WHERE Example 2

WHERE Clause Numerical & Textual Column Values Match

Check carefully to write the column values in the SQL query we’re using either single quotes (‘) or double quotes (“) to write the values. But in case you want to check the column values which are purely numerical, there is no need to use these quotes. But only in the case if the column value is purely numerical such as the int data type values. Even the date is not purely numerical and you won’t get the right results if you won’t use quotes in date.

As ID is a purely numerical field with its data type as int, we can use write the numerical value directly in the WHERE clause condition without any quotes.

Another example for the numerical field is the mobile_no field. So to match the values for this column, we need not use the quotes.

But for column likes first_name, last_name, description and email, we have to make use of the single or double quotes to get the right records fetched.

I hope you find this guide useful. If so, do share it with others who are willing to learn SQL. If you have any questions related to this article, feel free to ask them in the comments section.

Helpful Associated Guide.

Also, don’t forget to Subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *