- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL GROUP BY and CONCAT() to display distinct first and last name
Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (0.92 sec) mysql> alter table DemoTable add index(FirstName,LastName); Query OK, 0 rows affected (1.00 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (1.17 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.66 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | Adam | Smith | | Carol | Taylor | | John | Doe | | John | Doe | +-----------+----------+ 5 rows in set (0.00 sec)
Following is the query to combine distinct First and Last Name −
mysql> select concat(FirstName,' ',LastName) as combinedName from DemoTable group by LastName,FirstName;
This will produce the following output −
+--------------+ | combinedName | +--------------+ | Adam Smith | | Carol Taylor | | John Doe | +--------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to group concat distinct by Id?
- Getting last value in MySQL group concat?
- Display records where first and last name begins with the same letter in MySQL
- Group concatenate the last name from a MySQL column and set a condition to display limited records
- Display records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL
- MySQL query to display columns name first name, last name as full name in a single column?
- Display distinct column name in MySQL
- How to group by column name and ensure the query retrieves the last update in MySQL?
- Split First name and Last name using JavaScript?
- SELECT DISTINCT vs GROUP BY in MySQL?
- GROUP BY and display only non-empty column values in MySQL
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- How to display first day and last day of the month from date records in MySQL?
- How to quote values of single column using GROUP_CONCAT and CONCAT with DISTINCT in MySQL?
- MySQL query to group by names and display the count in a new column

Advertisements