
- 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 alphabetize records and count the duplicates?
For this, use both GROUP BY and ORDER BY clause. Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentGrade char(1) ); Query OK, 0 rows affected (0.87 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentGrade) values('A'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentGrade) values('F'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentGrade) values('C'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable(StudentGrade) values('A'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(StudentGrade) values('F'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentGrade) values('F'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentGrade) values('B'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(StudentGrade) values('C'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-----------+--------------+ | StudentId | StudentGrade | +-----------+--------------+ | 1 | A | | 2 | F | | 3 | C | | 4 | A | | 5 | F | | 6 | F | | 7 | B | | 8 | C | +-----------+--------------+ 8 rows in set (0.00 sec)
Here is the query to alphabetize and count −
mysql> select StudentGrade,count(StudentId) as Frequency from DemoTable group by StudentGrade order by StudentGrade;
Output
+--------------+-----------+ | StudentGrade | Frequency | +--------------+-----------+ | A | 2 | | B | 1 | | C | 2 | | F | 3 | +--------------+-----------+ 4 rows in set (0.00 sec)
- Related Articles
- Count duplicates records in MySQL table?
- MySQL query to count records that begin with specific letters
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to get the count of distinct records in a column
- Count the occurrences of specific records (duplicate) in one MySQL query
- MySQL query to avoid displaying duplicates values?
- MySQL Query to set currency records
- MySQL query to find single value from duplicates with certain condition by excluding other records using NOT IN
- MySQL query to find a match and fetch records
- MySQL query to subtract date records with week day and display the weekday with records
- Generate the row count (serial number) of records after returning the result in MySQL query?
- MySQL query to select top 10 records?
- MySQL query to select bottom n records
- MySQL query to insert multiple records quickly
- MongoDB query to count records on the basis of matching criteria

Advertisements