

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to select three highest values and sort alphabetically on the basis of corresponding column with name
For this, you can use the ORDER BY clause. Let us first create a table −
mysql> create table DemoTable ( Name varchar(40), Score int ); Query OK, 0 rows affected (1.11 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris',45); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Bob',98); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David',78); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Mike',96); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Carol',43); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------+ | Name | Score | +-------+-------+ | Chris | 45 | | Bob | 98 | | David | 78 | | Mike | 96 | | Carol | 43 | +-------+-------+ 5 rows in set (0.00 sec)
Following is the query to select three highest values from “Score” column and sort the corresponding “Name” column alphabetically −
mysql> select *from (select Name,Score from DemoTable order by Score desc,Name asc limit 3) tbl order by Name;
This will produce the following output −
+-------+-------+ | Name | Score | +-------+-------+ | Bob | 98 | | David | 78 | | Mike | 96 | +-------+-------+ 3 rows in set (0.03 sec)
- Related Questions & Answers
- MySQL query to sort column values and ignoring quotes on one of the values
- MySQL query to group concat and place data into a single row on the basis of 1 values in corresponding column?
- Find average on the basis of corresponding duplicate VARCHAR values in MySQL
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to select the nth highest value in a column by skipping values
- Add a column count in a MySQL query on the basis of last name records?
- Match column values on the basis of the other two column values in MySQL
- Concatenate rows on the basis of boolean values in another column with MySQL
- Select the table name as a column in a UNION select query with MySQL?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- MySQL query to find the average of only first three values from a column with five values
- MySQL query to fetch the maximum corresponding value from duplicate column values
- MySQL query to select column values ending with certain character/number?
- MySQL query to return the count of only NO values from corresponding column value
- Select distinct values from three columns and display in a single column with MySQL
Advertisements