Worldscope

BETWEEN

Palavras-chave:

Publicado em: 05/08/2025

Understanding and Using the BETWEEN Operator in Oracle

The BETWEEN operator in Oracle SQL is a powerful tool for filtering data based on a range of values. This article aims to provide a comprehensive understanding of the BETWEEN operator, its syntax, usage, and alternatives, along with an analysis of its performance characteristics.

Fundamental Concepts / Prerequisites

Before diving into the BETWEEN operator, a basic understanding of SQL queries and comparison operators is essential. Familiarity with Oracle's syntax, including SELECT, WHERE clauses, and data types like NUMBER, DATE, and VARCHAR2, will be helpful. You should also understand the concept of relational databases and table structures.

Implementation in Oracle SQL

The BETWEEN operator is used within the WHERE clause of a SQL query to select rows where a value falls within a specified range. Both the start and end values are inclusive.


-- Example: Selecting employees with salaries between 50000 and 70000

SELECT employee_id, employee_name, salary
FROM employees
WHERE salary BETWEEN 50000 AND 70000;

-- Example: Selecting orders placed between two specific dates

SELECT order_id, order_date, customer_id
FROM orders
WHERE order_date BETWEEN '2023-01-01' AND '2023-01-31';

-- Example: Using NOT BETWEEN to select employees with salaries outside the range

SELECT employee_id, employee_name, salary
FROM employees
WHERE salary NOT BETWEEN 50000 AND 70000;

--Important date time handling:
--BETWEEN implicitly includes the end values. When working with datetimes you often want to avoid including the last day in a range.

--If you want to include all events until the start of today you should use this:
SELECT *
FROM table_name
WHERE event_date >= date '2024-01-01' AND event_date < trunc(SYSDATE);

--In this case the `trunc` function removes the time component from SYSDATE, only leaving the date. The < operator ensures the current date is exclusive.

Code Explanation

The first example demonstrates selecting employees whose salary falls between 50000 and 70000 (inclusive). The WHERE clause filters the employees table based on the salary column.

The second example showcases using BETWEEN with dates. It retrieves orders placed between January 1st, 2023, and January 31st, 2023, inclusively. Note the date format used, which is YYYY-MM-DD.

The third example uses NOT BETWEEN to select employees whose salaries are *not* within the specified range. This effectively selects all employees earning less than 50000 or more than 70000.

The last example shows proper date time handling. `BETWEEN` implicitly includes the end values. When working with datetimes you often want to avoid including the last day in a range. The `trunc` function removes the time component from `SYSDATE`, only leaving the date.

Complexity Analysis

The complexity of the BETWEEN operator largely depends on whether an index is present on the column being filtered.

Time Complexity:

  • With Index: If an index exists on the column used with BETWEEN, Oracle can efficiently use the index to locate the relevant rows. The time complexity in this case is typically O(log n), where n is the number of rows in the table.
  • Without Index: If no index exists, Oracle will perform a full table scan, examining each row to check if the value falls within the specified range. This results in a time complexity of O(n).

Space Complexity: The space complexity is generally O(1), as the BETWEEN operator itself doesn't require significant additional memory. The result set's size will influence space usage, but that's independent of the operator itself.

Alternative Approaches

The BETWEEN operator can be replaced with a combination of greater than or equal to (>=) and less than or equal to (<=) operators. For example:


-- Equivalent query without BETWEEN

SELECT employee_id, employee_name, salary
FROM employees
WHERE salary >= 50000 AND salary <= 70000;

While functionally equivalent, the BETWEEN operator is generally considered more readable and concise. Performance is typically identical, as the query optimizer often translates the BETWEEN operator into the equivalent range comparison internally.

Conclusion

The BETWEEN operator is a convenient and efficient tool for filtering data within a specified range in Oracle SQL. Understanding its syntax, usage with different data types (including dates), and performance implications, especially the importance of indexing, is crucial for writing optimized and readable queries. While alternative approaches exist using comparison operators, BETWEEN is generally preferred for its clarity and conciseness.