Skip to content

Instantly share code, notes, and snippets.

@kenjipm
Last active November 23, 2021 10:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kenjipm/c1904b78792b3dc3c9e8e46a6c225fac to your computer and use it in GitHub Desktop.
Save kenjipm/c1904b78792b3dc3c9e8e46a6c225fac to your computer and use it in GitHub Desktop.
The following data definition defines an organization's employee hierarchy. An employee is a manager if any other employee has their managerId set to the first employees id. An employee who is a manager may or may not also have a manager. TABLE employees id INTEGER NOT NULL PRIMARY KEY managerId INTEGER REFERENCES employees(id) name VARCHAR(30) …
SELECT name FROM employees
WHERE id NOT IN (SELECT managerId FROM employees WHERE managerId IS NOT NULL);
@nazneenparveen
Copy link

CREATE TABLE employees (
id INTEGER NOT NULL PRIMARY KEY,
managerId INTEGER REFERENCES employees(id),
name VARCHAR(30) NOT NULL
);

INSERT INTO employees(id, managerId, name) VALUES(1, NULL, 'John');
INSERT INTO employees(id, managerId, name) VALUES(2, 1, 'Mike');

-- Expected output (in any order):
-- name


-- Mike

-- Explanation:
-- In this example.
-- John is Mike's manager. Mike does not manage anyone.
-- Mike is the only employee who does not manage anyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment