How to Use SQL Filter in Dataset

SQL Lesson 7 of 30 3 min read

 

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.

Sql Server Database Filter

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.

Filters in SQL

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.

Is Equal To

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.

Is Less Than or Equal To

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.

Is Like

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.

Is Not Like

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.

Is Greater Than or Equal To

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.

Is Less Than or Equal To

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.

Does Not Match

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.

Is NULL

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.

Is Not 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.

Advantages of SQL Server Filter

SQL Server Filters can be used for various purposes, here are the few advantages on using SQL Server Filters:

  • Filtering is a useful way to see only the data you want to be displayed in SQL Server databases. You can use filters to display specific records in a form, report, query, or datasheet or to print only certain records from a report, table, or query.
  • Filters can also be used to see the data you want to be displayed in Access databases.

SQL Training For Administrators & Developers

  • No cost for a Demo Class
  • Industry Expert as your Trainer
  • Available as per your schedule
  • Customer Support Available
cta13 icon

Conclusion

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.

Explore Career Programs Built for the AI Era

Choose from live, mentor-led programs designed around today’s fastest-growing technology skills and tomorrow’s AI-powered job roles.

Want a personalized learning plan?

Talk to a JanBask career expert — it's free and takes a minute to book.

Interviews

Browse Categories