SQL WHERE Clause | What is the WHERE Clause in SQL? with Example

SQL WHERE ClauseIn this article, you will learn how to use them SQL WHERE Clause with syntax and example, Therefore when we want to restrict the SQL query results to a specified with the condition. then SQL WHERE clause comes in handy in such situations. therefore, The WHERE clause SQL to filter rows returned by a query.

.
Note: The WHERE clause SQL is used to filter the records.
The SQL Server WHERE clause is used to extract only those records that fulfill the specified condition.

Introduction: SQL WHERE Clause

The SQL WHERE clause is not only used in the SELECT statement, WHERE Clause also used to filter the results and apply one or multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement, etc.

The SQL WHERE clause is used to specify a condition to get the rows from the table that satisfy one or more conditions, or while fetching the data from a single table or by joining with multiple tables. furthermore, If the given condition is satisfied, then only it returns the specific value from the table. You should use the SQL WHERE clause to filter the records and fetching only the necessary records.

Therefore, When you use the SQL SELECT statement to query data against the table, then you get all the rows of that table, which is unnecessary because the application may only process a set of the rows at the time.

Syntax:

There is the basic syntax of the SELECT statement with the SQL WHERE clause is as shown below:

SELECT column1, column2, columnN
FROM table_name
WHERE condition;

Furthermore, you can specify the condition using the logical or comparison operators like >, <, =, LIKE, NOT, etc. The given below examples would make this concept clear.

Sample Demo Table: Employees

EmployeeID Emp_Name Father_Name City Salary
21 Benjamin Thakur Elijah Connor Nagpur 25000
22 Logan Aaron Liam Isaiah Mumbai 35000
23 James Hudson Mason Ezra Sultanpur 30000
24 William Josiah Benjamin Hunter Meerut 28000
25 Noah Charles Ethan Adrian Jhansi 32000
26 Jacob Jonathan Henry Easton Agra 36000
27 Jackson Elias John Greyson Lucknow 38000
28 Kapil Gupta Sebastian Colton Barabanki 40000
29 Luke Austin Owen Dominic Allahabad 20000
30 Jack Adam Grayson Jordan Delhi 40000
31 Gabriel Roman Dylan Santiago Noida 40000
32 Anthony Xavier Jaxon Jose Surajpur 45000
33 Christopher Leonardo Andrew Bryson Amritsar 20000
34 Joshua Jameson Theodore Axel Jalandhar 26000
35 Thomas sawyer Nathan Miles Patna 38000

Example: SQL WHERE Clause

There are the given below following SQL statement selects all the Employee from the City “Amritsar”, in the "Employees" table:


WHERE Clause

SQL WHERE: Numeric Fields vs Text Field

Therefore, the SQL requires only single quotes around text values (most of the database systems will also allow double quotes).
However, the numeric fields should not be enclosed in quotes:


Text field vs Numeric field output


Example: WHERE clause SQL combined with AND LOGICAL Operator

therefore, the SQL WHERE clause when used together with the AND logical operator, is only executed if ALL filter criteria specified are met.
Let’s see the example – Suppose we want to get a record of all the Employees the table in EmployeeID 24 and where Father_Name is Benjamin Hunter, we would use the SQL script shown below to achieve that.


AND LOGICAL Operator

Example: WHERE clause combined with OR LOGICAL Operator

The WHERE clause SQL, when used together with the OR operator, furthermore, it is only executed if any or the entire specified filter criteria are met.
There are the given below the following script gets all the employee in either Salary 40000 or salary 36000


OR LOGICAL Operator

Example: SQL WHERE clause combined with IN Keyword

The SQL WHERE clause when used together with the IN keyword only affects the rows whose values match the list of values provided in the IN keyword. therefore, IN keyword help reduces the number of OR clauses you may have to use
Therefore, there are the given below the following query gives rows where EmployeeID is either 22, 24, 28, 32 or 35


IN Keyword

Example: SQL WHERE clause combined with NOT IN Keyword

The SQL WHERE clause, when used together with the NOT IN keyword, does not affects the rows whose values match the list of values provided in the NOT IN keyword.
Therefore, there are the given below the following query gives rows where EmployeeID is either 25, 27, 29, 31, 34, or 35.


NOT IN Keyword

Example: WHERE clause SQL combined with COMPARISON OPERATORS

therefore, The Greater than(>), less than (<), equal to (=), not equal to (< >) all comparison operators can be used with the Where clause SQL.
= Equal To
Therefore, the given below the following script gets all the salary records from the employee table using the equal to a comparison operator.


Equal to operator
< Less than
Furthermore, There are the given below the following script gets all the Salaries that are Less than 40000 from the Employees table.


Less then operator in SQL
> Greater than
Furthermore, There are the given below the following script gets all the Salaries that are Greater than 40000 from the Employees table.


greater then operator in SQL
< > Not Equal To
Furthermore, There are the given below following SQL script gets all the Employees whose salary is not 40000.


NOT Equal to operator in SQL

SQL WHERE Clause Condition allowed Operators

Therefore, in the above examples, you have seen that where clause SQL allowed operators such as <, >, AND, =. Furthermore, There are given below is the complete list of operators that we can be used in the SQL where clause.

.
Operators List: > Greater than operator
< Less than operator
= Equal operator
>= Greater than or equal
<= Less than or equal
< > Not equal.
IN keyword- To specify a set of values
BETWEEN- To specify a range of values
LIKE- To specify a pattern

Summary: WHERE Clause

The WHERE clause SQL is used to restrict the number of rows affected by a SELECT, UPDATE, or DELETE query.
The WHERE clause can be used in conjunction with the logical operators such as AND and OR, and the comparison operators such as =, etc.
When used with the AND logical operator when all the criteria must be met.
furthermore, When used with the OR logical operator, when any of the criteria must be met.
The SQL keyword IN is used to select rows matching a list of values.
I hope you will enjoy this article, if have any query regarding this article, you can mail me your query, I will definitely resolve the problem.