
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to select particular range of values in a MySQL table?
In order to select particular range of values in a MySQL table, you can use WHERE clause. Let us first create a table:
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(200), CustomerAge int, isRegularCustomer bool ); Query OK, 0 rows affected (0.57 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Chris',24,true); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Robert',26,false); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Mike',27,false); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Larry',22,true); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Sam',30,true); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(CustomerName,CustomerAge,isRegularCustomer)values('Carol',26,true); Query OK, 1 row affected (0.16 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+------------+--------------+-------------+-------------------+ | CustomerId | CustomerName | CustomerAge | isRegularCustomer | +------------+--------------+-------------+-------------------+ | 1 | Chris | 24 | 1 | | 2 | Robert | 26 | 0 | | 3 | Mike | 27 | 0 | | 4 | Larry | 22 | 1 | | 5 | Sam | 30 | 1 | | 6 | Carol | 26 | 1 | +------------+--------------+-------------+-------------------+ 6 rows in set (0.00 sec)
Following is the query to select particular range of values in a MySQL table. Here, we are selecting customer records with age greater than equal to 24 and less than equal to 27. Also, the customer should be a regular customer i.e. with 1 value under “isRegularCustomer” column:
mysql> select CustomerId,CustomerName,CustomerAge,isRegularCustomer from DemoTable where isRegularCustomer=true AND CustomerAge >=24 AND CustomerAge <= 27;
This will produce the following output
+------------+--------------+-------------+-------------------+ | CustomerId | CustomerName | CustomerAge | isRegularCustomer | +------------+--------------+-------------+-------------------+ | 1 | Chris | 24 | 1 | | 6 | Carol | 26 | 1 | +------------+--------------+-------------+-------------------+ 2 rows in set (0.00 sec)
- Related Articles
- In MySQL how to replace all NULL values in a particular field of a particular table?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Auto insert values into a MySQL table in a range?
- MySQL Select IN range?
- MySQL query to select records from a table on the basis of a particular month number?
- Select and insert values with preceding zeros in a MySQL table
- How to replace values of select return in MySQL?
- How can we update MySQL table after removing a particular string from the values of column?
- How to check table status of the tables in a particular MySQL database?
- MySQL query to select records with a particular date?
- How to find a particular varchar id in MySQL from a list of values?
- How to get the values of a particular row in a table in Selenium with python?
- How can we create a MySQL view by selecting some range of values from a base table?
- Select specific rows in a range with MySQL?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?

Advertisements