Guest User

Untitled

a guest
May 28th, 2013
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. http://www.jitbit.com/news/181-jitbits-sql-interview-questions/
  2.  
  3. # DATABASE
  4.  
  5. CREATE TABLE `departments` (
  6. `DepartmentID` int(11) NOT NULL AUTO_INCREMENT,
  7. `Name` varchar(255) NOT NULL,
  8. PRIMARY KEY (`DepartmentID`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
  10.  
  11. INSERT INTO `departments` VALUES
  12. (1, 'One'),
  13. (2, 'Two'),
  14. (3, 'Three');
  15.  
  16. CREATE TABLE `employees2` (
  17. `EmployeeID` int(11) NOT NULL AUTO_INCREMENT,
  18. `DepartmentID` int(11) NOT NULL,
  19. `BossID` int(11) NOT NULL,
  20. `Name` varchar(255) NOT NULL,
  21. `Salary` int(11) NOT NULL,
  22. PRIMARY KEY (`EmployeeID`)
  23. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
  24.  
  25. INSERT INTO `employees2` VALUES
  26. (1, 1, 0, 'Person1', 150),
  27. (2, 1, 1, 'Person2', 100),
  28. (3, 1, 1, 'Person3', 100),
  29. (4, 2, 2, 'Person4', 65),
  30. (5, 2, 2, 'Person5', 60),
  31. (6, 2, 2, 'Person6', 60),
  32. (7, 2, 2, 'Person7', 55),
  33. (8, 3, 3, 'Person8', 50),
  34. (9, 3, 3, 'Person9', 160);
  35.  
  36.  
  37. # ANSWERS
  38.  
  39. List employees (names) who have a bigger salary than their boss
  40.  
  41. SELECT e2.Name AS 'Employee', e.Name AS 'Boss', e.Salary AS 'Employee salary', e2.Salary AS 'Boss salary'
  42. FROM Employees e
  43. INNER JOIN Employees e2 ON e.BossID = e2.EmployeeID
  44. WHERE e2.Salary < e.Salary
  45.  
  46. List employees who have the biggest salary in their departments
  47.  
  48. SELECT e.Name AS 'Employee', d.Name AS 'Department', e.Salary AS 'Salary'
  49. FROM Departments d
  50. INNER JOIN Employees e ON e.DepartmentID = d.DepartmentID
  51. GROUP BY d.DepartmentID
  52. ORDER BY e.Salary DESC
  53.  
  54. List departments that have less than 3 people in it
  55.  
  56. SELECT d.Name AS 'Department'
  57. FROM Departments d
  58. INNER JOIN Employees e ON e.DepartmentID = d.DepartmentID
  59. GROUP BY d.DepartmentID
  60. HAVING COUNT(e.EmployeeId) < 3
  61.  
  62. List all departments along with the number of people there (tricky - people often do an "inner join" leaving out empty departments)
  63.  
  64. SELECT d.Name AS 'Department', COUNT(e.EmployeeId) AS 'Employee count'
  65. FROM Departments d
  66. LEFT OUTER JOIN Employees e ON e.DepartmentID = d.DepartmentID
  67. GROUP BY d.DepartmentID
  68.  
  69. List employees that don't have a boss in the same department
  70.  
  71. SELECT e.Name AS 'Employee', e2.Name AS 'Boss', d.Name AS "Employee's department", d2.Name AS "Boss' department"
  72. FROM Employees e
  73. INNER JOIN Employees e2 ON e.BossiD = e2.EmployeeID
  74. INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID
  75. INNER JOIN Departments d2 ON e2.DepartmentID = d2.DepartmentID
  76. WHERE e.BossID != 0 AND d.DepartmentID != d2.DepartmentID
  77.  
  78. List all departments along with the total salary there
  79.  
  80. SELECT d.Name AS 'Department', SUM(e.Salary) AS 'Total salary'
  81. FROM Employees e
  82. INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID
  83. GROUP BY d.DepartmentID
  84.  
  85.  
  86. By lucb1e / https://lucb1e.com/
Advertisement
Add Comment
Please, Sign In to add comment