Back to Yugabyte Db

SELF JOIN

community/learn/content/01-SQL-Basics/03-joins/03-self-join.md

2026.1.0.0-b25374 B
Original Source

SELECT with a self JOIN

In this exercise we will query the orders table using a self JOIN.

SELECT a.employee_id AS employee_id_1,
       b.employee_id AS employee_id_2,
       a.customer_id
FROM orders a,
     orders b
WHERE a.employee_id <> b.employee_id
  AND a.customer_id=b.customer_id
ORDER BY a.employee_id;

The query should return 8,704 rows.