SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
Syntax:-
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
SET column1 = value1, column2 = value2, ...
WHERE condition;
Note:- Be careful when updating records in a table! Notice the WHERE clause in the
UPDATE statement.
The WHERE clause specifies which record(s) that should be updated. If
you omit the WHERE clause, all records in the table will be updated!
SQL UPDATE Statement With SQL WHERE Clause
| 1. | W.A.Q to increase employee salary 500 ,who are working in 10th department. |
| => | update tblemp set salary=salary+500 where deptno = 10 |
| 2. | W.A.Q to increase employee salary 500 ,who are not getting commision |
| => | update tblemp set salary=salary+500 where comm is null |
| 3. | W.A.Q to change employee salary 1500 whose salary is less then 1000 |
| => | update tblemp set salary=1500 where salary < 1000 |
| 4. | W.A.Q to increase employee salary 200 , who are working at 10 & 20 department |
| => | update tblemp set salary = salary+200 where deptno = 10 or deptno =20 or update tblemp set salary = salary+200 where deptno in(10,20) |
| 5. | W.A.Q to change employee designation as analyst who are worki as clerk |
| => | update tblemp set job = "ANALYST" where job = "CLERK" |
| 6. | W.A.Q to increase employee salary =20000 whose name start's with 'A' |
| => | update tblemp set salary =salary +20000 ename like 'A%' |
| 7. | W.A.Q to change employee designation as president whose having more experience |
| => | update tblemp set job = "PRESIDENT" where (sysdate-hiredate)=(select max(sysdate-hiredate) from tblemp) |
| 8. | W.A.Q to increase employee salary 3000 who are join in the year of 87 |
| => | update tblemp set salary=salary+3000 where hiredate like '%87' |
Ans. 7 works on oracal but in sql server the answer is
ReplyDeleteupdate tblemp set job = 'PRESIDENT' where (DATEDIFF(YEAR,doj,cast(GETDATE()as date)))=(SELECT max(DATEDIFF(YEAR,doj,cast(GETDATE()as date))) AS DiffDate from emp )