Master SQL BETWEEN operator: filter data within a certain range
SQL BETWEEN operator: efficiently filter data
SQL's BETWEEN
operator is a powerful tool for filtering specific data ranges, and can quickly locate records between two values, which can be numbers, dates, or text (depending on the database's sorting rules).
grammar
<code class="sql">SELECT column1, column2, ... FROM table_name WHERE column_name BETWEEN value1 AND value2;</code>
- The
BETWEEN
clause contains upper and lower limit values (value1
andvalue2
) and contains boundary values.
How it works
BETWEEN
operator works as follows:
- Numerical range filter : Used to extract rows whose column values are within the specified range of values. For example, look for products that cost between 10 and 50.
- Date range filtering : Easy to obtain records for specific date intervals. For example, search for orders from January 1, 2024 to January 31, 2024.
- Text range filtering : Select lines whose text values are in a specific range alphabetical order (depending on the database's sorting rules).
Example
Numerical range filtering
Suppose there is a product list called products
:
productid | productname | price |
---|---|---|
1 | laptop | 1200 |
2 | mouse | 25 |
3 | keyboard | 45 |
4 | Monitor | 200 |
Query :
<code class="sql">SELECT productname, price FROM products WHERE price BETWEEN 30 AND 300;</code>
result :
productname | price |
---|---|
keyboard | 45 |
Monitor | 200 |
Date range filtering
Suppose there is an order form called orders
:
Orderid | Orderdate | customerid |
---|---|---|
101 | 2023-01-10 | 1 |
102 | 2023-01-15 | 2 |
103 | 2024-02-05 | 3 |
Query :
<code class="sql">SELECT orderid, orderdate FROM orders WHERE orderdate BETWEEN '2023-01-01' AND '2023-01-31';</code>
result :
Orderid | Orderdate |
---|---|
101 | 2023-01-10 |
102 | 2023-01-15 |
Text range filtering
Suppose there is a student table called students
:
studentid | name |
---|---|
1 | Alice |
2 | bob |
3 | charlie |
Query :
<code class="sql">SELECT name FROM students WHERE name BETWEEN 'a' AND 'c';</code>
Results : (The results depend on the database's sorting rules, assuming it is sorted alphabetically here)
name |
---|
Alice |
bob |
charlie |
Key points
- Include boundary values :
value1
andvalue2
are both included in the filter range. - Value order : Make sure that smaller values are in front and larger values are in back.
- Exclude ranges : Use
NOT BETWEEN
to exclude specified ranges. - Performance optimization : For large datasets, create indexes for query columns to improve performance.
Summarize
The BETWEEN
operator is a concise method for efficiently filtering data ranges in SQL, and is widely used in various database operations. Its ease of use and efficiency make it a common tool in database management.
Author: Abhay Singh Kathayat (full stack development engineer, contact email: kaashshorts28@gmail.com)
The above is the detailed content of Master SQL BETWEEN operator: filter data within a certain range. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The CentOS shutdown command is shutdown, and the syntax is shutdown [Options] Time [Information]. Options include: -h Stop the system immediately; -P Turn off the power after shutdown; -r restart; -t Waiting time. Times can be specified as immediate (now), minutes ( minutes), or a specific time (hh:mm). Added information can be displayed in system messages.

Backup and Recovery Policy of GitLab under CentOS System In order to ensure data security and recoverability, GitLab on CentOS provides a variety of backup methods. This article will introduce several common backup methods, configuration parameters and recovery processes in detail to help you establish a complete GitLab backup and recovery strategy. 1. Manual backup Use the gitlab-rakegitlab:backup:create command to execute manual backup. This command backs up key information such as GitLab repository, database, users, user groups, keys, and permissions. The default backup file is stored in the /var/opt/gitlab/backups directory. You can modify /etc/gitlab

Complete Guide to Checking HDFS Configuration in CentOS Systems This article will guide you how to effectively check the configuration and running status of HDFS on CentOS systems. The following steps will help you fully understand the setup and operation of HDFS. Verify Hadoop environment variable: First, make sure the Hadoop environment variable is set correctly. In the terminal, execute the following command to verify that Hadoop is installed and configured correctly: hadoopversion Check HDFS configuration file: The core configuration file of HDFS is located in the /etc/hadoop/conf/ directory, where core-site.xml and hdfs-site.xml are crucial. use

Enable PyTorch GPU acceleration on CentOS system requires the installation of CUDA, cuDNN and GPU versions of PyTorch. The following steps will guide you through the process: CUDA and cuDNN installation determine CUDA version compatibility: Use the nvidia-smi command to view the CUDA version supported by your NVIDIA graphics card. For example, your MX450 graphics card may support CUDA11.1 or higher. Download and install CUDAToolkit: Visit the official website of NVIDIACUDAToolkit and download and install the corresponding version according to the highest CUDA version supported by your graphics card. Install cuDNN library:

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

A complete guide to viewing GitLab logs under CentOS system This article will guide you how to view various GitLab logs in CentOS system, including main logs, exception logs, and other related logs. Please note that the log file path may vary depending on the GitLab version and installation method. If the following path does not exist, please check the GitLab installation directory and configuration files. 1. View the main GitLab log Use the following command to view the main log file of the GitLabRails application: Command: sudocat/var/log/gitlab/gitlab-rails/production.log This command will display product

PyTorch distributed training on CentOS system requires the following steps: PyTorch installation: The premise is that Python and pip are installed in CentOS system. Depending on your CUDA version, get the appropriate installation command from the PyTorch official website. For CPU-only training, you can use the following command: pipinstalltorchtorchvisiontorchaudio If you need GPU support, make sure that the corresponding version of CUDA and cuDNN are installed and use the corresponding PyTorch version for installation. Distributed environment configuration: Distributed training usually requires multiple machines or single-machine multiple GPUs. Place
