EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial BETWEEN in Oracle
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

BETWEEN in Oracle

By Priya PedamkarPriya Pedamkar

BETWEEN in Oracle

What is BETWEEN Operator in Oracle?

The Oracle BETWEEN operator is nothing but a range based operator that works on range based condition and allows us to provide a range based condition to fetch the data which falls under the given range. In this topic, we are going to learn about BETWEEN in Oracle.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

Points of Concentration:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • This operator is used to display rows based on a range of values.
  • The declared range is inclusive.
  • The lower limit should be declared first.
  • The negation of this operator is NOT BETWEEN…AND….
  • This operator can be applied in a SELECT, INSERT, UPDATE or DELETE statement.
  • This operator can be applied for NUMBER, CHARCTER or DATE data types.

Syntax for BETWEEN Operator in Oracle

The syntax is mentioned below:

Syntax:

SELECT Column_1, Column_2,..., Column_N FROM Table_Name WHERE Column_Name [NOT] BETWEEN Value1 AND Value2;

Description:

  • Col_1/2/n: The column(s) or calculation as per your requirement.
  • Table_Name: As per your requirement.
  • WHERE: It’s mandatory to use this operator.
  • Column_Name: The condition will be applied to the column to filter the data.
  • Value1/2: These two values will create an inclusive range to compare the data in the Column_Name column.
  • NOT: This is a negation of the BETWEEN operator. It excludes the records which fall under the range.

Example(with/without Negation)

Let us see with the help of examples:

Without Negation

SQL> SELECT Ename, Deptno, Sal FROM Emp WHERE Sal BETWEEN 1250 AND 2975;

Output:

BETWEEN in oracle output 1

With Negation

SQL> SELECT Ename, Deptno, Sal FROM Emp WHERE Sal NOT BETWEEN 1250 AND 2975;

Output:

BETWEEN in oracle output 2

Explanation: The above two examples (without and with negation) clearly shows how Oracle BETWEEN operator filters the data based on range condition.

Examples of BETWEEN Operator in Oracle

In this section, we’ll see the implementation of Oracle BETWEEN Operator and its behavior. For that, we will use the below sample table (Emp) with 14 records to understand the Oracle BETWEEN Operator behavior.

Query:

SQL> SELECT * from Emp;

Output:

BETWEEN in oracle output 3

Example #1 – with NUMBER data type

Code:

SQL > SELECT Ename, Deptno, Sal FROM Emp WHERE Sal BETWEEN 1250 AND 2975;

Output:

BETWEEN in oracle output 4

In the above SELECT statement, the BETWEEN operator creates an inclusive range of the given values (1250, 2975) and searches for the values in the Sal column which falls in the range and fetches the records.

Code:

SQL > SELECT Ename, Deptno, Sal FROM Emp WHERE Sal NOT BETWEEN 1250 AND 2975;

Output:

BETWEEN in oracle output 5

The above SELECT statement is just an opposite case of the previous example, as the BETWEEN operator creates an inclusive range of the given values (1250, 2975) and search for the values in the Sal column and fetch the records which fall in the range. But because of NOT keyword which is used with the BETWEEN operator, the records which fall in the range get excluded from the result and returns only records which are not in the range or outside of the range.

Example #2 – with DATE data type

Code:

SQL> SELECT Ename, Deptno, Hiredate FROM Emp WHERE Hiredate
BETWEEN '17-FEB-81' AND '23-JAN-82';

Output:

BETWEEN in oracle output 6

In the above SELECT statement, the BETWEEN operator works on the DATE data type which searches for the records (based on the date range) in the Hiredate column which is the date data type and returns the records which fall under the given date range (inclusive).

Code:

SQL> SELECT Ename, Deptno, Hiredate FROM Emp WHERE Hiredate
NOT BETWEEN '17-FEB-81' AND '23-JAN-82';

Output:

output 7

The above SELECT statement is the negation of the previous SELECT statement because NOT keyword that allows to fetch the records outside of the given date range and returns the records.

Tips:

  • Values should be in the same format that the date data type column contains
  • In case value(s) is/are in a different format then the format of the column data should be matched. For that To_date or To_char can be applied on the date data type column to match the format with the format of the value.

Example #3 – with VARCHAR data type

Code:

SQL> SELECT Ename, Deptno, Sal, Job FROM Emp WHERE Job
BETWEEN 'MANAGER' AND 'SALESMAN';

Output:

output 8

The BETWEEN operator creates an inclusive range of the given string values (‘MANAGER’ AND ‘SALESMAN’). But when it starts range based search for the records then it compares character-wise (like Dictionary words arrangement) and fetches the record(s). And the below SELECT statement is a negation of the above SELECT statement.

Code:

SQL> SELECT Ename, Deptno, Sal, Job FROM Emp WHERE Job
NOT BETWEEN 'MANAGER' AND 'SALESMAN';

Output:

output 9

Tips:

  • The character functions (UPPER, LOWER, INITCAP, etc.) can be used to match the format of values with the column data format. See the example below.

Code:

SQL> SELECT Ename, Deptno, Sal, Job FROM Emp WHERE
'manager' AND LOWER ('PRESIDENT');

Output:

output 10

  • If the format of values and column data in the BETWEEN operator doesn’t match, it will return a no row selected output. See the example below.

Code:

SQL> SELECT Ename, Deptno, Sal, Job FROM Emp WHERE
LOWER (Job) BETWEEN 'manager' AND 'PRESIDENT';

Output:

 output 11

In this SELECT statement, the LOWER function converts job column data into the lower case but the BETWEEN operator contains one value (PRESIDENT) in the upper case which is not matching with the column data format.

Example #4 – DML operation (INSERT, UPDATE, DELETE)

The BETWEEN operator can be applied for the data manipulation operation in the example mention below.

Before UPDATE

SQL> SELECT Ename, Deptno, Sal, Job FROM Emp WHERE
Job BETWEEN 'MANAGER' AND 'PRESIDENT';

Output:

output 12

UPDATE Query:

SQL> UPDATE Emp SET Sal= Sal*2 WHERE Job BETWEEN 'MANAGER' AND 'PRESIDENT';

After UPDATE

SQL> SELECT Ename, Deptno, Sal, Job FROM Emp WHERE
Job BETWEEN 'MANAGER' AND 'PRESIDENT';

Output:

output 13

Tips:

  • The BETWEEN operator is very useful while performing NON-EQUI Join on range based condition.
  • Take extra care about column data format and values format in the BETWEEN operator, both should be matched.
  • The function can be used to match the column data format and values format with each other.

Conclusion

Oracle BETWEEN … AND … is an operator that allows us to make a range based search and fetch the record(s) which falls in the range. By using NOT keyword with this operator, records can be fetched which doesn’t fall in the range. In other words, by using NOT keyword with this operator, record(s) can be excluded from the result set which falls in the given range.

Recommended Articles

This is a guide to BETWEEN in Oracle. Here we discuss the Implementation of Oracle BETWEEN operator with appropriate syntax and respective examples. You may also have a look at the following articles to learn more –

  1. Oracle Operators
  2. Oracle Aliases
  3. Oracle Procedures
  4. Oracle Cross Join
  5. Guide to BETWEEN in SQL
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more