Search code examples
mysqlsqldatabaserdbms

SQL query to find names of Employess living in the same city


Image of the Table

I am trying to output Employee names for employees who live in the same city.

It should not be like where city='Pune' or anything static.

If a City occurs more than once then display the name of employees living in that city.

Expected Output

| eid | ename    | city   
+-----+----------+--------  
|   2 | Abhishek | Pune   
|   1 | Amitab   | Pune  
|   4 | Amy      | Mumbai  
|   5 | John     | Mumbai  
|   3 | Rupali   | Mumbai  
|   7 | Sachin   | Satara  
|   6 | Sachin   | Satara  

Solution

  • select city, group_concat(distinct ename) from emp group by city