November 15, 2010 at 1:05 pm
Hi,
Can I get the 10th highest salary without using TOP or RowNumber() over partition?
Thanks [/font]
November 15, 2010 at 1:09 pm
You could do something stupid like:
select max(Salary)
from MyTable
where Salary <
(select max(Salary)
from MyTable
where Salary <
(select max(Salary)
from MyTable
where ...
Keep going nine levels deep on the sub-queries and you'll get the 10th highest. You could use that as a sub-query with an equality test and you'd get all the ties for 10th highest.
However, it's a really dumb idea. I'd use Top or Rank/Dense Rank/Row Number. That's what they're for.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
November 15, 2010 at 1:11 pm
Oh, and if this is for homework, you could also potentially use a loophole in the exact wording of the question, and build a cursor with an Order By in the query, and step through till you find the 10th time the salary value goes down (to account for ties). Would be another messy, but "legal" way to do this.
Again, like the nested sub-queries, I'd never use it in a production system.
Is this homework? That kind of requirement makes it sound like it.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
November 15, 2010 at 1:34 pm
What do you want to do if multiple people have the same salary?
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 15, 2010 at 2:44 pm
thanks ,,,,,,,no it's not a home work
its again an interview question but I got an answer already...
this can be done using a corelated query
SELECT DISTINCT (a.age) FROM test55 A WHERE 10=
(SELECT COUNT (DISTINCT (b.age)) FROM test55 B WHERE a.age<=b.age)
Thanks [/font]
November 15, 2010 at 2:47 pm
Learner1 (11/15/2010)
thanks ,,,,,,,no it's not a home workits again an interview quetion but I got an answer already...
this can be done using a corelated query
SELECT DISTINCT (a.age) FROM test55 A WHERE 2=
(SELECT COUNT (DISTINCT (b.age)) FROM test55 B WHERE a.age<=b.age)
Good luck on that interview; with an answer like that, you're going to need it.
November 15, 2010 at 2:50 pm
Learner1 (11/15/2010)
thanks ,,,,,,,no it's not a home workits again an interview question but I got an answer already...
this can be done using a corelated query
SELECT DISTINCT (a.age) FROM test55 A WHERE 10=
(SELECT COUNT (DISTINCT (b.age)) FROM test55 B WHERE a.age<=b.age)
You're getting a unique count of ages where the count of distinct ages is... :crazy: Wut?
I wish you luck. Please note I will be available for remote consulting in early January after the holidays when you find you need more support.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
November 15, 2010 at 2:58 pm
Here is the complete code with inserts... to find 3rd highest salary
try it..
CREATE TABLE [SAL](
[name] [varchar](12) NULL,
[salary] [int] NULL
)
select * from SAL
INSERT INTO sal ( name,salary) VALUES ( 'LARY', 10)
INSERT INTO sal ( name,salary) VALUES ( 'TIA', 20)
INSERT INTO sal ( name,salary) VALUES ( 'GARY', 30)
INSERT INTO sal ( name,salary) VALUES ( 'ANNROSE', 40)
INSERT INTO sal ( name,salary) VALUES ( 'RAMA', 50)
INSERT INTO sal ( name,salary) VALUES ( 'TAMY', 60)
INSERT INTO sal ( name,salary) VALUES ( 'RAGH', 70)
SELECT DISTINCT (a.salary) FROM SAL A WHERE 3=
(SELECT COUNT (DISTINCT (b.salary)) FROM SAL B WHERE a.salary<=b.salary)
Thanks [/font]
November 15, 2010 at 3:08 pm
@Craig: I like your reply regarding your availability 😀
@Learner1: insert the data into a table with an IDENTITY column and do a SELECT WHERE ID= 3 😛
November 15, 2010 at 3:13 pm
HAHAHA
very funny
Thanks [/font]
November 15, 2010 at 3:49 pm
Learner1 (11/15/2010)
HAHAHAvery funny
What's so funny?
Based on the "rules" you prvided, the IDENTITY approach is valid.
November 15, 2010 at 3:56 pm
LutzM (11/15/2010)
Learner1 (11/15/2010)
HAHAHAvery funny
What's so funny?
Based on the "rules" you prvided, the IDENTITY approach is valid.
I believe that was aimed at me, Lutz, not you. 😀
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
November 15, 2010 at 9:56 pm
Learner1 (11/15/2010)
Hi,Can I get the 10th highest salary without using TOP or RowNumber() over partition?
Since you specifically mentioned not to use RowNumber() over partition I am getting the result just by using ROW_NUMBER() 😛
CREATE TABLE [SAL](
[name] [varchar](12) NULL,
[salary] [int] NULL
)
INSERT INTO sal ( name,salary) VALUES ( 'LARY', 10)
INSERT INTO sal ( name,salary) VALUES ( 'TIA', 20)
INSERT INTO sal ( name,salary) VALUES ( 'GARY', 30)
INSERT INTO sal ( name,salary) VALUES ( 'ANNROSE', 40)
INSERT INTO sal ( name,salary) VALUES ( 'RAMA', 50)
INSERT INTO sal ( name,salary) VALUES ( 'TAMY', 60)
INSERT INTO sal ( name,salary) VALUES ( 'RAGH', 70)
--Your Query
SELECT DISTINCT (a.salary) FROM SAL A WHERE 3=
(SELECT COUNT (DISTINCT (b.salary)) FROM SAL B WHERE a.salary<=b.salary)
--My Query
select salary from
(
select *,ROW_NUMBER()over(order by salary desc)rid from SAL
) t where rid=3
drop table SAL
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
November 16, 2010 at 1:38 am
Learner1 (11/15/2010)
Hi,Can I get the 10th highest salary without using TOP or RowNumber() over partition?
Since this is a stupid nonsensical restriction 😉 , heres mine
Use denali
select * from #SAL
ORDER BY salary ASC
OFFSET 2 ROWS
FETCH NEXT 1 ROWS ONLY
November 16, 2010 at 5:10 am
Dave Ballantyne (11/16/2010)
Learner1 (11/15/2010)
Hi,Can I get the 10th highest salary without using TOP or RowNumber() over partition?
Since this is a stupid nonsensical restriction 😉 , heres mine
Use denali
select * from #SAL
ORDER BY salary ASC
OFFSET 2 ROWS
FETCH NEXT 1 ROWS ONLY
Hi Dave,
as far as I'm concerned, your answer is the best approach so far, since you're using the latest technology. Wait, it's not the latest, it's the next! Even better then 😀
As a replacement of my IDENTITY approach, we could also make use of the SEQUENCE table...
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply