100 Days of SQL

sql

Day 58 – SQL BETWEEN Keyword

SQL BETWEEN keyword is used in a WHERE clause to specify a range of values that a column value must fall within.

The syntax for using the BETWEEN keyword is as follows:


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

Here, column_name is the name of the column that you want to filter on, and value1 and value2 are the minimum and maximum values of the range, respectively.

For example, the following SQL statement selects all the orders from the “Orders” table where the order date is between January 1, 2022, and March 31, 2022:


SELECT *
FROM Orders
WHERE OrderDate BETWEEN '2022-01-01' AND '2022-03-31';

In this case, the BETWEEN keyword is used to specify the range of values for the OrderDate column, and all the rows where the OrderDate falls within this range are selected.

Note that the BETWEEN keyword is inclusive, meaning that the values of value1 and value2 are included in the range. So, in the above example, orders with the dates January 1, 2022, and March 31, 2022, will be included in the result set.

Also, it is important to note that the BETWEEN keyword works with numerical and date/time values, as well as with character strings that represent numerical or date/time values in a recognized format.