
- 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 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 Articles
- MySQL query to sort column values and ignoring quotes on one of the values
- MySQL query to select the nth highest value in a column by skipping values
- MySQL query to group concat and place data into a single row on the basis of 1 values in corresponding column?
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to select column values ending with certain character/number?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- 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
- Select the table name as a column in a UNION select query with MySQL?
- Find average on the basis of corresponding duplicate VARCHAR values in MySQL
- Concatenate rows on the basis of boolean values in another column with MySQL
- MySQL query to fetch the maximum corresponding value from duplicate column values
- MySQL query to find the average of only first three values from a column with five values
- MySQL query to get first two highest column values from a table?
- MySQL query to return the count of only NO values from corresponding column value

Advertisements