SQL supplies us with many gear that lend a hand in retrieving helpful knowledge from a wide variety of knowledge. Every so often, we wish to retrieve a spread of values from all of the values of a desk’s columns. As an example, to retrieve the details about all staff of an organization, born in a specific decade. The SQL Between operator is helping in appearing those actions and is an integral a part of this question language.
What Is Between in SQL?
The SQL Between operator is used to check whether or not an expression is inside of a spread of values. This operator is inclusive, so it comprises the beginning and finish values of the variety. The values will also be of textual, numeric kind, or dates.
This operator can be utilized with SELECT, INSERT, UPDATE, and DELETE command.
To get a clearer image of this operator, let’s get an perception into its syntax.
The Syntax for The use of the SQL Between Operator
This operator is maximum regularly used with the SELECT command. The syntax of that is as follows:
|
SELECT column_names FROM table_name WHERE column_name BETWEEN range_start AND range_end; |
- The columns to be retrieved are specified within the SELECT remark.
- The desk the columns are being retrieved from is specified within the FROM remark.
- The BETWEEN operator is used within the WHERE clause. The column we need to observe the variety situation on is specified within the “column_name” parameter. The beginning worth of the variety of values is specified within the “range_start” parameter and the finishing worth within the “range_end” parameter.
As an example, let’s take a pattern “Employeedetails” desk:
From this desk, if we need to retrieve the ID and title of the workers with ID within the vary of four to 7, we’ll use the next question:
This will likely outcome within the following:
As we will see, the outcome comprises ID values 4 and seven as neatly, as Between is inclusive.
The use of the Now not Operator With Between
We will additionally use the NOT operator with the BETWEEN operator to make a choice the values that don’t belong to the required vary. That is accomplished by means of the use of the NOT BETWEEN key phrase as a substitute of the BETWEEN key phrase within the above syntax.
As an example, from the pattern desk, if we need to retrieve all of the staff’ ID and wage, with the wage now not within the vary 30000 to 80000, we’ll use the next question:
This will likely outcome within the following:
The use of Between With Numeric Values
We will use the ORDER BY remark to type the outcome in accordance with some columns.
As an example, from the pattern desk, to retrieve ID, title, and wage of all of the staff with wage starting from 50000 to 78000, and type this outcome in accordance with wage:
This will likely outcome within the following:
The use of Between With Date Values
When the use of Between with dates, we want to keep in mind to surround the date in unmarried inverted commas, as differently, the question returns a syntax error.
- From the “Employeedetails” desk, the purpose is to retrieve the ID, title, and date of beginning of staff born within the years 1994 to 1997:
This will likely outcome within the following:
- It’s crucial to specify the right kind values for the beginning and finishing positions of the variety.
Let’s see what occurs once we don’t do this:
As we will see, the question discussed above returns an empty set as the variety values had been specified incorrectly.
The use of Between With Textual content
The Between operator will also be used with persona information varieties. Whilst the use of this on textual content, we want to keep in mind to surround the information in unmarried inverted commas, as differently, the question returns a syntax error.
- From the pattern tables, the purpose is to retrieve all of the staff’ ID and title with names belonging to the variety ‘Arun’ to ‘Rahul’:
This will likely outcome within the following:
- Let’s see what occurs once we don’t enclose the information in inverted commas:
As we will see, the question returns a syntax error.
The use of Now not Between on Values
From the pattern desk, the purpose is to retrieve the ID, title, and DOB of all of the staff now not born within the vary 1st January 1997 to twelfth November 2000:
This will likely outcome within the following:
As we will see, all of the rows that don’t belong to the variety specified are returned.
The use of Between With the SQL In Operator
The SQL In operator is applied to specify a couple of values in a WHERE clause and is an alternate for a couple of OR prerequisites.
The syntax for the use of this operator with BETWEEN is:
|
SELECT column_names FROM table_name WHERE column_1 BETWEEN range_start AND range_end AND column_2 IN(value_1, value_2, value_3,…value_n); |
- The IN operator can be utilized to test whether or not the values decided on from the column specified within the column_2 parameter belong to the values we would like, that may be specified within the parentheses after the IN key phrase.
- We will additionally make a selection the values that don’t belong to the values specified within the parentheses by means of the use of the NOT IN key phrase as a substitute of the IN key phrase.
Let’s see some examples of this.
- From the pattern desk, the purpose is to retrieve the ID, title, and wage of all of the staff with title within the vary ‘A’ to ‘R’ and ID belonging to the values 3, 4, 5, or 6:
This will likely outcome within the following:
- Let’s see what occurs once we use the NOT IN key phrase.
To retrieve the ID, title, and DOB of staff with ID within the vary 3 to 9 and names instead of ‘Arun’, ‘Rahul’, and ‘Hannah’:
This will likely outcome within the following:
With this, we achieve the top of this text in regards to the SQL Between operator.
Acquire experience in the most recent Industry analytics gear and methods with the Submit Graduate Program in Industry Research. Join now!
Subsequent Steps
For those who have in mind all of the SQL Between operator laws and syntax, you’ll be able to customise your queries to retrieve the kind of knowledge this is wanted. This instrument, along side different SQL gear, allows us to write down other varieties of crucial queries.
Now that you already know in regards to the Between operator, it’s time so that you can get started the use of this along side different instructions, clauses, and operators to question information and transfer ahead for your adventure to turn into knowledgeable in SQL. For those who favored this text and need to get qualified, you should take a look at our SQL Certification Route because it covers the A-Z of SQL as neatly.
Do you’ve any questions for us? Point out them within the feedback phase of this “SQL Between: The Very best Technique to Retrieve Desired Vary of Values” article, and we’ll have our mavens within the box resolution them for you.
supply: www.simplilearn.com






