MySQL Update Table Statement Exercises: Increase the minimum and maximum salary of PU_CLERK by 2000 and salary for those employees by 20% and commission percent by .10
MySQL Update Table Statement: Exercise-9 with Solution
Write a MySQL statement to increase the minimum and maximum salary of PU_CLERK by 2000 as well as the salary for those employees by 20% and commission percent by .10.
Here is the sample table employees.
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | 100 | Steven | King | SKING | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-06-19 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 1987-06-23 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 1987-06-24 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 1987-06-25 | FI_MGR | 12000.00 | 0.00 | 101 | 100 | ......... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Here is the sample table jobs.
+------------+---------------------------------+------------+------------+ | JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY | +------------+---------------------------------+------------+------------+ | AD_PRES | President | 20000 | 40000 | | AD_VP | Administration Vice President | 15000 | 30000 | | AD_ASST | Administration Assistant | 3000 | 6000 | | FI_MGR | Finance Manager | 8200 | 16000 | | FI_ACCOUNT | Accountant | 4200 | 9000 | | AC_MGR | Accounting Manager | 8200 | 16000 | | AC_ACCOUNT | Public Accountant | 4200 | 9000 | | SA_MAN | Sales Manager | 10000 | 20000 | | SA_REP | Sales Representative | 6000 | 12000 | | PU_MAN | Purchasing Manager | 8000 | 15000 | ...... | PR_REP | Public Relations Representative | 4500 | 10500 | +------------+---------------------------------+------------+------------+
-- This SQL query updates the 'min_salary' and 'max_salary' columns in the 'jobs' table,
-- and the 'salary' and 'commission_pct' columns in the 'employees' table for a specific job.
UPDATE jobs, employees
-- Increase the 'min_salary' and 'max_salary' in the 'jobs' table by 2000,
-- and increase the 'salary' in the 'employees' table by 20%,
-- also increase the 'commission_pct' in the 'employees' table by 0.10.
SET jobs.min_salary = jobs.min_salary + 2000,
jobs.max_salary = jobs.max_salary + 2000,
employees.salary = employees.salary + (employees.salary * 0.20),
employees.commission_pct = employees.commission_pct + 0.10
-- Specify the conditions for updating rows:
-- 1. 'job_id' should be 'PU_CLERK' in both 'jobs' and 'employees' tables.
WHERE jobs.job_id = 'PU_CLERK'
AND employees.job_id = 'PU_CLERK';
Let execute the above code in MySQL command prompt
See the result before update. Only the effected rows have been displayed.
table - jobs +----------+------------------+------------+------------+ | JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY | +----------+------------------+------------+------------+ | PU_CLERK | Purchasing Clerk | 2500 | 5500 | +----------+------------------+------------+------------+ table - employees +-------------+------------+------------+----------+--------------+------------+----------+---------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+------------+------------+----------+--------------+------------+----------+---------+----------------+------------+---------------+ | 115 | Alexander | Khoo | AKHOO | 515.127.4562 | 1987-07-02 | PU_CLERK | 3100.00 | 0.00 | 114 | 30 | | 116 | Shelli | Baida | SBAIDA | 515.127.4563 | 1987-07-03 | PU_CLERK | 2900.00 | 0.00 | 114 | 30 | | 117 | Sigal | Tobias | STOBIAS | 515.127.4564 | 1987-07-04 | PU_CLERK | 2800.00 | 0.00 | 114 | 30 | | 118 | Guy | Himuro | GHIMURO | 515.127.4565 | 1987-07-05 | PU_CLERK | 2600.00 | 0.00 | 114 | 30 | | 119 | Karen | Colmenares | KCOLMENA | 515.127.4566 | 1987-07-06 | PU_CLERK | 2500.00 | 0.00 | 114 | 30 | +-------------+------------+------------+----------+--------------+------------+----------+---------+----------------+------------+---------------+
See the result. Only the effected rows have been displayed.
table - jobs +----------+------------------+------------+------------+ | JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY | +----------+------------------+------------+------------+ | PU_CLERK | Purchasing Clerk | 4500 | 7500 | +----------+------------------+------------+------------+ table - employees +-------------+------------+------------+----------+--------------+------------+----------+---------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+------------+------------+----------+--------------+------------+----------+---------+----------------+------------+---------------+ | 115 | Alexander | Khoo | AKHOO | 515.127.4562 | 1987-07-02 | PU_CLERK | 3720.00 | 0.10 | 114 | 30 | | 116 | Shelli | Baida | SBAIDA | 515.127.4563 | 1987-07-03 | PU_CLERK | 3480.00 | 0.10 | 114 | 30 | | 117 | Sigal | Tobias | STOBIAS | 515.127.4564 | 1987-07-04 | PU_CLERK | 3360.00 | 0.10 | 114 | 30 | | 118 | Guy | Himuro | GHIMURO | 515.127.4565 | 1987-07-05 | PU_CLERK | 3120.00 | 0.10 | 114 | 30 | | 119 | Karen | Colmenares | KCOLMENA | 515.127.4566 | 1987-07-06 | PU_CLERK | 3000.00 | 0.10 | 114 | 30 | +-------------+------------+------------+----------+--------------+------------+----------+---------+----------------+------------+---------------+
Explanation:
Here's a brief explanation of the above MySQL code:
- UPDATE jobs, employees: This part of the statement indicates that you want to update records in both the 'jobs' and 'employees' tables.
- SET jobs.min_salary = jobs.min_salary + 2000, jobs.max_salary = jobs.max_salary + 2000, employees.salary = employees.salary + (employees.salary * 0.20), employees.commission_pct = employees.commission_pct + 0.10: This sets the new values for the specified columns in both tables. It increases 'min_salary' and 'max_salary' in the 'jobs' table by 2000, increases 'salary' in the 'employees' table by 20%, and increases 'commission_pct' in the 'employees' table by 0.10.
- WHERE jobs.job_id = 'PU_CLERK' AND employees.job_id = 'PU_CLERK';: This specifies the condition for updating rows. It ensures that the update applies only to rows where the 'job_id' is 'PU_CLERK' in both the 'jobs' and 'employees' tables.
Go to:
PREV : Write a SQL statement to increase the salary of employees under the department 40, 90 and 110 according to the company rules that, salary will be increased by 25% for the department 40, 15% for department 90 and 10% for the department 110 and the rest of the departments will remain same.
NEXT : MySQL Alter Table
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
