In this article, you will learn the use of the where
a clause in SQL Server
Where clause is used to filter the records from data set when we query the table that time you will get the list of records, which are unnecessary when the application needs a specific set of records.
Syntax:
SELECT column_list
FROM table_name
WHERE search_condition;
In the above syntax, in where
the clause, we filter the result set return from
clause. Where
clause only filter the records when search condition to evaluate true
. In where
condition, we need to add logical expression.
where condition evaluates to TRUE
,FALSE
or UNKNOWN
. The where clause won’t return any rows when search_condition evaluate false
or unknown
For the examples, we will use the below table
Searching records using single condtion
In the below example, will filter the records using employee id
SELECT *
FROM [Blog].[dbo].[Employee] where EmployeeID=1
Output:
Searching records using multiple condtion
In this example, we can filter records based on the <strong>Salary</strong>
and <strong>JoinYear</strong>
conditions along with AND and OR logical operators.
AND Condition
SELECT *
FROM [Blog].[dbo].[Employee] where Salary=10000 AND JoinYear=2018
Output:
OR Condition
SELECT *
FROM [Blog].[dbo].[Employee] where Salary=10000 OR JoinYear=2018
Output:
Filtering records using comparison operator
You can filter the records, using a comparison operator. I can demonstrate in the below example
SELECT *
FROM [Blog].[dbo].[Employee] where Salary>10000
We are filtering records greater than 10000.
Output:
Filtering records between two records
Using between
clause, we can get the range between two values, now I need records between salary 10k and 25k.
SELECT *
FROM [Blog].[dbo].[Employee]
where Salary between 10000 and 25000
Output:
Filtering records using List
Using the IN
operator, we can filter result set using the multiple records in the search condition(1,5)
SELECT *
FROM [Blog].[dbo].[Employee]
where EmployeeID in (1,5)
Output:
Filtering records contain a string
Using the wildcard operator like
, we can get records containing a specific string, In the below example we are searching rows containing a character.
SELECT *
FROM [Blog].[dbo].[Employee]
where [Employee Name] like '%a%'
Output:
Need help?
Read this post again, if you have any confusion or else add your questions in Community