SQL BETWEEN syntax and BETWEEN instance tutorial

Source: Internet
Author: User

The BETWEEN operator is used in the WHERE clause to select a data range BETWEEN two values. The BETWEEN operator BETWEEN... AND selects a data range BETWEEN two values. These values can be numerical values, text values, or dates.

SQLBETWEENSyntax and BETWEEN instance tutorial
The BETWEEN operator is used in the WHERE clause to select a data range BETWEEN two values.

BETWEEN Operator
The BETWEEN operator... AND selects a data range BETWEEN two values. These values can be numerical values, text values, or dates.

SQL BETWEEN Syntax
SELECT column_name (s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2

Example 1: select * from test where ID between 2 and 5 Records with IDs of 2-5 are displayed. The results show three records with IDs of 234. Different database tutorials have different scopes, some databases will display four records containing ID 5. Example 2: select * from test where ID not between 2 and 5 results for records with id not 2-5: five records with the id of 16789 can also be displayed in alphabetical order. For example, select * from table_name where title between 'A' and 'C' is displayed for records with the title from a to c'


The BETWEEN operator is used in the WHERE clause to select a data range BETWEEN two values.
BETWEEN Operator
OperatorBETWEEN... AND selects a data range between two values. These values can be numerical values, text values, or dates.

SQL BETWEEN Syntax
SELECT column_name (s)
FROM table_name
WHERE column_name
Original BETWEEN value1 AND value2 table (used in the instance :)
Persons table:

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth th Avenue www. bKjia. c0mNew York
3 Carter Thomas Changan Street Beijing
4 Gates Bill Xuanwumen 10 Beijing
BETWEEN operator instance
To display people between "Adams" (included) and "Carter" (excluded) alphabetically, use the following SQL:

SELECT * FROM Persons
WHERE LastName
BETWEEN 'adams' AND 'cart'
Result set:
Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York

Important: different databases have different processing methods for the BETWEEN... AND operator. Some databases list people between "Adams" and "Carter", but do not include "Adams" and "Carter "; some databases list people between "Adams" and "Carter" and include "Adams" and "Carter; other databases list people between "Adams" and "Carter", including "Adams", but not "Carter ".

Therefore, check how your database handles the BETWEEN... AND operator!
Instance 2
To use a person out of the range shown in the preceding example, use the not operator:

SELECT * FROM Persons
WHERE LastName
Not between 'adams' AND 'cart'
Result set:
Id LastName FirstName Address City
3 Carter Thomas Changan Street Beijing
4 Gates Bill Xuanwumen 10 Beijing

BETWEEN... AND operator
Determines whether a numeric value is within a specific range. The www. bKjia. c0m operator can only be used in SQL statements.
Expr [Not]BETWEENValue1 AND value2
Expr
Specifies the combination of fields and expressions to be calculated.
Value1, value2
The specified value range.
For example:
If you want to query all employees aged between 25-30, you can use the following procedure.
SELECT name, age BETWEEN 25 AND 30
FROM employee form;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.