
- 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 get first two highest column values from a table?
To get the first two highest columns, use ORDER BY. With that, use LIMIT 2 to get only the first 2 −
select *from yourTableName order by yourColumnName DESC LIMIT 2;
Let us first create a table −
mysql> create table DemoTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(105); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-------+ | Value | +-------+ | 90 | | 70 | | 40 | | 120 | | 98 | | 105 | +-------+ 6 rows in set (0.00 sec)
Following is the query to get first two highest column values from a table −
mysql> select *from DemoTable order by Value DESC LIMIT 2;
Output
+-------+ | Value | +-------+ | 120 | | 105 | +-------+ 2 rows in set (0.00 sec)
- Related Articles
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- MySQL query to select the nth highest value in a column by skipping values
- MySQL query to find the average of only first three values from a column with five values
- MySQL query to match any of the two strings from column values
- Get the highest score value from a single column and the greatest from two columns in MySQL
- MySQL query to count number of duplicate values in a table column
- Get row data for the lowest and highest values in a MySQL column
- How to get first N characters from a MySQL column?
- MySQL query to remove special characters from column values?
- How to update a MySQL table by swapping two column values?
- MySQL query to select average from distinct column of table?
- A single MySQL query to search multiple words from different column values
- MySQL query to find the top two highest scores
- MySQL query to display the first alphabet from strings in a separate column
- How to get a specific column record from SELECT query in MySQL?

Advertisements