
- 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 display the count of distinct records from a column with duplicate records
Let us first create a table −
mysql> create table DemoTable ( StudentId int, StudentFirstName varchar(100) ); Query OK, 0 rows affected (0.88 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'John'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(101,'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(102,'John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(103,'Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(104,'Sam'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+ | StudentId | StudentFirstName | +-----------+------------------+ | 100 | John | | 101 | Chris | | 102 | John | | 103 | Sam | | 104 | Sam | +-----------+------------------+ 5 rows in set (0.00 sec)
Following is the query to display the count of distinct records from a column with duplicate records −
mysql> select count(distinct StudentFirstName) from DemoTable;
This will produce the following output −
+----------------------------------+ | count(distinct StudentFirstName) | +----------------------------------+ | 3 | +----------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to get the count of distinct records in a column
- Display distinct dates in MySQL from a column with date records
- Display the count of duplicate records from a column in MySQL and order the result
- How to display the count from distinct records in the same row with MySQL?
- Count the occurrences of specific records (duplicate) in one MySQL query
- MySQL select only duplicate records from database and display the count as well?
- MySQL query to subtract date records with week day and display the weekday with records
- Add records from corresponding duplicate values in another column with MySQL
- How do we count the total duplicate records in a column of MySQL table?
- Find and display duplicate records in MySQL?
- How do we count the records from MySQL table where column holds duplicate/triplicates data?
- Display month names and year from a column with date records with MySQL
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MySQL query to display records from a table filtered using LIKE with multiple words?
- MySQL query to count records that begin with specific letters

Advertisements