MySQL Update Table
MySQL Update Table
ANUDIP FOUNDATION
LESSON-1 Java Enterprise Edition
1. Write a SQL statement to change the email column of employees table with 'not available' for all
employees.
1
LESSON-1 Java Enterprise Edition
2
LESSON-1 Java Enterprise Edition
3
LESSON-1 Java Enterprise Edition
4
LESSON-1 Java Enterprise Edition
5
LESSON-1 Java Enterprise Edition
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+---
-------------+------------+---------------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID
| SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID |
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+---
-------------+------------+---------------+
| 100 | Steven | King | not available | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 |
0.00 | 0| 90 |
| 101 | Neena | Kochhar | not available | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 |
0.00 | 100 | 90 |
+-------------+------------+-----------+---------------+--------------+------------+---------+----------+---
-------------+------------+---------------
2. Write a SQL statement to change the email and commission_pct column of employees table with 'not
available' and 0.10 for all employees.
6
LESSON-1 Java Enterprise Edition
7
LESSON-1 Java Enterprise Edition
8
LESSON-1 Java Enterprise Edition
9
LESSON-1 Java Enterprise Edition
10
LESSON-1 Java Enterprise Edition
commission_pct=0.10;
Copy
Let execute the above code in MySQL 5.6 command prompt
11
LESSON-1 Java Enterprise Edition
12
LESSON-1 Java Enterprise Edition
13
LESSON-1 Java Enterprise Edition
14
LESSON-1 Java Enterprise Edition
15
LESSON-1 Java Enterprise Edition
UPDATE employees
commission_pct=0.10
WHERE department_id=110;
Copy
Let execute the above code in MySQL 5.6 command prompt
+-------------+------------+-----------+---------------+--------------+------------+------------+----------
+----------------+------------+---------------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID
| SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID |
+-------------+------------+-----------+---------------+--------------+------------+------------+----------
+----------------+------------+---------------+
| 205 | Shelley | Higgins | not available | 515.123.8080 | 1987-09-30 | AC_MGR |
12000.00 | 0.10 | 101 | 110 |
| 206 | William | Gietz | not available | 515.123.8181 | 1987-10-01 | AC_ACCOUNT |
8300.00 | 0.10 | 205 | 110 |
+-------------+------------+-----------+---------------+--------------+------------+------------+----------
+----------------+------------+---------------+
2 rows in set (0.00 sec)
16
LESSON-1 Java Enterprise Edition
4. Write a SQL statement to change the email column of employees table with 'not available' for those
employees whose department_id is 80 and gets a commission is less than .20%
17
LESSON-1 Java Enterprise Edition
18
LESSON-1 Java Enterprise Edition
19
LESSON-1 Java Enterprise Edition
20
LESSON-1 Java Enterprise Edition
UPDATE employees
Copy
Let execute the above code in MySQL 5.6 command prompt
21
LESSON-1 Java Enterprise Edition
See the result. Only the effected rows have been displayed.
+-------------+------------+-----------+---------------+--------------------+------------+--------+---------
+----------------+------------+---------------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE |
JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID |
+-------------+------------+-----------+---------------+--------------------+------------+--------+---------
+----------------+------------+---------------+
| 155 | Oliver | Tuvault | not available | 011.44.1344.486508 | 1987-08-11 | SA_REP |
7000.00 | 0.15 | 145 | 80 |
| 163 | Danielle | Greene | not available | 011.44.1346.229268 | 1987-08-19 | SA_REP |
9500.00 | 0.15 | 147 | 80 |
| 164 | Mattea | Marvins | not available | 011.44.1346.329268 | 1987-08-20 | SA_REP |
7200.00 | 0.10 | 147 | 80 |
| 165 | David | Lee | not available | 011.44.1346.529268 | 1987-08-21 | SA_REP |
6800.00 | 0.10 | 147 | 80 |
| 166 | Sundar | Ande | not available | 011.44.1346.629268 | 1987-08-22 | SA_REP |
6400.00 | 0.10 | 147 | 80 |
| 167 | Amit | Banda | not available | 011.44.1346.729268 | 1987-08-23 | SA_REP |
6200.00 | 0.10 | 147 | 80 |
| 171 | William | Smith | not available | 011.44.1343.629268 | 1987-08-27 | SA_REP |
7400.00 | 0.15 | 148 | 80 |
| 172 | Elizabeth | Bates | not available | 011.44.1343.529268 | 1987-08-28 | SA_REP |
7300.00 | 0.15 | 148 | 80 |
| 173 | Sundita | Kumar | not available | 011.44.1343.329268 | 1987-08-29 | SA_REP |
6100.00 | 0.10 | 148 | 80 |
| 179 | Charles | Johnson | not available | 011.44.1644.429262 | 1987-09-04 | SA_REP |
6200.00 | 0.10 | 149 | 80 |
+-------------+------------+-----------+---------------+--------------------+------------+--------+---------
+----------------+------------+---------------+
5. Write a SQL statement to change the email column of employees table with 'not available' for those
employees who belongs to the 'Accouning' department.
+-------------+------------+-----------+---------------+--------------+------------+------------+----------
+----------------+------------+---------------+
Here is the sample table departments.
+---------------+----------------------+------------+-------------+
| DEPARTMENT_ID | DEPARTMENT_NAME | MANAGER_ID | LOCATION_ID |
+---------------+----------------------+------------+-------------+
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 30 | Purchasing | 114 | 1700 |
| 40 | Human Resources | 203 | 2400 |
| 50 | Shipping | 121 | 1500 |
| 60 | IT | 103 | 1400 |
| 70 | Public Relations | 204 | 2700 |
22
LESSON-1 Java Enterprise Edition
See the result. Only the effected rows have been displayed.
6. Write a SQL statement to change salary of employee to 8000 whose ID is 105, if the existing salary
is less than 5000.
+-------------+------------+-----------+---------------+--------------+------------+------------+----------
+----------------+------------+---------------+
UPDATE employees SET SALARY = 8000 WHERE employee_id = 105 AND salary < 5000;
Copy
Let execute the above code in MySQL 5.6 command prompt
See the result. Only the effected rows have been displayed.
+-------------+------------+-----------+---------+--------------+------------+---------+---------+----------
------+------------+---------------+
23
LESSON-1 Java Enterprise Edition
7. Write a SQL statement to change job ID of employee which ID is 118, to SH_CLERK if the employee
belongs to department, which ID is 30 and the existing job ID does not start with SH.
+-------------+------------+-----------+---------------+--------------+------------+------------+----------
+----------------+------------+---------------+
WHERE employee_id=118
AND department_id=30
Copy
Let execute the above code in MySQL 5.6 command prompt
See the result. Only the effected rows have been displayed.
+-------------+------------+-----------+---------+--------------+------------+----------+---------+---------
-------+------------+---------------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID |
SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID |
+-------------+------------+-----------+---------+--------------+------------+----------+---------+---------
-------+------------+---------------+
| 118 | Guy | Himuro | GHIMURO | 515.127.4565 | 1987-07-05 | SH_CLERK | 2600.00 |
0.00 | 114 | 30 |
+-------------+------------+-----------+---------+--------------+------------+----------+---------+---------
-------+------------+---------------+
8. 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.
24
LESSON-1 Java Enterprise Edition
+-------------+------------+-----------+---------------+--------------+------------+------------+----------
+----------------+------------+---------------+
ELSE salary
END
Copy
Let execute the above code in MySQL 5.6 command prompt
See the result before update. Only the effected rows have been displayed.
+-------------+------------+-----------+----------+--------------+------------+------------+----------+-----
-----------+------------+---------------+
| 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 |
| 203 | Susan | Mavris | SMAVRIS | 515.123.7777 | 1987-09-28 | HR_REP | 6500.00 |
0.00 | 101 | 40 |
| 205 | Shelley | Higgins | SHIGGINS | 515.123.8080 | 1987-09-30 | AC_MGR | 12000.00 |
0.00 | 101 | 110 |
| 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 |
0.00 | 205 | 110 |
+-------------+------------+-----------+----------+--------------+------------+------------+----------+-----
-----------+------------+---------------+
9. Write a SQL 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 jobs.
25
LESSON-1 Java Enterprise Edition
+------------+---------------------------------+------------+------------+
| 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 |
| PU_CLERK | Purchasing Clerk | 2500 | 5500 |
| ST_MAN | Stock Manager | 5500 | 8500 |
| ST_CLERK | Stock Clerk | 2000 | 5000 |
| SH_CLERK | Shipping Clerk | 2500 | 5500 |
| IT_PROG | Programmer | 4000 | 10000 |
| MK_MAN | Marketing Manager | 9000 | 15000 |
| MK_REP | Marketing Representative | 4000 | 9000 |
| HR_REP | Human Resources Representative | 4000 | 9000 |
| PR_REP | Public Relations Representative | 4500 | 10500 |
+------------+---------------------------------+------------+------------+
UPDATE jobs,employees
SET jobs.min_salary=jobs.min_salary+2000,
jobs.max_salary=jobs.max_salary+2000,
employees.salary=employees.salary+(employees.salary*.20),
employees.commission_pct=employees.commission_pct+.10
WHERE jobs.job_id='PU_CLERK'
AND employees.job_id='PU_CLERK';
Copy
Let execute the above code in MySQL 5.6 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
26
LESSON-1 Java Enterprise Edition
+-------------+------------+------------+----------+--------------+------------+----------+---------+-------
---------+------------+---------------+
| 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 |
+-------------+------------+------------+----------+--------------+------------+----------+---------+-------
---------+------------+---------------+
27
LESSON-1 Java Enterprise Edition
28