Black Friday Deal : Up to 40% OFF! + 2 free self-paced courses + Free Ebook - SCHEDULE CALL
What do you do when you want to remove impurities from drinking water? Or what do you do when you want to remove tea leaves from your Tea? You use a strainer. What does a strainer do? It filters out the tea leaves or any other impurities from your tea and gives you a pure leaker. Similarly, how do you extract relevant data from a pool of data in SQL Server? You use SQL Filters. SQL filters are text strings that you use to specify a subset of the data items in an internal or SQL database data type. For SQL databases and internal data types, the filter is an SQL WHERE clause that provides a set of comparisons that must be true for a data item to be returned. Over the next few paragraphs, we will discuss the different aspects of filters, its advantage and disadvantage, if any, and when we should use database filters. However, if you are willing to opt for a career in SQL, consider enrolling in a certified and industry-recognized online SQL server training course and shape your ever-growing SQL career.
Let us start with the different types of filter operators. Below is a list of different types of filter operators. Once we check them out, we will see practical examples of these operators.
Filter |
Description |
Is Equal To |
Returns attribute values that are exactly the same as the specified criteria. |
Is Not Equal To |
Returns attribute values that are not exactly the same as the specified criteria. The filter criteria must be exactly the same as the attribute value you want to omit from the results. |
Is Like |
Uses the LIKE operator from Transact-SQL to filter results. |
Is Not Like |
Uses the NOT operator from Transact-SQL to filter results. |
Is Greater Than |
Returns attribute values that are greater than the specified criteria. |
Is Less Than |
Returns attribute values that are less than the specified criteria. |
Is Greater Than or Equal To |
Returns attribute values that are greater than or equal to the specified criteria. |
Is Less Than or Equal To |
Returns attribute values that are less than or equal to the specified criteria. |
Matches |
Uses a fuzzy lookup index to filter results. |
Does Not Match |
Uses a fuzzy lookup index to filter results. |
Contains Pattern |
Uses .NET Framework regular expressions to filter results on a specified pattern. |
Does Not Contain Pattern |
Uses the .NET Framework regular expressions to filter results that do not match a specified pattern. |
Is NULL |
Returns attribute values that are null. The Criteria field is disabled when you select the Is NULL operator. |
Is Not NULL |
Returns attribute values that are not null. The Criteria field is disabled when you select the Is not NULL operator. |
Now let us explore the above details with practical examples. For the examples, we will be using the good old AdventureWorks database. This will give you an idea of how to use this filter in SQL. Filters are an integral part of businesses, and there’s great demand for administrators. Therefore, you go through the SQL DBA career path if you want to set yourself up for this role.
This filter returns any set of data equal to a particular value. The sample query is as follows.
select * from [dbo].[BookInventory] where TitleID=2
The above query returns values where TitleID is equal to 2.
This filter returns any set of data that is less than or equal to a particular value. The sample query is as follows
select * from [dbo].[BookInventory] where TitleID <=12
This returns all the values from the table BookInventory which are less than equal to 12.
This filter is used with a wild card character and returns all the value which satisfies a particular condition. Following is a sample query.
select * from [dbo].[BookInventory] where Title like 'P%'
This returns all the values where Titleid starts with P, and then there can be any number of characters after it.
This filter is used with a wild card character and returns all the value which does not satisfy a particular condition. Following is a sample query.
select * from [dbo]. [Bookinventory] where Title not like P%'
This returns all the values where P is not the first character in the Title column. Following is the output.
This filter returns any data set greater than or equal to a particular value. The sample query is as follows
select * from [dbo].[BookInventory] where Quantity >= 3
This returns all the rows where quantity is greater than or equal to a particular value.
This filter returns any set of data that is less than or equal to a particular value. The sample query is as follows
select * from [dbo].[BookInventory] where Quantity<=3
This returns all the rows where quantity is less than or equal to a particular value.
This filter returns any data set that does not match a particular value. The sample query is as follows
select * from [dbo].[BookInventory] where Quantity<>3
This returns all the rows where the quantity does not match the particular value 3.
This filter returns any set of data that matches the null value in a particular column. The sample query is as follows
select * from [HumanResources]. [Employee] where OrganizationLevel is null
This returns all the rows where the OrganizationLevel column value is null.
This filter returns any data set that does not match the null value in a particular column. The sample query is as follows
select * from [Human Resources].[Employee] where Organization Level is not null
This returns all the rows where the OrganizationLevel column value is not null.
Please note, although we have used * wildcard characters in most of the query examples, the filters will have equal effect if used with individual table columns as well.
SQL Server Filters can be used for various purposes, here are the few advantages on using SQL Server Filters:
SQL Training For Administrators & Developers
Over the last few paragraphs, we have discussed the different aspects of SQL Server database filters. This write-up gives you a glimpse of what we can achieve if we use filters in a SQL query. This also gives a brief idea of when to use a filter and when not to. This write-up can begin a more detailed study of filters for any users in the future. Do you love working with data? Or want to pursue a career in the Microsoft SQL Server Database domain? But feel stuck with doubts? A comprehensive SQL career path will help you explore all the career options.
What is Schema in SQL With Example: All You Need to Know
Data Definition Language (DDL) Commands in SQL
How To Create Database Table-All You Need To know
What does a Database Administrator do? A Detailed Study
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Download Syllabus
Get Complete Course Syllabus
Enroll For Demo Class
It will take less than a minute
Tutorials
Interviews
You must be logged in to post a comment