SQL WHERE Clause – Adding a condition using SQL

Last modified date:

WHERE clause is used to define limit requirements for queries. We are not interested in all records in most cases of queries into tables. Only some subsets of the records based on set criteria are of our interest.

Order of WHERE Clause in SQL Script

  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. ORDER BY

Syntax

SELECT [Column 1], [Column 2]
FROM Tabule
WHERE Column <Operator>
value;

SQL Operators in WHERE clause

We will take a look at all SQL operators in a separate chapter. Below you can see the list of simple comparison operators which we work with whenever we need to filter data.

  • = Equals to
  • <> Does not equal to
  • < Is lower than
  • <= Is lower than or equal to
  • > Is higher
  • >= Is higher or equal to

Example of  WHERE Clause Usage with BETWEEN operator and >= <= operators

The table which we will use is shown below. It consists of 18484 records.

Dimcustomer-pred-WHERE-klauzuli

We use clause to filter all customers whose birthday is between 1920-01-01 a 1921-12-31 including. We have two options and the results will be the same.

(i) The first option is to use BETWEEN operator. Marginal dates are included in the requirement.

--Option 1
SELECT [CustomerKey]
,[FirstName]
,[LastName]
,[BirthDate]
FROM [AdventureWorksDW2012].[dbo].[DimCustomer]
WHERE BirthDate BETWEEN '1920-01-01' AND '1921-12-31';

(ii) second option is to use operators <= and >= operators

--Option 2
SELECT [CustomerKey]
,[FirstName]
,[LastName]
,[BirthDate]
FROM [AdventureWorksDW2012].[dbo].[DimCustomer]
WHERE BirthDate >= '1920-01-01' AND BirthDate<=
'1921-12-31';

The result is 5 records of customers born between 1.1.1920 and 31.12.1921.

Vysledek po omezeni SQL WHERE

For more information, see the official Microsoft documentation

Rate this article

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Category: SQL Basics SQL Clauses

About Ing. Jan Zedníček - BI Developer, Finance controller

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.