Use ORDER BY to sort data for duplicate record.
Let us first create a table −
mysql> create table DemoTable788 ( FirstName varchar(100), Score int ); Query OK, 0 rows affected (1.89 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable788 values('Chris',78); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable788 values('Robert',67); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable788 values('Chris',98); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable788 values('Chris',56); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable788 values('Robert',43); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable788 values('Robert',97); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable788 values('Chris',79); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable788;
This will produce the following output -
+-----------+-------+ | FirstName | Score | +-----------+-------+ | Chris | 78 | | Robert | 67 | | Chris | 98 | | Chris | 56 | | Robert | 43 | | Robert | 97 | | Chris | 79 | +-----------+-------+ 7 rows in set (0.00 sec)
Following is the query to sort data for duplicate record. We are sorting data for duplicate name ‘Chris’ −
mysql> select Score from DemoTable788 where FirstName='Chris' order by Score DESC;
This will produce the following output -
+-------+ | Score | +-------+ | 98 | | 79 | | 78 | | 56 | +-------+ 4 rows in set (0.00 sec)