Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Remove least paid employee who are reporting to BL

Author  Topic 

ashwanth
Starting Member

1 Post

Posted - 2014-04-06 : 08:44:42
Remove least paid employee who are reporting to BLAKE ?
my solution
delete emp where sal = (select min(sal) from emp where mgr =
(select empno from emp where ename = 'BLAKE')
the correct solution is
delete emp where sal = (select min(sal) from emp where mgr =
(select empno from emp where ename = 'BLAKE')) and
ename in(select ename from emp where mgr =
(select empno from emp where ename = 'BLAKE'))
here the question why my solution is wrong
1)find out who is the least paid employee who are reporting to BLAKE
(select min(sal) from emp where mgr =
(select empno from emp where ename = 'BLAKE')
2)Remove least paid employee who are reporting to BLAKE ?
my solution
so ,
delete emp where sal = (select min(sal) from emp where mgr =
(select empno from emp where ename = 'BLAKE')
--1 row deleted
delete emp where sal = (select min(sal) from emp where mgr =
(select empno from emp where ename = 'BLAKE')) and
ename in(select ename from emp where mgr =
(select empno from emp where ename = 'BLAKE'))
----1 row deleted

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2014-04-19 : 12:37:08
You would be deleting all employees that have the same salary as the min salary of those reporting to Blake. So if they have that salary then they would be deleted whether or not the report to Blake.

Your solution happens to work because there is only one employee with that salary.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -