The Wayback Machine - https://web.archive.org/web/20240605004323/https://www.geeksforgeeks.org/sql-where-clause/
Open In App

SQL | WHERE Clause

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

WHERE keyword is used for fetching filtered data in a result set. It is used to fetch data according to particular criteria. WHERE keyword can also be used to filter data by matching patterns.

Syntax:

SELECT column1,column2 FROM table_name WHERE column_name operator value;

Parameter Explanation:

  1. column1,column2: fields in the table
  2. table_name: name of table
  3. column_name: name of field used for filtering the data
  4. operator: operation to be considered for filtering
  5. value: exact value or pattern to get related data in result 

List of Operators that Can be Used with WHERE Clause

Operator Description
> Greater Than
>= Greater than or Equal to
< Less Than
<= Less than or Equal to
= Equal to
<> Not Equal to
BETWEEN In an inclusive Range
LIKE Search for a pattern
IN To specify multiple possible values for a column

Query:

CREATE TABLE Emp1(
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Country VARCHAR(50),
Age int(2),
mob int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Emp1 (EmpID, Name,Country, Age, mob)
VALUES (1, 'Shubham', 'India','23','738479734'),
(2, 'Aman ', 'Australia','21','436789555'),
(3, 'Naveen', 'Sri lanka','24','34873847'),
(4, 'Aditya', 'Austria','21','328440934'),
(5, 'Nishant', 'Spain','22','73248679');
Select * from Emp1;

Where Clause with Logical Operators

To fetch records of  Employee with ages equal to 24. 

Query:

SELECT * FROM Emp1 WHERE Age=24;

Output:

 

To fetch the EmpID, Name and Country of Employees with Age greater than 21. 

Query:

SELECT EmpID, Name, Country FROM Emp1 WHERE Age > 21;

Output:

 

Where Clause with BETWEEN Operator

It is used to fetch filtered data in a given range inclusive of two values. 

Syntax: 

SELECT column1,column2 FROM table_name 

WHERE column_name BETWEEN value1 AND value2;

Parameter Explanation:

  1. BETWEEN: operator name 
  2. value1 AND value2: exact value from value1 to value2 to get related data in result set.  

To fetch records of Employees where Age is between 22 and 24 (inclusive).

Query:

SELECT * FROM Emp1 WHERE Age BETWEEN 22 AND 24;

Output:

 

Where Clause with LIKE Operator

It is used to fetch filtered data by searching for a particular pattern in the where clause. 

Syntax: 

SELECT column1,column2 FROM

table_name WHERE column_name LIKE pattern;

Parameters Explanation:

  1. LIKE: operator name 
  2. pattern: exact value extracted from the pattern to get related data in the result set. 

Note: The character(s) in the pattern is case-insensitive.

To fetch records of Employees where Name starts with the letter S.

Query:

SELECT * FROM Emp1 WHERE Name LIKE 'S%'; 

The ‘%'(wildcard) signifies the later characters here which can be of any length and value. 

Output:

 

To fetch records of Employees where Name contains the pattern ‘M’.

Query:

SELECT * FROM Emp1 WHERE Name LIKE '%M%';

Output:

 

Where Clause with IN Operator

It is used to fetch the filtered data same as fetched by ‘=’ operator just the difference is that here we can specify multiple values for which we can get the result set.

Syntax: 

SELECT column1,column2 FROM table_name WHERE column_name IN (value1,value2,..);

Parameters Explanation:

  1. IN: operator name 
  2. value1,value2,..: exact value matching the values given and get related data in the result set.

To fetch the Names of Employees where Age is 21 or 23.

Query:

SELECT Name FROM Emp1 WHERE Age IN (21,23);

Output:


Previous Article
Next Article

Similar Reads

SQL | Intersect &amp; Except clause
1. INTERSECT clause : As the name suggests, the intersect clause is used to provide the result of the intersection of two select statements. This implies the result contains all the rows which are common to both the SELECT statements. Syntax : SELECT column-1, column-2 …… FROM table 1 WHERE….. INTERSECT SELECT column-1, column-2 …… FROM table 2 WHE
1 min read
SQL | Sub queries in From Clause
From clause can be used to specify a sub-query expression in SQL. The relation produced by the sub-query is then used as a new relation on which the outer query is applied. Sub queries in the from clause are supported by most of the SQL implementations.The correlation variables from the relations in from clause cannot be used in the sub-queries in
2 min read
SQL | ON Clause
The join condition for the natural join is basically an EQUIJOIN of all columns with same name. To specify arbitrary conditions or specify columns to join, the ON Clause is used. The join condition is separated from other search conditions. The ON Clause makes code easy to understand. ON Clause can be used to join columns that have different names.
2 min read
SQL Distinct Clause
The distinct keyword is used in conjunction with the select keyword. It is helpful when there is a need to avoid duplicate values present in any specific columns/table. When we use distinct keywords only the unique values are fetched. Syntax: SELECT DISTINCT column1, column2 FROM table_name Parameters Used: column1, column2: Names of the fields of
3 min read
SQL | Union Clause
The Union Clause is used to combine two separate select statements and produce the result set as a union of both select statements. NOTE: The fields to be used in both the select statements must be in the same order, same number, and same data type.The Union clause produces distinct values in the result set, to fetch the duplicate values too UNION
3 min read
SQL | Except Clause
In SQL, EXCEPT returns those tuples that are returned by the first SELECT operation, and not returned by the second SELECT operation. This is the same as using a subtract operator in relational algebra. Example: Say we have two relations, Students and TA (Teaching Assistant). We want to return all those students who are not teaching assistants. The
1 min read
PostgreSQL - ORDER BY clause
The PostgreSQL ORDER BY clause is used to sort the result query set returned by the SELECT statement. As the query set returned by the SELECT statement has no specific order, one can use the ORDER BY clause in the SELECT statement to sort the results in the desired manner. Syntax: SELECT column_1, column_2 FROM table_name ORDER BY column_1 [ASC | D
2 min read
SQL Interview Questions | Set 2
Difference between Locking, Blocking and Deadlocking Locking: Locking occurs when a connection needs access to a piece of data in a database and it locks it for certain use so that no other transaction is able to access it. Blocking: Blocking occurs when a transaction tries to acquire an incompatible lock on a resource that another transaction has
5 min read
SQL using Python and SQLite | Set 2
Databases offer numerous functionalities by which one can manage large amounts of information easily over the web, and high-volume data input and output over a typical file such as a text file. SQL is a query language and is very popular in databases. Many websites use MySQL. SQLite is a "light" version that works over syntax very much similar to S
3 min read
Program for Fibonacci numbers in PL/SQL
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-1 with seed values F0= 0 and F1 = 1. Given a number n, print n-th Fibonacci Number. Input : n = 2 Output : 1 Inpu
1 min read
Article Tags :
Practice Tags :