Update Query is used for updating existing rows(records) in a table. In last few tutorials we have seen how to insert data in table using INSERT query and how to fetch the data using SELECT Query and Where clause. What if we want to update an exiting record? this is where update query comes into picture. Using this we can update any number of rows in a table.
Syntax
UPDATE TableName SET column_name1 = value, column_name2 = value.... WHERE condition;
Query would update only those rows that satisfy the condition defined in where clause.
Example
EMPLOYEES table:
+------+----------+---------+----------+ |SSN | EMP_NAME | EMP_AGE |EMP_SALARY| +------+----------+---------+----------+ | 101 | Steve | 23 | 9000.00 | | 223 | Peter | 24 | 2550.00 | | 388 | Shubham | 19 | 2444.00 | | 499 | Chaitanya| 29 | 6588.00 | | 589 | Apoorv | 21 | 1400.00 | | 689 | Rajat | 24 | 8900.00 | | 700 | Ajeet | 20 | 18300.00 | +------+----------+---------+----------+
Update the salary of employees to 10000 if they are having age greater than 25.
SQL> UPDATE EMPLOYEES SET EMP_SALARY = 10000 WHERE EMP_AGE > 25;
Updated EMPLOYEES table would look like this:
+------+----------+---------+----------+ |SSN | EMP_NAME | EMP_AGE |EMP_SALARY| +------+----------+---------+----------+ | 101 | Steve | 23 | 9000.00 | | 223 | Peter | 24 | 2550.00 | | 388 | Shubham | 19 | 2444.00 | | 499 | Chaitanya| 29 | 10000.00 | | 589 | Apoorv | 21 | 1400.00 | | 689 | Rajat | 24 | 8900.00 | | 700 | Ajeet | 20 | 18300.00 | +------+----------+---------+----------+
As you can see that only one employee is there in table above the age of 25. The salary for the employee got updated to 10000.
Update the salary of employee “Apoorv” to 120000.
SQL> UPDATE EMPLOYEES SET EMP_SALARY = 120000 WHERE EMP_NAME = 'Apoorv';
Output:
+------+----------+---------+----------+ |SSN | EMP_NAME | EMP_AGE |EMP_SALARY| +------+----------+---------+----------+ | 101 | Steve | 23 | 9000.00 | | 223 | Peter | 24 | 2550.00 | | 388 | Shubham | 19 | 2444.00 | | 499 | Chaitanya| 29 | 10000.00 | | 589 | Apoorv | 21 | 12000.00 | | 689 | Rajat | 24 | 8900.00 | | 700 | Ajeet | 20 | 18300.00 | +------+----------+---------+----------+
Leave a Reply