
- 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
MySQL query to return 5 random records from last 20 records?
For this, you need to use ORDER BY to order records. With that use RAND() to get random records and LIMIT 5 since we want to display only 5 random records.
Let us first create a table −
mysql> create table DemoTable773 (StudentId int); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable773 values(100); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable773 values(200); Query OK, 1 row affected (0.87 sec) mysql> insert into DemoTable773 values(300); Query OK, 1 row affected (1.59 sec) mysql> insert into DemoTable773 values(400); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable773 values(500); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable773 values(1); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable773 values(2); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable773 values(3); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable773 values(4); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable773 values(5); Query OK, 1 row affected (0.77 sec) mysql> insert into DemoTable773 values(6); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable773 values(7); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable773 values(8); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable773 values(9); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable773 values(10); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable773 values(90); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable773 values(91); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable773 values(92); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable773 values(93); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable773 values(94); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable773 values(95); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable773 values(96); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable773 values(97); Query OK, 1 row affected (0.27 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable773;
This will produce the following output -
+-----------+ | StudentId | +-----------+ | 100 | | 200 | | 300 | | 400 | | 500 | | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | | 90 | | 91 | | 92 | | 93 | | 94 | | 95 | | 96 | | 97 | +-----------+ 23 rows in set (0.00 sec)
Following is the query to return 5 random records from
the last 20 records −
mysql> select *from ( select * from DemoTable773 order by StudentId desc limit 20 ) AS RANDOM_OUTPUT order by rand() limit 5;
This will produce the following output -
+-----------+ | StudentId | +-----------+ | 95 | | 4 | | 10 | | 7 | | 300 | +-----------+ 5 rows in set (0.51 sec)
- Related Articles
- Select last 20 records ordered in ascending order in MySQL?
- How to order last 5 records by ID in MySQL
- Return maximum value from records in MySQL
- MySQL SELECT query to return records with specific month and year
- MySQL query to return multiple row records with AND & OR operator
- MySQL query to find alternative records from a table
- MySQL Query to set currency records
- MySQL query to display the count of distinct records from a column with duplicate records
- MongoDB query to skip first 5 records and display only the last 5 of them
- MySQL query to select records beginning from a specific id
- MySQL query to fetch records from a range of months?
- MySQL query to select top 10 records?
- MySQL query to select bottom n records
- MySQL query to insert multiple records quickly
- MySQL query to return all records with a datetime older than 1 week

Advertisements