Link Search Menu Expand Document

Module 2: Filtering Data with WHERE Clause Overview

Previous Tutorial - Next Tutorial

We are somewhat familiar with the basic functionality of the SELECT statement, it will return the required result from the query table. But sometimes there is a situation when we want to restrict the query results to a specified condition. Filtering data is one of the most fundamental aspects of T-SQL querying.

Almost every query that you write involves some form of filtering. We will be going to learn about various condition operators used with the WHERE clause, such as comparison operators, range operators, etc. So this is the general syntax of the WHERE clause and it is followed by some search criteria or a predicate. It returns only those rows for which the predicate evaluates to true. So, for example, in this person's table, I want to find the person whose last name is Galvin.

How to Filter Data

So if I write this query, then it will give me the result where I have highlighted some records which I want to filter out. So when I apply the WHERE predicted something like this, then it will give me this result. So here I have used the equal to the operator in the WHERE clause I can use the different operators to filter out the result.

Here are the different operators that I can use with the WHERE clause. They are used to specify conditions in an inner SQL statement. For example, this is the comparison operator in the previous query. In the previous slide, we have used this equal to operator, which checks if the value of the next left expression and a right expression are equal or not. If they are equal, then the condition becomes true and it will return the result.

In the same way, we have a range operator. It will check if the column value falls within a certain range or not using the between operator here, using the (<)Less-than operator and (>)greater-than operator. So, if the specified value falls within the range, then it will return the result.

In the same way, we can use the IS, NULL operator, to compare with the null value. So null has some special meaning in SQL Server. Null means unknown or missing information. NULL is a result keyword in the SQL server.

Filtering Data

So comparing any value with null will result in an unknown and then we have a like operator, so using like you can search for values that start or end with specified characters, you can also search for a specific character contain within the specified string with the help of wild card such as percentage sign and underscore.

So rather than reading this slide, let's jump into the code and perform each and every operator practically. We will start by looking at how to use the WHERE clause with a single condition. So first, let's take the equal to operator.

Web API for developers Free Trial Offline SDK

Here's SQL video tutorial:

Other useful tutorials:


Back to top

© , Learn SQL 24 / 7 — All Rights Reserved - Terms of Use - Privacy Policy