PostgreSQL Update Table: Change the value of more than one column with user defined content
2. 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.
Sample Solution:
Code:
-- This SQL statement updates the 'email' and 'commission_pct' columns in the 'employees' table,
-- setting the 'email' column to 'not available' and the 'commission_pct' column to 0.10 for all rows.
UPDATE employees SET email='not available', commission_pct=0.10;
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 for all rows should be set to 'not available', and the value of the 'commission_pct' column should be set to 0.10. This effectively updates the email addresses to 'not available' and sets the commission percentage to 0.10 for all employees in the table.
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 displayed.
postgres=# SELECT * FROM employees LIMIT 2;
employee_id | first_name | last_name | email | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id
-------------+------------+-----------+---------------+--------------+------------+----------+---------+----------------+------------+---------------
128 | Steven | Markle | not available | 650.124.1434 | 1987-07-15 | ST_CLERK | 2200.00 | 0.10 | 120 | 50
129 | Laura | Bissot | not available | 650.124.5234 | 1987-07-16 | ST_CLERK | 3300.00 | 0.10 | 121 | 50
Go to:
PREV : Write a SQL statement to change the email column of the employees table with 'not available' for all employees.
NEXT : 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.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
