Advertisement
Georgi_Benchev

sql_in_class2

Dec 19th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 7.09 KB | None | 0 0
  1. /* 1 Write a SQL query to find the average salary of employees who have been hired before
  2.    year 2000 incl. Round it down to the nearest integer.*/
  3. SELECT ROUND(AVG(salary)) AS avg_salary
  4. FROM employees
  5. WHERE YEAR(hire_date) < 2000;
  6.  
  7.  
  8. /* 2 Write a SQL query to find all town names that end with a vowel (a,o,u,e,i).*/
  9. SELECT name
  10. FROM towns
  11. WHERE RIGHT(name, 1) IN ('e', 'o', 'u', 'e', 'i');
  12.  
  13.  
  14. /* 3 Write a SQL query to find all town names that start with a vowel (a,o,u,e,i).*/
  15. SELECT name
  16. FROM towns
  17. WHERE LEFT(name, 1) IN ('e', 'o', 'u', 'e', 'i');
  18.  
  19.  
  20. /* 4 Write a SQL query that outputs the name and the length of the town with the longest name.*/
  21. SELECT name, LENGTH(name) AS name_length
  22. FROM towns
  23. ORDER BY LENGTH(name) DESC
  24. LIMIT 1;
  25.  
  26.  
  27. /* 5 Write a SQL query that outputs the name and the length of the town with the shortest name.*/
  28. SELECT name, LENGTH(name) AS name_length
  29. FROM towns
  30. ORDER BY LENGTH(name)
  31. LIMIT 1;
  32.  
  33.  
  34. /* 6 Write a SQL query to find all employees with even IDs.*/
  35. SELECT *
  36. FROM employees
  37. WHERE employee_id % 2 = 0;
  38.  
  39.  
  40. /* 7 Write a SQL query to find the names and salaries of the employees
  41.      that take the minimal salary in the company.*/
  42. SELECT *
  43. FROM employees
  44. WHERE salary = (SELECT MIN(salary) FROM employees);
  45.  
  46.  
  47. /* 8 Write a SQL query to find the names and salaries of the employees
  48.    that have a salary that is up to 10% higher than the minimal salary for the company.*/
  49. SELECT *
  50. FROM employees
  51. WHERE salary <= (SELECT MIN(salary) * 1.1 FROM employees);
  52.  
  53.  
  54. /* 9 Write a SQL query to find the full name, salary and department of the employees
  55.    that take the minimal salary in their department.*/
  56. SELECT e.first_name, e.last_name, e.salary, m.name
  57. FROM employees e
  58.          JOIN departments m
  59.               ON e.department_id = m.department_id
  60. WHERE e.salary = (SELECT MIN(salary)
  61.                   FROM employees
  62.                   WHERE department_id = e.department_id
  63.                   GROUP BY department_id);
  64.  
  65.  
  66. /* 10 Write a SQL query to find the average salary in department #1.*/
  67. SELECT AVG(salary) AS average_salary_dep_1
  68. FROM employees e
  69. WHERE department_id = 1;
  70.  
  71.  
  72. /* 11 Write a SQL query to find the average salary in the "Sales" department.*/
  73. SELECT AVG(salary) AS avg_salary_sales
  74. FROM employees e
  75.          JOIN departments d
  76.               ON e.department_id = d.department_id
  77. WHERE d.name = 'Sales';
  78.  
  79.  
  80. /* 12 Write a SQL query that outputs the number of employees in the "Sales" department.*/
  81. SELECT COUNT(*) AS employees_in_sales
  82. FROM employees e
  83.          JOIN departments d
  84.               ON e.department_id = d.department_id
  85. WHERE d.name = 'Sales';
  86.  
  87.  
  88. /* 13 Write a SQL query that outputs the number of employees that have manager.*/
  89. SELECT COUNT(*) AS have_manager
  90. FROM employees
  91. WHERE manager_id IS NOT NULL;
  92.  
  93.  
  94. /* 14 Write a SQL query that outputs the number of employees that don't have manager.*/
  95. SELECT COUNT(*) AS dont_have_manager
  96. FROM employees
  97. WHERE manager_id IS NULL;
  98.  
  99.  
  100. /* 15 Write a SQL query to find all departments, along with the average salary for each of them.*/
  101. SELECT d.name, AVG(e.salary) AS avg_salary
  102. FROM employees e
  103.          JOIN departments d
  104.               ON d.department_id = e.department_id
  105. GROUP BY d.name;
  106.  
  107.  
  108. /* 16 Write a SQL query to find all projects that took less than 1 year (365 days) to complete.*/
  109. SELECT projects.name
  110. FROM projects
  111. WHERE datediff(end_date, start_date) < 365;
  112.  
  113.  
  114. /* 17 Write a SQL query that outputs the number for employees from each town, grouped by department.
  115.    For example how many people from Bellevue work in Sales.
  116.    How many people from Calgary work in Marketing, and so on...*/
  117. SELECT t.name AS town, d.name AS department, COUNT(e.employee_id) AS number_of_employees
  118. FROM employees e
  119.          JOIN towns t ON e.employee_id = t.town_id
  120.          JOIN departments d ON e.department_id = d.department_id
  121. GROUP BY d.name, t.name;
  122.  
  123.  
  124. /* 18 Write a SQL query that outputs the first and last name of all managers that have exactly 5 employees.*/
  125. SELECT m.first_name, e.last_name
  126. FROM employees m
  127.          JOIN employees e
  128.               ON m.employee_id = e.manager_id
  129. GROUP BY m.employee_id
  130. HAVING COUNT(e.employee_id) = 5;
  131.  
  132.  
  133. /* 19 Write a SQL query to find all employees along with their managers.
  134.    For employees that do not have manager display the value "(no manager)".*/
  135. SELECT CONCAT(e.first_name, ' ', e.last_name)                       AS empoloyee,
  136.        IFNULL(CONCAT(m.first_name, ' ', m.last_name), 'no manager') AS manager
  137. FROM employees e
  138.          LEFT JOIN employees m
  139.                    ON m.employee_id = e.manager_id;
  140.  
  141.  
  142. /* 20 Write a SQL query that outputs the names of all employees whose last name is exactly 5 characters long.*/
  143. SELECT first_name, last_name
  144. FROM employees
  145. WHERE LENGTH(last_name) = 5;
  146.  
  147.  
  148. /* 21 Write a SQL query that outputs the current date and time in the following format
  149.    "day.month.year hour:minutes:seconds:milliseconds". */
  150. SELECT DATE_FORMAT(NOW(), '%d.%m.%Y %H:%i:%s');
  151.  
  152.  
  153. /* 22 Write a SQL query to display the average employee salary by department and job title.
  154.    For example, the average salary in Engineering for Design Engineer is 32,700.*/
  155. SELECT d.name, e.job_title, ROUND(avg(e.salary)) AS avg_salary
  156. FROM employees e
  157.          JOIN departments d
  158.               ON e.department_id = d.department_id
  159. GROUP BY d.name, e.job_title;
  160.  
  161.  
  162. /* 23 Write a SQL query that outputs the town with most employees.*/
  163. SELECT t.name AS town_name, COUNT(e.employee_id) AS employees_count
  164. FROM employees e
  165.          JOIN addresses a ON e.address_id = a.address_id
  166.          JOIN towns t ON a.town_id = t.town_id
  167. GROUP BY t.name
  168. ORDER BY employees_count DESC
  169. LIMIT 1;
  170.  
  171.  
  172. /* 24 Write a SQL query that outputs the number of managers from each town.*/
  173. SELECT t.name AS town_name, COUNT(DISTINCT e.employee_id) AS managers_count
  174. FROM employees e
  175.          JOIN addresses a ON e.address_id = a.address_id
  176.          JOIN towns t ON a.town_id = t.town_id
  177. WHERE e.employee_id IN (SELECT DISTINCT manager_id FROM employees WHERE manager_id IS NOT NULL)
  178. GROUP BY t.name
  179. ORDER BY managers_count DESC;
  180.  
  181.  
  182. /* 25 Write a SQL query to find the manager who is in charge of the most employees
  183.       and their average salary.*/
  184. SELECT CONCAT(m.first_name, ' ', m.last_name) AS manager_name,
  185.        COUNT(e.employee_id)                   AS num_employees,
  186.        ROUND(AVG(m.salary))                   AS employee_avg_salary
  187. FROM employees e
  188.          JOIN employees m ON e.manager_id = m.employee_id
  189. GROUP BY e.manager_id
  190. ORDER BY num_employees DESC
  191. LIMIT 1;
  192.  
  193.  
  194. /* 26 Write a SQL query that outputs the names of the employees who have worked on the most projects.*/
  195. WITH project_counts AS (SELECT e.employee_id, e.first_name, e.last_name, COUNT(ep.project_id) AS projects_count
  196.                         FROM employees e
  197.                                  JOIN employees_projects ep ON e.employee_id = ep.employee_id
  198.                         GROUP BY e.employee_id)
  199. SELECT CONCAT(first_name, ' ', last_name) AS empoloyee_name, projects_count
  200. FROM project_counts
  201. WHERE projects_count = (SELECT MAX(projects_count) FROM project_counts)
  202. ORDER BY first_name;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement