w3resource

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 table: employees


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.

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)

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: 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%.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/postgresql-exercises/update-table/update-table-exercise-3.php