Get employee name from a specific company using MySQL GROUP BY?

MySQLMySQLi Database

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)
raja
Published on 23-Aug-2019 10:57:30
Advertisements