SQL BETWEEN运算符

SQL BETWEEN运算符

BETWEEN经常用于指示时间、空间或位置的范围或间隔。换句话说,它描述了一种情况,即某物位于两者之间。

SQL中的BETWEEN运算符

BETWEEN 运算符是SQL中的逻辑运算符,用于检索指定范围或时间间隔内的数据。检索到的值可以是整数、字符或日期。

通过以下示例表格更好地理解:

SQL BETWEEN运算符

假设我们想要列出上表中年龄在20到30之间的人的名字。因此,我们将得到“Varma(21)”,“Nikhil(25)”和“Bhargav(29)”。

语法

以下是SQL中 BETWEEN 操作符的语法:

SELECT column_name1, column_name2, column_name3,……column_nameN
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

这里,

  • value1 是范围的起始值。
  • value2 是范围的结束值。

示例

首先,让我们使用以下查询语句创建一个名为“EMPLOYEE”的表。

SQL> CREATE TABLE EMPLOYEE(
   ID INT NOT NULL, 
   NAME VARCHAR(15) NOT NULL, 
   AGE INT NOT NULL, 
   ADDRESS CHAR(25), 
   SALARY DECIMAL(10, 2), 
   JOIN_DATE DATE, 
   PRIMARY KEY(ID)
);

一旦表创建完成,让我们使用以下查询向表中插入一些值-

INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(2, 'Ramesh', 21, 'Hyderabad', 25550.12, '2023/01/02');
INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(1, 'khilan', 22, 'Nijamabad', 57500.84, '2022/01/14');
INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(4, 'Kaushik', 18, 'Bangolore', 47275.43, '2023/03/15');
INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(5, 'chaitali', 23, 'Ranchi', 40700.76, '2022/04/18');
INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(6, 'Hardhik', 19, 'Noida', 44200.09, '2023/06/04');
INSERT INTO EMPLOYEE(ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES(3, 'komal', 23, 'Chennai', 44200.09, '2023/10/08');

让我们使用以下查询来验证表是否已被创建:

SQL> SELECT * FROM EMPLOYEE;

如下图所示,表已在数据库中创建。

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 1  | Khilan   | 22  |  Nizamabad  |   57500.84 |   2022-01-14   |
| 2  | Ramesh   | 21  |  Hyderabad  |   25550.12 |   2023-01-02   |
| 3  | komal    | 23  |  Chennai    |   44200.09 |   2023-10-08   |
| 4  | Kaushik  | 18  |  Bangalore  |   47275.43 |   2023-03-15   |
| 5  | chaitali | 23  |  Ranchi     |   40700.76 |   2022-04-18   |
| 6  | Hardhik  | 19  |  Noida      |   44200.09 |   2023-06-04   |
+----+----------+-----+-------------+------------+----------------+

现在,我们正在尝试使用BETWEEN运算符来检索年龄( 数字数据 )在18岁至22岁之间的员工。

SQL> SELECT * FROM EMPLOYEE 
WHERE AGE BETWEEN 18 AND 22;

输出

当我们执行上面的查询时,输出结果如下:

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 1  | Khilan   | 22  |  Nizamabad  |   57500.84 |   2022-01-14   |
| 2  | Ramesh   | 21  |  Hyderabad  |   25550.12 |   2023-01-02   |
| 4  | Kaushik  | 18  |  Bangalore  |   47275.43 |   2023-03-15   |
| 6  | Hardhik  | 19  |  Noida      |   44200.09 |   2023-06-04   |
+----+----------+-----+-------------+------------+----------------+

示例

在这里,我们尝试使用BETWEEN运算符与‘ characters ’。让我们使用以下查询检索员工姓名以字母“A”和“L”之间开头的员工-

SQL> SELECT * FROM EMPLOYEE
WHERE NAME BETWEEN 'A' AND 'L';

输出

当我们执行上述查询时,得到的输出如下所示:

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 1  | Khilan   | 22  |  Nizamabad  |   57500.84 |   2022-01-14   |
| 3  | komal    | 23  |  Chennai    |   44200.09 |   2023-10-08   |
| 4  | Kaushik  | 18  |  Bangalore  |   47275.43 |   2023-03-15   |
| 5  | chaitali | 23  |  Ranchi     |   40700.76 |   2022-04-18   |
| 6  | Hardhik  | 19  |  Noida      |   44200.09 |   2023-06-04   |
+----+----------+-----+-------------+------------+----------------+

示例

现在,我们尝试使用’日期’之间的BETWEEN运算符。让我们使用以下查询检索在’2023-01-01’和’2023-03-31’之间加入的员工:

SQL> SELECT * FROM EMPLOYEE
WHERE ORDER_DATE BETWEEN '2023-01-01' AND '2023-03-31';

输出

当我们执行上述查询时,输出结果如下所示−

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 2  | Ramesh   | 21  |  Hyderabad  |   25550.12 |   2023-01-02   |
| 4  | Kaushik  | 18  |  Bangalore  |   47275.43 |   2023-03-15   |
+----+----------+-----+-------------+------------+----------------+

在SQL中使用BETWEEN运算符和IN运算符

在SQL中,我们可以组合使用BETWEEN运算符和IN运算符来选择在某个范围内且与指定值匹配的值。

示例

在下面的查询中,我们选择了所有工资在40000到50000之间的员工。此外,我们在SQL中使用IN运算符不检索居住在Chennai、Ranchi和Hyderabad的员工。 IN 运算符。

SQL> SELECT * FROM EMPLOYEE
WHERE SALARY BETWEEN 40000 AND 45000
AND ADDRESS IN ('chennai', 'Ranchi', 'Hyderabad');

输出

执行以上查询后,输出如下所示−

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 3  | komal    | 23  |  Chennai    |   44200.09 |   2023-10-08   |
| 5  | chaitali | 23  |  Ranchi     |   40700.76 |   2022-04-18   |
+----+----------+-----+-------------+------------+----------------+

使用UPDATE语句的BETWEEN运算符

我们还可以在UPDATE语句中使用BETWEEN运算符来更新指定范围内的值。UPDATE语句用于修改数据库表中的现有数据。

示例

让我们尝试使用以下查询来更新年龄在20到25之间的员工的薪水。

SQL> UPDATE EMPLOYEE SET SALARY = 60000 
WHERE AGE BETWEEN 20 AND 25;

输出

上面查询的输出如下所示:

(4 rows affected)

验证

让我们使用以下查询来验证工资是否已经更新:

SQL> SELECT * FROM EMPLOYEE;

上述查询所生成的表如下所示:

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 1  | Khilan   | 22  |  Nizamabad  |   60000.00 |   2022-01-14   |
| 2  | Ramesh   | 21  |  Hyderabad  |   60000.00 |   2023-01-02   |
| 3  | komal    | 23  |  Chennai    |   60000.00 |   2023-10-08   |
| 4  | Kaushik  | 18  |  Bangalore  |   47275.43 |   2023-03-15   |
| 5  | chaitali | 23  |  Ranchi     |   60000.00 |   2022-04-18   |
| 6  | Hardhik  | 19  |  Noida      |   44200.09 |   2023-06-04   |
+----+----------+-----+-------------+------------+----------------+

使用DELETE语句的BETWEEN操作符

我们也可以在DELETE语句中使用BETWEEN操作符来删除指定范围内的行。

示例

现在,让我们尝试使用DELETE命令删除年龄在18到20之间的员工。

SQL> DELETE FROM EMPLOYEE WHERE AGE BETWEEN 18 AND 20;

输出

如果我们编译并运行上述查询,结果将如下生成−

(2 rows affected)

验证

让我们使用以下查询来验证是否已删除指定年龄的员工-

SQL> SELECT * FROM EMPLOYEE;

上述查询的结果如下表所示:

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 1  | Khilan   | 22  |  Nizamabad  |   60000.00 |   2022-01-14   |
| 2  | Ramesh   | 21  |  Hyderabad  |   60000.00 |   2023-01-02   |
| 3  | komal    | 23  |  Chennai    |   60000.00 |   2023-10-08   |
| 5  | chaitali | 23  |  Ranchi     |   60000.00 |   2022-04-18   |
+----+----------+-----+-------------+------------+----------------+

SQL中的NOT BETWEEN运算符

NOT BETWEEN 运算符在SQL中的工作方式与BETWEEN运算符完全相反。它用于检索不在指定范围或时间间隔内的数据。

通过以下示例表更好地理解-

SQL BETWEEN运算符

假设我们想要列出上述表中年龄不在20至30岁之间的学生。因此,我们将得到“Prudhvi(45)和Ganesh(33)”作为结果。

语法

以下是SQL中 NOT BETWEEN 操作符的语法-

SELECT column_name1, column_name2, column_name3,……column_nameN
FROM table_name
WHERE column_name NOT BETWEEN value1 AND value2;

示例

考虑先前创建的EMPLOYEE表,让我们尝试检索年龄不大于等于18且不小于等于22的员工( 数值数据 )使用以下查询语句。

SQL> SELECT * FROM EMPLOYEE 
WHERE AGE NOT BETWEEN 18 AND 22;

输出

当我们执行以上查询时,输出结果如下:

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 3  | komal    | 23  |  Chennai    |   60000.00 |   2023-10-08   |
| 5  | chaitali | 23  |  Ranchi     |   60000.00 |   2022-04-18   |
+----+----------+-----+-------------+------------+----------------+

示例

使用以下查询列出加入日期不在’2023-01-01’和’2023-03-31’之间的员工:

SQL> SELECT * FROM EMPLOYEE
WHERE JOIN_DATE NOT BETWEEN '2023-01-01' AND '2023-03-31';

输出

执行上述查询后,输出如下所示:

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 1  | Khilan   | 22  |  Nizamabad  |   60000.00 |   2022-01-14   |
| 3  | komal    | 23  |  Chennai    |   60000.00 |   2023-10-08   |
| 5  | chaitali | 23  |  Ranchi     |   60000.00 |   2022-04-18   |
| 6  | Hardhik  | 19  |  Noida      |   44200.09 |   2023-06-04   |
+----+----------+-----+-------------+------------+----------------+

NOT BETWEEN运算符和IN运算符

在SQL中,我们可以使用NOT BETWEEN运算符与IN运算符组合,选择在范围之外且与指定值不匹配的值。

示例

在下面的查询中,我们选择工资 不在 40000和50000之间的员工。此外,在SQL中使用 IN 运算符,我们不检索居住在Chennai、Ranchi和Hyderabad的员工。

SQL> SELECT * FROM EMPLOYEE
WHERE SALARY NOT BETWEEN 40000 AND 45000
AND ADDRESS IN ('chennai', 'Ranchi', 'Hyderabad');

输出

执行上述查询后,输出如下所示:

+----+----------+-----+-------------+------------+----------------+
| ID |   NAME   | AGE |   ADDRESS   |   SALARY   |   JOIN_DATE    |
+----+----------+-----+-------------+------------+----------------+
| 2  | Ramesh   | 21  |  Hyderabad  |   60000.00 |   2023-01-02   |
+----+----------+-----+-------------+------------+----------------+

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程