![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Get employee name from a specific company using MySQL GROUP BY?
Let us first create a table −
mysql> create table DemoTable642( EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,EmployeeName archar(100), EmployeeCompanyNamevarchar(100) ); Query OK, 0 rows affected (0.81 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('John','Google'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('Carol','Microsoft'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('John','Amazon'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('Carol','Google'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable642(EmployeeName,EmployeeCompanyName) values('John','Flipkart'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable642;
This will produce the following output −
+------------+--------------+---------------------+ | EmployeeId | EmployeeName | EmployeeCompanyName | +------------+--------------+---------------------+ | 1 | John | Google | | 2 | Carol | Microsoft | | 3 | John | Amazon | | 4 | Carol | Google | | 5 | John | Flipkart | +------------+--------------+---------------------+ 5 rows in set (0.00 sec)
Following is the query to get employee name from a specific company −
mysql> select EmployeeName from DemoTable642 where EmployeeCompanyName="Google" group by EmployeeName;
This will produce the following output −
+--------------+ | EmployeeName | +--------------+ | John | | Carol | +--------------+ 2 rows in set (0.00 sec)
Advertisements