
- 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
How can group functions be used in ORDER BY clause?
We can sort the result set groups by using group functions in the ORDER BY clause. By default, the sort order is ascending but we can reverse it by using DESC keyword.
Example
mysql> Select designation, YEAR(Doj), count(*) from employees GROUP BY designation, YEAR(DoJ) ORDER BY Count(*) DESC; +-------------+-----------+----------+ | designation | YEAR(Doj) | count(*) | +-------------+-----------+----------+ | Prof | 2009 | 2 | | Asst.Prof | 2015 | 1 | | Asst.Prof | 2016 | 1 | | Prof | 2010 | 1 | | Asso.Prof | 2013 | 1 | +-------------+-----------+----------+ 5 rows in set (0.00 sec) mysql> Select designation, YEAR(Doj), count(*) from employees GROUP BY designation, YEAR(DoJ) ORDER BY designation DESC; +-------------+-----------+----------+ | designation | YEAR(Doj) | count(*) | +-------------+-----------+----------+ | Prof | 2009 | 2 | | Prof | 2010 | 1 | | Asst.Prof | 2015 | 1 | | Asst.Prof | 2016 | 1 | | Asso.Prof | 2013 | 1 | +-------------+-----------+----------+ 5 rows in set (0.00 sec)
- Related Articles
- How MySQL LTRIM() and RTRIM()functions can be used with WHERE clause?
- How Can MySQL GROUP BY clause behave like DISTINCT clause?
- How to order or choose rows in MySQL GROUP BY clause?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How Groups function can be used in MySQL SELECT clause?
- How can CONCAT() function be used with MySQL WHERE clause?
- How can CONCAT_WS() function be used with MySQL WHERE clause?
- How LOCATE() function can be used with MySQL WHERE clause?
- How can MySQL REPLACE() function be used with WHERE clause?
- How can we create a MySQL view with GROUP BY clause?
- Why should we not use group functions with non-group fields without GROUP BY clause in MySQL SELECT query?
- How can we use ORDER BY clause while calculating the Date?
- How can we create the MySQL view with ORDER BY clause?
- How to use order by, group by in c#?
- How to use union and order by clause in MySQL?

Advertisements