
- 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 get the count of values and display the count in a new column ordered in descending ordern
Use ORDER BY with DESC to order in descending order. For counting the values, use the COUNT(). For example, if the name “John” appears thrice in the column, then a separate column will display the count 3 and in this way all the count values will be arranged in descending order using the ORDER BY DESC.
Let us first create a table −
mysql> create table DemoTable -> ( -> EmployeeName varchar(100) -> ); Query OK, 0 rows affected (0.85 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------------+ | EmployeeName | +--------------+ | Sam | | David | | David | | Chris | | Robert | | Chris | | Sam | | Sam | | David | | Robert | | David | | Chris | +--------------+ 12 rows in set (0.00 sec)
Following is the query to get the count of values and display the count in a new column ordered in descending order −
mysql> select EmployeeName,count(EmployeeName) as Total from DemoTable -> group by EmployeeName -> order by Total DESC;
Output
+--------------+-------+ | EmployeeName | Total | +--------------+-------+ | David | 4 | | Chris | 3 | | Sam | 3 | | Robert | 2 | +--------------+-------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL query to group by names and display the count in a new column
- MySQL query to count the duplicate ID values and display the result in a separate column
- MySQL query to get the count of distinct records in a column
- Get multiple count in a single MySQL query for specific column values
- Display the count of duplicate records from a column in MySQL and order the result
- MySQL query to display record with maximum count values in a group with other column values?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- MySQL Query to get count of unique values?
- MySQL query to count number of duplicate values in a table column
- Get the count of duplicate values from a single column in MySQL?
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- MySQL query to group results by date and display the count of duplicate values?
- MySQL query to get the length of all columns and display the result in a single new column?
- MySQL query to select the values having multiple occurrence and display their count

Advertisements