PostgreSQL Update Table: Update the designation of employees for a specific number of employees using LIKE operator in the HR database

7. Write a SQL statement to change the job ID of the employee which ID is 118 to SH_CLERK if the employee belongs to a department which ID is 30 and the existing job ID does not start with SH.

Sample table: employees

Sample Solution:


-- This SQL statement updates the 'JOB_ID' column in the 'employees' table for an employee with employee_id=118 
-- and department_id=30 if their current job does not start with 'SH'. It sets their job to 'SH_CLERK'.

UPDATE employees 
WHERE employee_id = 118 
AND department_id = 30 


  • The UPDATE statement is used to modify existing records in a table.
  • employees is the name of the table being updated.
  • SET JOB_ID = 'SH_CLERK' specifies that the value of the 'JOB_ID' column should be set to 'SH_CLERK' for the rows that meet the specified condition.
  • WHERE employee_id = 118 AND department_id = 30 AND NOT JOB_ID LIKE 'SH%' restricts the update operation to only the row where the value of the 'employee_id' column is 118, the value of the 'department_id' column is 30, and the 'JOB_ID' column does not start with 'SH'. This ensures that only the employee with employee_id=118, department_id=30, and a job not starting with 'SH' will have their job updated to 'SH_CLERK'.


See the result. Only the effected rows have been displayed.

postgres=# SELECT * FROM  employees 
WHERE employee_id=118 
AND department_id=30 

 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
(1 row)

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

Previous: Write a SQL statement to change the salary of an employee to 8000 whose ID is 105, if the existing salary is less than 5000.
Next: Write a SQL statement to increase the salary of employees under the department 40, 90 and 110 according to the company rules that, the salary will be increased by 25% of the department 40, 15% for department 90 and 10% of the department 110 and the rest of the department will remain same.

What is the difficulty level of this exercise?

Follow us on Facebook and Twitter for latest update.