SQL BETWEEN Operator

The SQL Between Operator displays the records (or rows) whose values are in between the given values. The syntax of the Between operator in SQL Server is

-- SQL Server Between Operator Syntax
SELECT [Column Names]
FROM [Source]
WHERE [Column Name] BETWEEN Value1 AND Value2

--We can also write the above statement
SELECT [Column Names]
FROM [Source]
WHERE [Column Name] >= Value1 AND
      [Column Name] <= Value2

For example, If you want to find Sales between 18 May 2015 to 19 June 2015. Or If you want the Amazon website to display the products whose price range between 1000 to 2500, then internally, we have to use this SQL Server Between operator. For this Between Operator, We are going to use the below-shown data

SQL BETWEEN Operator

SQL Between Operator On Numeric Data Example

The following between operator query finds all the Customers present in the Customers table whose [Yearly Income] is between 50000 and 70000

-- SQL Server Between Operator Example
SELECT [FirstName]
      ,[LastName]
      ,[YearlyIncome]
      ,[Education]
      ,[Occupation]
FROM [Customer]
WHERE [YearlyIncome] BETWEEN 50000 AND 70000
SQL BETWEEN Operator 1

Between Operator On Text Data Example

The following Between operator query will find all the existing Customers in the Customers table whose Last Name is between Carlson and Ruiz

-- SQL Server Between Operator Example
SELECT [FirstName]
      ,[LastName]
      ,[YearlyIncome]
      ,[Education]
      ,[Occupation]
FROM [Customer]
WHERE [LastName] BETWEEN 'Carlson' AND 'Ruiz'
ORDER BY [LastName]

TIP: In SQL Server, We can also use single character instead of writing the complete name.

SQL BETWEEN Operator 2

SQL NOT BETWEEN Example

We can also use the NOT Keyword along with the Between operator. For example, the following SQL Not between operator query will find all the Customers present in a table whose [Yearly Income] is not between 50000 and 70000

-- SQL Server Not Between Operator Example
SELECT [FirstName]
      ,[LastName]
      ,[YearlyIncome]
      ,[Education]
      ,[Occupation]
FROM [Customer]
WHERE [YearlyIncome] NOT BETWEEN 50000 AND 70000
SQL BETWEEN Operator 3

Comments are closed.