SQL between
Created By: chatGPT
The BETWEEN operator in SQL is used to filter the result set within a specified range. It can be applied to numeric, date, or text values, and it is inclusive, meaning it includes the start and end values. Here's how you can use the BETWEEN operator in a SQL query.
SELECT *
FROM employees
WHERE hire_date BETWEEN '2020-01-01' AND '2021-12-31';
In the example above, the query retrieves all records from the employees table where the hire_date falls between January 1, 2020, and December 31, 2021. You can also use BETWEEN with numeric values.
SELECT *
FROM products
WHERE price BETWEEN 10 AND 50;
This query selects all products with a price between 10 and 50. The BETWEEN operator can also be used with text values, which are compared in alphabetical order.
SELECT *
FROM customers
WHERE last_name BETWEEN 'A' AND 'M';
In this case, we retrieve all customers whose last_name starts with letters from A to M. The BETWEEN operator is a convenient way to filter data without writing multiple OR conditions.
This retrieves all orders placed from the beginning of 2023 up till the current date. It's essential to ensure that the values you are comparing with BETWEEN are of the same data type to avoid unexpected results.SELECT *
FROM orders
WHERE order_date BETWEEN '2023-01-01' AND CURRENT_DATE;