BartekPogoda

Untitled

Mar 30th, 2017
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.95 KB | None | 0 0
  1. -- 1. Group functions work across many rows to produce one result per group.
  2. -- TRUE
  3.  
  4.  
  5. -- 2. Group functions include nulls in calculations.
  6. -- TRUE
  7.  
  8.  
  9. -- 3.  The WHERE clause restricts rows prior to inclusion in a group calculation.
  10. -- TRUE
  11.  
  12.  
  13. /* 4. Display the highest, lowest, sum, and average sa
  14. lary of all employees. Label the columns Maximum, M
  15. inimum,
  16. Sum, and Average, respectively. Round your results
  17. to the nearest whole number.  */
  18.  
  19. SELECT
  20.   ROUND(MAX(salary),0) "Maximum",
  21.   ROUND(MIN(salary),0) "Minimum",
  22.   ROUND(SUM(salary),0) "Sum",
  23.   ROUND(AVG(salary),0) "Average"
  24. FROM
  25.   employees;
  26.  
  27.  
  28.   /* 6. Write a query to display the number of people wi
  29. th the same job.  */
  30.  
  31. SELECT
  32.   job_id, COUNT(employee_id) "Empl count"
  33. FROM
  34.   employees
  35. GROUP BY
  36.   job_id;
  37.  
  38.  
  39.   /* 8. Write a query that displays the difference betwe
  40. en the highest and lowest salaries. Label the colum n DIFFERENCE. */
  41.  
  42. SELECT
  43.   ROUND(MAX(salary)-MIN(salary),0) "DIFFERENCE"
  44. FROM
  45.   employees;
  46.  
  47.  
  48.  /* 9. Display the manager number and the salary of the
  49.  lowest paid employee for that manager. Exclude any one whose
  50. manager is not known. Exclude any groups where the
  51. minimum salary is less than $6,000. Sort the output
  52. in descending order of salary. */
  53.  
  54. SELECT
  55.   manager_id, MIN(salary)
  56. FROM
  57.   employees
  58. WHERE
  59.   manager_id IS NOT NULL
  60. GROUP BY
  61.   manager_id
  62. HAVING
  63.   MIN(salary) >= 6000
  64. ORDER BY
  65.   MIN(salary) DESC;
  66.  
  67.  
  68. /*  13. Displays the number of employees in department 50*/
  69.  
  70. SELECT
  71.   COUNT(*)
  72. FROM
  73.   employees
  74. WHERE department_id = 50;
  75.  
  76. /* 16. Display the number of distinct department values  in the EMPLOYEES table. */
  77.  
  78. SELECT
  79.   COUNT(DISTINCT department_id)
  80. FROM
  81.   employees;
  82.  
  83.  
  84. /* 19. Display of average salaries of those departments
  85. that have  an average salary greater than $8,000  */
  86.  
  87. SELECT
  88.   department_id, ROUND(AVG(salary),0)
  89. FROM
  90.   employees
  91. GROUP BY
  92.   department_id
  93. HAVING
  94.   AVG(salary) > 8000;
Advertisement
Add Comment
Please, Sign In to add comment