I was solving one of the puzzles and came across swapping column values using DML queries:
SELECT * FROM TEMP_TABLE;
ID1, ID2
--------
20, 15
20, 15
20, 15
There's no need to have three update statements, one is sufficient:
UPDATE temp_table_new
SET id1 = id2,
id2 = id1;
select * from employ;
EMPNO FNAME LNAME
----- ---------- ----------
1001 kiran kumar
1002 santosh reddy
update employ e set fname=(select lname from employ where empno=e.empno),
lname=(select fname from employ where empno=e.empno);
select * from employ;
EMPNO FNAME LNAME
------ ---------- ----------
1001 kumar kiran
1002 reddy santosh
CREATE TABLE Names
(
F_NAME VARCHAR(22),
L_NAME VARCHAR(22)
);
INSERT INTO Names VALUES('Ashutosh', 'Singh'),('Anshuman','Singh'),('Manu', 'Singh');
UPDATE Names N1 , Names N2 SET N1.F_NAME = N2.L_NAME , N1.L_NAME = N2.F_NAME
WHERE N1.F_NAME = N2.F_NAME;
SELECT * FROM Names;