Advertisement
desislava_topuzakova

Basic CRUD - Exercise

Sep 19th, 2022
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. Basic CRUD - Exercise
  2. # 1. Find All Information About Departments
  3. SELECT * FROM `departments`
  4. ORDER BY `department_id`;
  5.  
  6. # 2. Find all Department Names
  7. SELECT `name` FROM `departments`
  8. ORDER BY `department_id`;
  9.  
  10. # 3. Find salary of Each Employee
  11. SELECT `first_name`, `last_name`, `salary` FROM `employees`
  12. ORDER BY `employee_id`;
  13.  
  14. # 4. Find Full Name of Each Employee
  15. SELECT `first_name`, `middle_name`, `last_name` FROM `employees`
  16. ORDER BY `employee_id`;
  17.  
  18. # 5. Find Email Address of Each Employee
  19. # email: {first name}.{last name}@softuni.bg
  20. SELECT CONCAT(`first_name`, '.', `last_name`, '@softuni.bg')
  21. AS `full_email_address`
  22. FROM `employees`;
  23.  
  24. # 6. Find All Different Employee's Salaries
  25. SELECT DISTINCT `salary` FROM `employees`;
  26.  
  27. # 7. Find all Information About Employees
  28. SELECT * FROM `employees`
  29. WHERE `job_title` = "Sales Representative"
  30. ORDER BY `employee_id`;
  31.  
  32. # 8. Find Names of All Employees by salary in Range
  33. SELECT `first_name`, `last_name`, `job_title` FROM `employees`
  34. WHERE `salary` BETWEEN 20000 AND 30000
  35. ORDER BY `employee_id`;
  36.  
  37. # 9. Find Names of All Employees
  38. # Full Name = "{first name} {middle name} {last name}"
  39. SELECT CONCAT(`first_name`, " ", `middle_name`, " ", `last_name`)
  40. AS `Full Name`
  41. FROM `employees`
  42. WHERE `salary` IN (25000, 14000, 12500, 23600);
  43. #WHERE `salary` = 25000 OR `salary` = 14000 OR `salary` = 12500 OR `salary` = 23600;
  44.  
  45. # 10. Find All Employees Without Manager
  46. SELECT `first_name`, `last_name` FROM `employees`
  47. WHERE `manager_id` IS NULL;
  48.  
  49. # 11. Find All Employees with salary More Than 50000
  50. SELECT `first_name`, `last_name`, `salary` FROM `employees`
  51. WHERE `salary` > 50000
  52. ORDER BY `salary` DESC;
  53.  
  54. # 12. Find 5 Best Paid Employees
  55. SELECT `first_name`, `last_name` FROM `employees`
  56. ORDER BY `salary` DESC
  57. LIMIT 5;
  58.  
  59. # 13. Find All Employees Except Marketing
  60. SELECT `first_name`, `last_name` FROM `employees`
  61. WHERE `department_id` != 4;
  62.  
  63. # 14. Sort Employees Table
  64. # 1. salary in decreasing order
  65. # 2. first name alphabetically (asc)
  66. # 3. then by last name descending
  67. # 4. Then by middle name alphabetically
  68. # 5. employee_id
  69. SELECT * FROM `employees`
  70. ORDER BY `salary` DESC, `first_name`, `last_name` DESC, `middle_name`, `employee_id`;
  71.  
  72. # 15. Create View Employees with Salaries
  73. CREATE VIEW `v_employees_salaries` AS
  74. SELECT `first_name`, `last_name`, `salary`
  75. FROM `employees`;
  76. SELECT * FROM `v_employees_salaries`;
  77.  
  78. # 16. Create View Employees with Job Titles
  79. CREATE VIEW `v_employees_job_titles` AS
  80. SELECT CONCAT_WS(' ', `first_name`, `middle_name`, `last_name`) AS `full_name`, `job_title`
  81. FROM `employees`;
  82.  
  83. # 17. Distinct Job Titles
  84. SELECT DISTINCT `job_title` FROM `employees`
  85. ORDER BY `job_title`;
  86.  
  87. # 18. Find First 10 Started Projects
  88. SELECT * FROM `projects`
  89. ORDER BY `start_date`, `name`, `project_id`
  90. LIMIT 10;
  91.  
  92. # 19. Last 7 Hired Employees
  93. SELECT `first_name`, `last_name`, `hire_date` from `employees`
  94. ORDER BY `hire_date` DESC
  95. LIMIT 7;
  96.  
  97. # 20. Increase Salaries
  98. UPDATE `employees`
  99. SET `salary` = `salary` * 1.12
  100. WHERE `department_id` IN (1, 2, 4, 11);
  101. SELECT `salary` FROM `employees`;
  102.  
  103. # 21. All Mountain Peaks
  104. SELECT `peak_name` FROM `peaks`
  105. ORDER BY `peak_name`;
  106.  
  107. # 22. Biggest Countries by Population
  108. SELECT `country_name`, `population` from `countries`
  109. WHERE `continent_code` = "EU"
  110. ORDER BY `population` DESC, `country_name`
  111. LIMIT 30;
  112.  
  113. # 23. Countries and Currency (Euro / Not Euro)
  114. SELECT `country_name`, `country_code`,
  115. IF (`currency_code` = "EUR", 'Euro', 'Not Euro') AS `currency`
  116. FROM `countries`
  117. ORDER BY `country_name`;
  118.  
  119. # 24. All Diablo Characters
  120. SELECT `name` FROM `characters`
  121. ORDER BY `name`;
  122.  
  123.  
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement