
- 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 select bottom n records
Let us first create a table −
mysql> create table DemoTable724 (Value int); Query OK, 0 rows affected (0.79 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable724 values(101); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable724 values(183); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable724 values(983); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable724 values(234); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable724 values(755); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable724 values(435); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable724 values(675); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable724 values(345); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable724 values(948); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable724;
This will produce the following output -
+-------+ | Value | +-------+ | 101 | | 183 | | 983 | | 234 | | 755 | | 435 | | 675 | | 345 | | 948 | +-------+ 9 rows in set (0.00 sec)
Following is the query to select bottom n records. Here, we have selected 4 records after ordering in DESC. This would fetch us the last 4 records from the initial values −
mysql> select *from DemoTable724 order by Value DESC LIMIT 0,4;
This will produce the following output -
+-------+ | Value | +-------+ | 983 | | 948 | | 755 | | 675 | +-------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to select top 10 records?
- MySQL query to select records with a particular date?
- MySQL query to select records beginning from a specific id
- MySQL query to select records approaching in next 12 hours?
- How to write a MySQL query to select first 10 records?
- MySQL query to select top n rows efficiently?
- MySQL query to select records with a particular date and time?
- MySQL SELECT query to return records with specific month and year
- MySQL Query to set currency records
- MongoDB Query to select records having a given key?
- MySQL query to select everything to left of last space in a column with name records
- MySQL query to select one specific row and another random row?\n
- MySQL query to insert multiple records quickly
- MySQL query to select records from a table on the basis of a particular month number?
- MySQL query to return 5 random records from last 20 records?

Advertisements