SQL Between Operator - How to Use BETWEEN in SQL



Demo wikitechydatabase

  • Below is a selection from the wikitechytable table used in the examples:
sql-between-operator-1

The SQL BETWEEN Operator

  • The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
  • The BETWEEN operator is inclusive: begin and end values are included.

BETWEEN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

BETWEEN Example

  • The following SQL statement selects all wikitechytable with a fees between 20 and 40:
SELECT  * FROM wikitechytable where fees BETWEEN 20 AND 40

Output

sql-between-operator-2

NOT BETWEEN Example

  • To display the wikitechytable outside the range of the previous example, use NOT BETWEEN:
select * from wikitechytable where fees NOT BETWEEN 20 AND 40

Output

sql-between-operator-3

BETWEEN Text Values Example

  • The following SQL statement selects all products with a wikitechytable between Praveen and Venkat:
select * from wikitechytable where name BETWEEN 'Praveen' AND 'Venkat'

Output

sql-between-operator-4

NOT BETWEEN Text Values Example

  • The following SQL statement selects all products with a wikitechytable not between Praveen and Venkat:
select * from wikitechytable where name NOT BETWEEN 'Praveen' AND 'Venkat'

Output

sql-between-operator-5

BETWEEN Dates Example

  • The following SQL statement selects all orders with an OrderDate between '2002-01-01' and '2009-01-01':
select * from wikitechytable where dob  BETWEEN '2002-01-01' AND '2009-01-01'

Output

sql-between-operator-6

NOT BETWEEN Dates Example

  • The following SQL statement selects all orders with an OrderDate not between '2002-01-01' and '2009-01-01':
select * from wikitechytable where dob NOT BETWEEN '2002-01-01' AND '2009-01-01'

Output

sql-between-operator-7

Related Searches to SQL Between Operator - How to Use BETWEEN in SQL