Wednesday, December 18, 2024

SQL BETWEEN Keyword

 The SQL BETWEEN keyword is used in a WHERE clause to filter the results within a certain range. It can be used with numeric, date, and text values. The BETWEEN keyword is inclusive, meaning it includes the start and end values of the range.

Syntax:

SELECT column_name
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example 1: Using BETWEEN with Numeric Values

SELECT * 
FROM employees
WHERE salary BETWEEN 50000 AND 70000;

This will return all rows from the employees table where the salary is between 50,000 and 70,000.

Example 2: Using BETWEEN with Date Values

SELECT * 
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';

This query will return all rows from the orders table where the order_date is between January 1, 2024, and December 31, 2024.

Example 3: Using BETWEEN with Text Values

SELECT * 
FROM products
WHERE product_name BETWEEN 'A' AND 'M';

This will return all products where the product_name starts with any letter from 'A' to 'M'.

Notes:

  • The BETWEEN operator is inclusive, so both value1 and value2 are included in the results.
  • The order of value1 and value2 matters. BETWEEN assumes value1 is less than value2.

No comments:

Post a Comment