SQL Tutorial

SQL Introduction SQL Aggregate Functions SQL Aliases SQL And SQL Any All SQL Avg SQL Between SQL Case SQL Comments SQL Count SQL Delete SQL Distinct SQL Exists SQL GROUP BY SQL Having SQL In SQL INSERT INTO SQL Is Not Null SQL Join SQL Full Outer Join SQL Inner Join SQL Left Join SQL Right Join SQL Self Join SQL Like SQL Min Max SQL NOT Operator SQL Null SQL Operators SQL OR operator SQL ORDER BY SQL Select SQL Select Into SQL Top Limit Fetch SQL Stored Procedures SQL Sum SQL Union SQL Update SQL Where SQL Wildcards

SQL Database

SQL Alter Table SQL Auto increment SQL Backup Database SQL Check SQL Constraints SQL Create View SQL Create Database SQL Create Table SQL Data types SQL Dates SQL Default Constraint SQL Drop Database SQL Drop Table SQL Foreign Key SQL Hosting SQL Index SQL injections SQL Not NULL SQL PrimaryKey SQL Unique SQL Views

SQL Between

Example Table

We will use the following table named orders for our examples:


        CREATE TABLE orders (
            order_id INT PRIMARY KEY,
            customer_name VARCHAR(50),
            order_date DATE,
            amount DECIMAL(10, 2)
        );

        INSERT INTO orders (order_id, customer_name, order_date, amount) VALUES
        (1, 'Alice', '2023-01-15', 250.00),
        (2, 'Bob', '2023-02-20', 150.00),
        (3, 'Charlie', '2023-03-10', 300.00),
        (4, 'David', '2023-04-05', 100.00),
        (5, 'Eve', '2023-05-25', 200.00);
        
order_id customer_name order_date amount
1 Alice 2023-01-15 250.00
2 Bob 2023-02-20 150.00
3 Charlie 2023-03-10 300.00
4 David 2023-04-05 100.00
5 Eve 2023-05-25 200.00

Using SQL BETWEEN

The SQL BETWEEN operator is used to filter the result set within a certain range. The values can be numbers, text, or dates.

Example 1: Orders Between Two Dates

SELECT * FROM orders WHERE order_date BETWEEN '2023-02-01' AND '2023-04-30';

Result:

order_id 2
customer_name Bob
order_date 2023-02-20
amount 150.00
order_id 3
customer_name Charlie
order_date 2023-03-10
amount 300.00
order_id 4
customer_name David
order_date 2023-04-05
amount 100.00

Example 2: Orders with Amount Between 150 and 300

SELECT * FROM orders WHERE amount BETWEEN 150 AND 300;

Result:

order_id 1
customer_name Alice
order_date 2023-01-15
amount 250.00
order_id 2
customer_name Bob
order_date 2023-02-20
amount 150.00
order_id 3
customer_name Charlie
order_date 2023-03-10
amount 300.00
order_id 5
customer_name Eve
order_date 2023-05-25
amount 200.00