Skip to content

Commit b9e7400

Browse files
[Edit] SQL: BETWEEN (#7440)
* [Edit] SQL: BETWEEN * Update content/sql/concepts/operators/terms/between/between.md * Update content/sql/concepts/operators/terms/between/between.md ---------
1 parent 4e3bf97 commit b9e7400

File tree

1 file changed

+133
-18
lines changed
  • content/sql/concepts/operators/terms/between

1 file changed

+133
-18
lines changed
Lines changed: 133 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,154 @@
11
---
22
Title: 'BETWEEN'
3-
Description: 'Selects values, inclusively of beginning and end values, within a given range. BETWEEN works with numbers, text, or date data types.'
3+
Description: 'Filters records within a specified range of values.'
44
Subjects:
5+
- 'Computer Science'
56
- 'Data Science'
67
Tags:
7-
- 'Database'
8+
- 'Filter'
9+
- 'Operators'
810
- 'Queries'
9-
- 'PostgreSQL'
10-
- 'MySQL'
11-
- 'SQLite'
12-
- 'Tables'
11+
- 'Range'
1312
CatalogContent:
1413
- 'learn-sql'
1514
- 'paths/analyze-data-with-sql'
1615
---
1716

18-
The **`BETWEEN`** operator selects values, inclusive of beginning and end values, within a given range.
17+
The **`BETWEEN`** operator in SQL filters records within a specified range of values. It simplifies the process of retrieving data that falls between two boundary values, working with numeric, text, and date columns to create inclusive range queries.
1918

20-
## Syntax
19+
## Syntax of `BETWEEN` operator
2120

2221
```pseudo
23-
SELECT *
24-
FROM table
25-
WHERE column BETWEEN value_A AND value_B;
22+
SELECT column_name(s)
23+
FROM table_name
24+
WHERE column_name BETWEEN value1 AND value2;
2625
```
2726

28-
The `column` must exist and `value_A` and `value_B` must define a valid range. `BETWEEN` works with numbers, text, or date data types.
27+
**Parameters:**
2928

30-
## Example
29+
- `column_name(s)`: The name of the column(s) to retrieve data from
30+
- `table_name`: The name of the table containing the data
31+
- `value1`: The starting value of the range (inclusive)
32+
- `value2`: The ending value of the range (inclusive)
3133

32-
The following query returns all rows from the `students` table with a `gpa` between 0 and 2 (exclusive) in ascending order:
34+
**Return value:**
35+
36+
The `BETWEEN` operator returns all records where the specified column value falls within the inclusive range from `value1` to `value2`.
37+
38+
## Sample Database
39+
40+
The following examples use this sample `Employees` table:
41+
42+
| EmployeeID | FirstName | LastName | Age | Salary | Department | HireDate |
43+
| ---------- | --------- | -------- | --- | ------ | ---------- | ---------- |
44+
| 1 | John | Adams | 28 | 45000 | IT | 2023-03-15 |
45+
| 2 | Sarah | Brown | 34 | 65000 | Sales | 2022-07-10 |
46+
| 3 | Mike | Davis | 42 | 75000 | IT | 2021-01-20 |
47+
| 4 | Emma | Johnson | 29 | 55000 | Marketing | 2023-08-05 |
48+
| 5 | Robert | Miller | 38 | 68000 | Sales | 2022-11-12 |
49+
| 6 | Lisa | Wilson | 25 | 48000 | IT | 2023-05-18 |
50+
51+
## NOT BETWEEN Example
52+
53+
This example demonstrates how to exclude records within a specific range using the `NOT BETWEEN` operator:
54+
55+
```sql
56+
-- Find employees whose ages are NOT between 30 and 40
57+
SELECT EmployeeID, FirstName, LastName, Age
58+
FROM Employees
59+
WHERE Age NOT BETWEEN 30 AND 40;
60+
```
61+
62+
The output of this code is:
63+
64+
```shell
65+
EmployeeID | FirstName | LastName | Age
66+
1 | John | Adams | 28
67+
3 | Mike | Davis | 42
68+
4 | Emma | Johnson | 29
69+
6 | Lisa | Wilson | 25
70+
```
71+
72+
The query returns employees whose ages fall outside the 30-40 range, effectively filtering out employees within that age bracket.
73+
74+
## BETWEEN with IN Example
75+
76+
This example combines the `BETWEEN` operator with the `IN` operator to create more complex filtering conditions for real-world scenarios:
77+
78+
```sql
79+
-- Find employees with salaries between 50000 and 70000 from specific departments
80+
SELECT EmployeeID, FirstName, LastName, Salary, Department
81+
FROM Employees
82+
WHERE Salary BETWEEN 50000 AND 70000
83+
AND Department IN ('IT', 'Sales', 'Marketing');
84+
```
85+
86+
The output of this code is:
87+
88+
```shell
89+
EmployeeID | FirstName | LastName | Salary | Department
90+
2 | Sarah | Brown | 65000 | Sales
91+
4 | Emma | Johnson | 55000 | Marketing
92+
5 | Robert | Miller | 68000 | Sales
93+
```
94+
95+
This query filters employees that meet both conditions: salaries within the $50,000-$70,000 range and belonging to specific departments, useful for HR analysis and budget planning.
96+
97+
## BETWEEN Text Values Example
98+
99+
This example shows how to filter text data alphabetically using the `BETWEEN` operator, useful for name-based searches and alphabetical sorting:
100+
101+
```sql
102+
-- Find employees whose last names fall alphabetically between 'Brown' and 'Miller'
103+
SELECT EmployeeID, FirstName, LastName, Department
104+
FROM Employees
105+
WHERE LastName BETWEEN 'Brown' AND 'Miller';
106+
```
107+
108+
The output of this code is:
109+
110+
```shell
111+
EmployeeID | FirstName | LastName | Department
112+
2 | Sarah | Brown | Sales
113+
3 | Mike | Davis | IT
114+
4 | Emma | Johnson | Marketing
115+
5 | Robert | Miller | Sales
116+
```
117+
118+
The query returns employees whose surnames fall within the alphabetical range, including both boundary values.
119+
120+
## BETWEEN Dates Example
121+
122+
This example demonstrates date range filtering, commonly used for reporting and time-based analysis in business applications:
33123

34124
```sql
35-
SELECT *
36-
FROM students
37-
WHERE gpa BETWEEN 0.0 AND 1.99
38-
ORDER BY gpa;
125+
-- Find employees hired between January 1, 2023 and December 31, 2023
126+
SELECT EmployeeID, FirstName, LastName, HireDate, Department
127+
FROM Employees
128+
WHERE HireDate BETWEEN '2023-01-01' AND '2023-12-31';
39129
```
130+
131+
The output of this code is:
132+
133+
```shell
134+
EmployeeID | FirstName | LastName | HireDate | Department
135+
1 | John | Adams | 2023-03-15 | IT
136+
4 | Emma | Johnson | 2023-08-05 | Marketing
137+
6 | Lisa | Wilson | 2023-05-18 | IT
138+
```
139+
140+
This query retrieves all employees hired during the year 2023, useful for analyzing hiring patterns and new employee onboarding.
141+
142+
## Frequently Asked Questions
143+
144+
### 1. Is SQL `BETWEEN` inclusive or exclusive?
145+
146+
The SQL `BETWEEN` operator is inclusive, meaning it includes both the starting and ending values in the result set. For example, `WHERE Age BETWEEN 25 AND 35` will return records where Age equals 25, 35, and any value in between.
147+
148+
### 2. What is the function of `BETWEEN`?
149+
150+
The `BETWEEN` operator functions as a shorthand for range comparisons, replacing the need to write `WHERE column >= value1 AND column <= value2`. It simplifies queries by providing a more readable way to filter data within specific ranges for numeric, text, and date values.
151+
152+
### Why is SQL `BETWEEN` operator used?
153+
154+
The `BETWEEN` operator is used because it improves query readability and reduces syntax complexity when filtering data within ranges. It's particularly valuable for date ranges, price filtering, age groups, and alphabetical sorting, making SQL queries more intuitive and maintainable for developers and analysts.

0 commit comments

Comments
 (0)