In SQL, the BETWEEN operator is used to filter records based on a range of values. It allows you to select rows where a particular column value falls within a specified range, inclusive of the endpoints. The basic syntax of the BETWEEN operator is as follows:
sql
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Here's an example that demonstrates the usage of the BETWEEN operator:
sql
SELECT *
FROM products
WHERE price BETWEEN 10 AND 50;
In the example above, the query retrieves all rows from the "products" table where the "price" column value falls between 10 and 50 (including 10 and 50).
The BETWEEN operator can be used with various data types, such as numeric values, dates, or timestamps, as long as the values can be compared using the range.
Here are a few more examples to illustrate the usage of the BETWEEN operator:
1. Retrieve rows where the "age" column value is between 18 and 30:
sql
SELECT *
FROM users
WHERE age BETWEEN 18 AND 30;
2. Retrieve rows where the "created_at" column value is between two specific dates:
sql
SELECT *
FROM orders
WHERE created_at BETWEEN '2022-01-01' AND '2022-12-31';
3. Retrieve rows where the "order_date" column value is between two specific timestamps:
sql
SELECT *
FROM orders
WHERE order_date BETWEEN '2022-01-01 00:00:00' AND '2022-01-01 23:59:59';
Remember to replace "table_name" with the actual name of your table, "column_name" with the specific column you want to filter on, and adjust the values and data types according to your database structure and requirements.
The BETWEEN operator simplifies the process of selecting records within a range and can be combined with other SQL operators and clauses to create more complex conditions for filtering data.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc