
- 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
Select results from the middle of a sorted list in MySQL?
To select results from the middle of a sorted list, use ORDER BY clause along with LIMIT.
Let us first create a table. Following is the query −
mysql> create table sortedListDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.46 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into sortedListDemo(StudentName) values('John'); Query OK, 1 row affected (0.62 sec) mysql> insert into sortedListDemo(StudentName) values('Sam'); Query OK, 1 row affected (0.18 sec) mysql> insert into sortedListDemo(StudentName) values('Adam'); Query OK, 1 row affected (0.13 sec) mysql> insert into sortedListDemo(StudentName) values('James'); Query OK, 1 row affected (0.21 sec) mysql> insert into sortedListDemo(StudentName) values('Jace'); Query OK, 1 row affected (0.13 sec) mysql> insert into sortedListDemo(StudentName) values('Mike'); Query OK, 1 row affected (0.12 sec) mysql> insert into sortedListDemo(StudentName) values('Carol'); Query OK, 1 row affected (0.18 sec) mysql> insert into sortedListDemo(StudentName) values('Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into sortedListDemo(StudentName) values('Ramit'); Query OK, 1 row affected (0.16 sec) mysql> insert into sortedListDemo(StudentName) values('Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into sortedListDemo(StudentName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into sortedListDemo(StudentName) values('David'); Query OK, 1 row affected (0.21 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from sortedListDemo;
This will produce the following output −
+----+-------------+ | Id | StudentName | +----+-------------+ | 1 | John | | 2 | Sam | | 3 | Adam | | 4 | James | | 5 | Jace | | 6 | Mike | | 7 | Carol | | 8 | Bob | | 9 | Ramit | | 10 | Chris | | 11 | Robert | | 12 | David | +----+-------------+ 12 rows in set (0.00 sec)
Following is the query to select results from the middle of a sorted list. We have set LIMIT as 4, 6 that means 6 records will be displayed randomly −
mysql> select *from sortedListDemo -> order by StudentName -> LIMIT 4,6;
This will produce the following output −
+----+-------------+ | Id | StudentName | +----+-------------+ | 12 | David | | 5 | Jace | | 4 | James | | 1 | John | | 6 | Mike | | 9 | Ramit | +----+-------------+ 6 rows in set (0.00 sec)
- Related Articles
- Insert the results of a MySQL select? Is it possible?
- Combine SELECT & SHOW command results in MySQL?
- Select random number from a specific list in MySQL?
- Limit length of longtext field in MySQL SELECT results?
- Select * but ignore displaying results containing a specific character in MySQL
- How to order results of a query randomly & select random rows in MySQL?
- MySQL SELECT to skip first N results?\n
- Populating a table from query results in MySQL?
- Select first element of a commaseparated list in MySQL?
- Display MySQL Results as comma separated list?
- Python program to delete a node from the middle of the Circular Linked List
- Remove Duplicates from Sorted List in C++
- Find kth node from Middle towards Head of a Linked List in C++
- Python program to delete a new node from the middle of the doubly linked list
- Add results from several COUNT queries in MySQL?

Advertisements