SQLite

SQLite BETWEEN

SQLite is a relational-based database server implemented as a software platform. In aspects of deployment, system administration, and needed requirements, the lite of SQLite stands for “lighter weight”. Self-contained needs no server and needs no configuration are several of SQLite’s prominent features. SQLite would not allow the use of a server. The software that retrieves the SQLite database is connected with it. The applications integrate through the SQLite database by reading and writing to database folders in memory.

SQLite offers the user a variety of operators, one of which is BETWEEN. It is being used to compare the expression to the provided limit. Generally, the BETWEEN operator would be a logical operator that returns true if a limit is defined and false otherwise. We will employ the BETWEEN clause having a low range to an upper range as needed, and also with the WHERE clause and some other SQL commands like SELECT, REMOVE, UPDATE, and so on. We could utilize any clause and any other command to get rows from particular tables based on user needs. The outcome is NULL or undefined if some of the entries specified to the BETWEEN operator contain NULL. In this article, we are going to discuss the use of SQLite BETWEEN command with different illustrations.

We have downloaded the “BD Browser for SQLite” compiler to run SQLite queries. To begin, press CTRL+N on the keyboard to construct a new database. The database file is saved in any folder on the computer that has the type “SQLite databases files”. After creating a new database, the next step is to build any specified tables.

Creation of Table

We are going to create a table named “Product”. This table contains the data of different products. We specify the data type of all columns in the “CREATE” query. The column “Id” has an integer data type, “Name” has a text data type, “Quantity” has a numeric data type, “Price” also has a numeric data type, and “Rack” has a character data type.

CREATE TABLE Product (
Id INTEGER PRIMARY KEY,
Name TEXT,
Quantity NUMERIC,
Price NUMERIC,
Rack CHAR
);

The output shows that the following query for creating a table “Product” is executed without any error:

Insertion of Data

We have utilized the “INSERT” query to add the data of all the columns of the “Product” table. The data includes the columns of “Id”, “Name”, “Quantity”, “Price”, “Rack”.

INSERT INTO Product (Id,Name,Quantity,Price,Rack) VALUES (1,'Shampo', '50', '1000','4A'),
(2,'Face wash', '40', '2000','3B'),
(3,'Soup ', '60', '3000','6C'),
(4,'Tea', '30', '1500','1A'),
(5,'Tooth paste', '70', '5500','6D'),
(6,'Nail Polish', '58', '1200','3A'),
(7,'Washing powder', '64', '3200.','1B'),
(8,'Tooth Brush', '94', '3500','2C');

We successfully inserted the data into the table “Product”, as shown in the figure:

Use “SELECT” Query

Now, we want to retrieve the data in the columns from the table “Product”. When we have used “SELECT” along with the * symbol, it returns the entire data of the required table. We can sometimes specify the names of the columns in the “SELECT” query from which we want to fetch data.

>> SELECT * FROM Product

The query returns the data of eight different products. We utilize the * symbol in the “SELECT” query to get all information about the products.

Use BETWEEN Operator

BETWEEN is a logical operator that determines if a value falls within a specified range. The BETWEEN operator evaluates to true when the value falls within the provided limit. The “WHERE” statement of the SELECT, REMOVE and REPLACE commands will utilize the BETWEEN operator.

In this case, we apply the “SELECT” query along with the “WHERE” clause. In the “WHERE” clause, we employ BETWEEN operator on the column “Id”.

>> SELECT Id, Name, Price FROM Product WHERE Id BETWEEN 3 AND 6

After running the previous query, we get the Id, Name, and Price of those products whose Ids lie between 3 and 6.

We utilize the “SELECT” query on the table “Product” to get the data (Name, Quantity, and Price) of the products. But we specify the condition by using the BETWEEN operator in the “WHERE” clause. We also apply for the “ORDER BY” clause in the query, so the output will be sorted in ascending order by using the prices of the products.

>> SELECT Name, Quantity, Price FROM Product WHERE Quantity BETWEEN 40 AND 64 ORDER BY Price

After running the previous query, we obtain the data of the products whose quantity is greater than 40 and less than 64. The BETWEEN operator is applied on the “Quantity” column here.

The BETWEEN operator in SQLite is being applied to retrieve rows of data in a given range in the “SELECT” query. We can employ BETWEEN operator on the names of the products in this example. We want to fetch the data of those products with the starting alphabet of the names between S and W.

>> SELECT Id, Name, Price FROM Product WHERE Name BETWEEN 'S' AND 'W'

In the outcome, we have the Id, Name, and Price of five different products as provided below:

Use NOT BETWEEN Operator

The NOT BETWEEN operator is being used to neglect the output of the BETWEEN operator. If the value of the defined condition would be smaller than the value of the lower range or more than the value of the upper range, the NOT BETWEEN operator provides true. The NOT BETWEEN operator is applied to the column “Price”.

>> SELECT * FROM Product WHERE Price NOT BETWEEN 1000 AND 3000

Here, we get the data of these products whose price is not between the range of 1000 and 3000.

In this case, we employ the NOT BETWEEN operator on the column “Rack” of the table “Product”. We want to get the Name, Quantity, and Rack of the products whose “Rack” number is except 4A and 6C.

>> SELECT Name, Quantity, Rack FROM Product WHERE Rack NOT BETWEEN '4A'

In the output, we have only six products whose “Rack” number is other than 4A and 6C.

Conclusion

We hope this information has helped you to fully grasp SQLite BETWEEN. With the help of several examples of BETWEEN operators, we have discussed the basic structure of BETWEEN and NOT BETWEEN operators in the preceding article. This article also showed us when and how to employ SQLite BETWEEN and NOT BETWEEN. The BETWEEN operator compares a value to a range. The limit comprises a start expression, an AND term, and a terminal expression. Whenever the selected value is within the limit, the operator provides 1. Else, it provides 0.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.