Advertisement
Georgi_Benchev

sql_in_class1

Dec 19th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 6.40 KB | None | 0 0
  1. /* 1 Write a SQL query that outputs all information about all departments.*/
  2. SELECT *
  3. FROM departments;
  4.  
  5.  
  6. /* 2 Write a SQL query that outputs all department names.*/
  7. SELECT name
  8. FROM departments;
  9.  
  10.  
  11. /* 3 Write a SQL query that outputs first and last name of each employee, along with their salary.*/
  12. SELECT first_name, last_name, salary
  13. FROM employees;
  14.  
  15.  
  16. /* 4 Write a SQL query that outputs the full name of each employee.*/
  17. SELECT CONCAT_WS(' ', first_name, middle_name, last_name)
  18. FROM employees;
  19.  
  20.  
  21. /* 5 Write a SQL query to generate an email addresses for each employee.
  22.    Consider that the email domain is company.com. For example,John Doe's email would be
  23.    "John.Doe@company.com". The produced column should be named "Full Email Addresses".*/
  24. SELECT CONCAT(first_name, '.', last_name, '@company.com') as 'full email addresses'
  25. FROM employees;
  26.  
  27.  
  28. /* 6 Write a SQL query to find all the different employee salaries.*/
  29. SELECT DISTINCT ROUND(salary) as salary
  30. FROM employees
  31. ORDER BY salary desc;
  32.  
  33.  
  34. /* 7 Write a SQL query that outputs all information about the employees
  35.    whose job title is "Sales Representative".*/
  36. SELECT *
  37. FROM employees
  38. WHERE job_title = 'Sales Representative';
  39.  
  40.  
  41. /* 8 Write a SQL query to find all employees who have a salary that is bigger than their manager's.*/
  42. SELECT CONCAT(e.first_name, ' ', e.last_name) as name, e.salary, m.salary AS manager_salary
  43. FROM employees e
  44.          JOIN employees m
  45.               ON e.manager_id = m.employee_id
  46. WHERE e.salary > m.salary;
  47.  
  48.  
  49. /* 9 Write a SQL query to find the names of all employees whose first name starts with "SA".*/
  50. SELECT first_name, last_name
  51. FROM employees
  52. WHERE first_name LIKE 'SA%'
  53. ORDER BY first_name;
  54.  
  55.  
  56. /* 10 Write a SQL query to find the names of all employees whose last name contains "ei".*/
  57. SELECT first_name, last_name
  58. FROM employees
  59. WHERE last_name LIKE '%ei%';
  60.  
  61.  
  62. /* 11 Write a SQL query to find all employees whose salary is in the range [20000…30000].*/
  63. SELECT CONCAT(first_name, ' ', last_name) as name, ROUND(salary) as salary
  64. FROM employees
  65. WHERE salary BETWEEN 20000 AND 30000
  66. ORDER BY salary;
  67.  
  68.  
  69. /* 12 Write a SQL query to find the names of all employees
  70.    whose salary is 25000, 14000, 12500 or 23600.*/
  71. SELECT first_name, last_name, ROUND(salary) as salary
  72. FROM employees
  73. WHERE salary in (25000, 14000, 12500, 23600)
  74. ORDER BY employees.salary;
  75.  
  76.  
  77. /* 13 Write a SQL query to find all employees that do not have manager.*/
  78. SELECT CONCAT(first_name, ' ', last_name) as employees_with_no_manager
  79. FROM employees
  80. WHERE manager_id IS NULL;
  81.  
  82.  
  83. /* 14 Write a SQL query to find the names of all employees who were hired before their managers.*/
  84. SELECT CONCAT(e1.first_name, ' ', e1.last_name) as employee
  85. FROM employees e1
  86. WHERE e1.hire_date < (SELECT e2.hire_date
  87.                       FROM employees e2
  88.                       WHERE e1.manager_id = e2.employee_id);
  89.  
  90.  
  91. /* 15 Write a SQL query to find all employees whose salary is more than 50000.
  92.    Order them in decreasing order, based on their salary.*/
  93. SELECT CONCAT(first_name, ' ', last_name) as name, ROUND(salary) as salary
  94. FROM employees
  95. WHERE salary > 50000
  96. ORDER BY salary desc;
  97.  
  98.  
  99. /* 16 Write a SQL query to find the top 5 best paid employees.*/
  100. SELECT CONCAT(first_name, ' ', last_name) as name, ROUND(salary) as salary
  101. FROM employees
  102. ORDER BY salary desc
  103. LIMIT 5;
  104.  
  105.  
  106. /* 17 Write a SQL query that outputs all employees along their address.*/
  107. SELECT CONCAT(e.first_name, ' ', e.last_name) as employee_name, t.name as town, a.text as address
  108. FROM employees e
  109.          JOIN addresses a
  110.               ON e.address_id = a.address_id
  111.          JOIN towns t ON a.town_id = t.town_id
  112. ORDER BY t.name, e.first_name;
  113.  
  114.  
  115. /* 18 Write a SQL query to find all employees whose middle name is the same
  116.    as the first letter of their town.*/
  117. SELECT CONCAT_WS(' ', e.first_name, middle_name, last_name) as full_name, t.name
  118. FROM employees e
  119.          JOIN addresses a ON e.address_id = a.address_id
  120.          JOIN towns t ON a.town_id = t.town_id
  121. WHERE LEFT(t.name, 1) = e.middle_name;
  122. /* ------------------------------------------------------------------------------------- */
  123. /* 18 Write a SQL query to find all employees whose !FIRST! middle name !LETTER! is the same
  124.    as the first letter of their town.*/
  125. /*
  126. SELECT CONCAT_WS(' ', e.first_name, middle_name, last_name) as full_name, t.name
  127. FROM employees e
  128.          JOIN addresses a ON e.address_id = a.address_id
  129.          JOIN towns t ON a.town_id = t.town_id
  130. WHERE LEFT(t.name, 1) = left(e.middle_name, 1); */
  131.  
  132.  
  133. /* 19 Write a SQL query that outputs all employees (first and last name) that have a manager,
  134.    along with their manager (first and last name).*/
  135. SELECT CONCAT(e.first_name, ' ', e.last_name) as employee, CONCAT(m.first_name, ' ', m.last_name) as manager
  136. FROM employees e
  137.          JOIN employees m ON e.manager_id = m.employee_id
  138. WHERE e.manager_id IS NOT NULL;
  139.  
  140.  
  141. /* 20 Write a SQL query that outputs all employees that have a manager (first and last name),
  142.    along with their manager (first and last name) and the employee's address.*/
  143. SELECT CONCAT(e.first_name, ' ', e.last_name) as employee,
  144.        CONCAT(m.first_name, ' ', m.last_name) as manager,
  145.        a.text                                 as address
  146. FROM employees e
  147.          JOIN employees m ON e.manager_id = m.employee_id
  148.          JOIN addresses a ON e.address_id = a.address_id;
  149.  
  150.  
  151. /* 21 Write a SQL query to find all departments and all town names in a single column.*/
  152. SELECT d.name as department_or_town_name
  153. FROM departments d
  154. UNION
  155. SELECT t.name as department_or_town_name
  156. FROM towns t;
  157.  
  158.  
  159. /* 22 Write a SQL query to find all employees and their manager,
  160.    along with the employees that do not have manager.
  161.    If they do not have a manager, output "n/a".*/
  162. SELECT CONCAT(e.first_name, ' ', e.last_name)                        as employee,
  163.        CONCAT(IFNULL(CONCAT(m.first_name, ' ', m.last_name), 'n/a')) as manager
  164. FROM employees e
  165.          LEFT JOIN employees m ON e.manager_id = m.employee_id;
  166.  
  167.  
  168. /* 23 Write a SQL query to find the names of all employees from the departments
  169.    "Sales" AND "Finance" whose hire year is between 1995 and 2005.*/
  170. SELECT CONCAT(e.first_name, ' ', e.last_name) as employee, d.name as department
  171. FROM employees e
  172.          JOIN departments d ON e.department_id = d.department_id
  173. WHERE d.name = 'Finance'
  174.    OR d.name = 'Sales'
  175.     AND YEAR(e.hire_date) BETWEEN 1995 AND 2005;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement