选择所有员工的姓名及其老板的姓名的查询

Query that selects the names of all employees and the names of their bosses

我需要编写一个查询来选择所有员工的姓名及其老板的姓名。如果员工没有老板,return NULL。老板也算员工。

Table:

CREATE TABLE employees (
  id INTEGER NOT NULL PRIMARY KEY,
  bossId INTEGER,
  name VARCHAR(20) NOT NULL,
  FOREIGN KEY (bossId) REFERENCES employees(id)
  );
  
  INSERT INTO employees(id, bossId, name) VALUES(1, NULL, 'Fred');
  INSERT INTO employees(id, bossId, name) VALUES(2, 1, 'Jason');
  INSERT INTO employees(id, bossId, name) VALUES(3, 1, 'Mike');

预期结果:

name boss
Fred
Jason Fred
Mike Fred

你需要左连接,如果员工没有老板那么在输出中显示空值

select emp.name,empBoss.name
from employees  emp 
left join employees empBoss
on emp.bossId = empBoss.id