PostgreSQL Update Table: Change the value of more than one column with user defined content
3. Write a SQL statement to change the email and commission_pct column of the employees table with 'not available' and 0.10 for those employees whose department_id is 110.
Sample Solution:
Code:
-- This SQL statement updates the 'email' and 'commission_pct' columns in the 'employees' table
-- for employees belonging to the department with department_id=110.
-- It sets the 'email' column to 'not available' and the 'commission_pct' column to 0.10 for matching rows.
UPDATE employees
SET email='not available', commission_pct=0.10
WHERE department_id=110;
Explanation:
- The UPDATE statement is used to modify existing records in a table.
- employees is the name of the table being updated.
- SET email='not available', commission_pct=0.10 specifies that the value of the 'email' column should be set to 'not available', and the value of the 'commission_pct' column should be set to 0.10 for the rows that meet the specified condition.
- WHERE department_id=110 restricts the update operation to only those rows where the value of the 'department_id' column is 110. This ensures that only employees belonging to the department with department_id=110 will have their email addresses updated to 'not available' and their commission percentage set to 0.10.
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 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Output:
See the result. Only two rows have been updated.
postgres=# SELECT *
postgres-# FROM employees
postgres-# WHERE department_id=110
postgres-# AND email='not available';
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)
Go to:
PREV : Write a SQL statement to change the email and commission_pct column of the employees table with 'not available' and 0.10 for all employees.
NEXT : 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%.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
