MySQL query to select rows except first row in descending order?


Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Amount int
   -> );
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values(20);
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable values(30);
Query OK, 1 row affected (0.10 sec)

mysql> insert into DemoTable values(40);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+--------+
| Amount |
+--------+
|     10 |
|     20 |
|     30 |
|     40 |
+--------+
4 rows in set (0.00 sec)

Following is the query to select rows except first row in descending order −

mysql> select *from DemoTable WHERE Amount NOT IN (SELECT MAX(Amount) from DemoTable) ORDER BY Amount DESC;

Output

+--------+
| Amount |
+--------+
|     30 |
|     20 |
|     10 |
+--------+
3 rows in set (0.05 sec)
raja
Published on 10-Jul-2019 16:50:33
Advertisements