Author |
Topic |
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-09-24 : 09:29:51
|
SELECT * FROM Salary-------- -----------A1 100A2 200A3 300A4 400A5 500i need to write a query to select salary < salary of "A3" without using subqueries |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-09-24 : 10:09:49
|
Why not using subqueries? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2010-09-24 : 10:21:18
|
declare @temp as floatselect @temp = salary where empid='A1'select * from salary where salary<@temp |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-09-24 : 10:21:57
|
Here's one way. -- Asuming that the first column is a keyDECLARE @salVal MONEYSELECT @salVal = [The2ndColumn] FROM [TheTable] WHERE [TheFirstColumn] = 'A3'SELECT * FROM Salary WHERE [The2ndColumn] < @salVal Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-09-24 : 12:45:29
|
Hi all, Thank you very very much for your replies..Actually there is no specific reason for not using subquery for above query but i am not able to do that without subquery.. and finally i tried with joins and did that..Please see below solution.. please correct me if anything wrong..create table #tmp (names varchar(50), salary int)insert into #tmpselect 'A1', 100union allselect 'A2', 200union allselect 'A3', 300union allselect 'A4', 400union allselect 'A5', 500select * from #tmpselect t3.*from #tmp t1 left join (select names, salary from #tmp where names = 'A3') t2 on t1.names = t2.names left join #tmp t3 on t3.salary < t2.salarywhere t3.names is not nulldrop table #tmpThanks.. |
 |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2010-09-24 : 13:02:11
|
If I were you, I'd use Transact Charlie's sugestion, it will likely have better performance. Check the execution plans though, you never know for certain unless you try. Also make sure you index the salary column. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-09-24 : 13:59:20
|
yes.. i agree with your opinion.. |
 |
|
X002548
Not Just a Number
15586 Posts |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-09-25 : 02:03:21
|
I want in result set that.. i have name of customer..i want list of customer who has less salary then passed customer's salary..so for example if i pass A3 then A3 has 300 salary say for example..then i want result set which includes salary less then 300 without subquery. |
 |
|
|