← All SQL interview questions

Colleague pairs in the same city, different teams

MediumJoins — Self JoinOracleRSystems

The people team is planning cross-team coffee meetups and wants to pair colleagues who work in the same city but belong to different departments.

From staff, return each pair once as staff_a, dept_a, staff_b, dept_b and city. A pair must not appear twice in mirrored order, and nobody may be paired with themselves. Order by city, then the two staff_id values.

Sample data

Table: staff

12 row(s) · showing first 8

staff_idINTstaff_nameTEXTcityTEXTdepartmentTEXT
1Aditi RaoBengaluruData
2Bilal KhanBengaluruEngineering
3Charu IyerBengaluruData
4Dinesh PaiPuneEngineering
5Esha JoshiPuneSales
6Faisal AhmedHyderabadData
7Gita NambiarHyderabadSales
8Hari OmDelhiEngineering