Skip to content

Instantly share code, notes, and snippets.

@tusharbihani
Forked from rsepassi/JitBitSQLAnswers.sql
Created March 18, 2019 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tusharbihani/80382682abffc62d7c11801d19f10be5 to your computer and use it in GitHub Desktop.
Save tusharbihani/80382682abffc62d7c11801d19f10be5 to your computer and use it in GitHub Desktop.
JitBit SQL Interview Questions Answered
-- JitBit SQL Interview Questions (Posted on Hacker News)
-- http://www.jitbit.com/news/181-jitbits-sql-interview-questions/?utm_source=hackernewsletter&utm_medium=email
-- Schema:
-- employees table
-- EmployeeID
-- DepartmentID
-- BossID
-- Name
-- Salary
-- departments table
-- DepartmentID
-- Name
-- List employees (names) who have a bigger salary than their boss
SELECT *
FROM employees
JOIN employees AS bosses
ON employees.BossID = bosses.id
WHERE employees.salary > bosses.salary
-- List employees who have the biggest salary in their departments
SELECT *
FROM employees
JOIN employees AS colleagues
ON employees.DepartmentID = colleagues.DepartmentID
WHERE employees.salary >= colleagues.salary
-- List departments that have less than 3 people in it
SELECT *,
COUNT(employees.DepartmentID) AS employeeCount
FROM departments
JOIN employees
ON employees.DepartmentID = departments.DepartmentID
GROUP BY departments.DepartmentID
HAVING employeeCount < 3
-- List all departments along with the number of people there (tricky - people often do an "inner join" leaving out empty departments)
SELECT *,
COUNT(employees.DepartmentID) AS employeeCount
FROM departments
LEFT JOIN employees
ON employees.DepartmentID = departments.DepartmentID
GROUP BY departments.DepartmentID
-- List employees that don't have a boss in the same department
SELECT *
FROM employees
JOIN employees AS bosses
ON employees.id = bosses.BossID
WHERE employees.DepartmentID != bosses.DepartmentID
-- List all departments along with the total salary there
SELECT *,
SUM(employees.Salary) AS total_salary
FROM departments
LEFT JOIN employees
ON departments.DepartmentID = employees.DepartmentID
GROUP BY departments.DepartmentID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment