Get three records having higher value from MySQL


Let us first create a table −

mysql> create table DemoTable1614
   -> (
   -> StudentName varchar(20),
   -> StudentScore int
   -> );
Query OK, 0 rows affected (0.78 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1614 values('Adam',65);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1614 values('Chris',89);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1614 values('Bob',58);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable1614 values('Sam',98);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1614 values('Mike',87);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1614 values('Carol',79);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1614;

This will produce the following output

+-------------+--------------+
| StudentName | StudentScore |
+-------------+--------------+
| Adam        |           65 |
| Chris       |           89 |
| Bob         |           58 |
| Sam         |           98 |
| Mike        |           87 |
| Carol       |           79 |
+-------------+--------------+
6 rows in set (0.00 sec)

Here is the query to get 3 records having higher value −

mysql> select * from DemoTable1614 order by StudentScore DESC LIMIT 3;

This will produce the following output

+-------------+--------------+

| StudentName | StudentScore |
+-------------+--------------+
| Sam         |           98 |
| Chris       |           89 |
| Mike        |           87 |
+-------------+--------------+
3 rows in set (0.00 sec)

Updated on: 17-Dec-2019

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements