January 28, 2013 at 1:26 pm
PLEASE HELP ME WHO EVER CAN WRITE MORE EFFICIENT T-SQL THAT ANSWERS THE QUESTION.
THIS IS WHEN WE DONT KNOW THE SALARY INFORMATION AND WE ONLY CARE THE 10TH AND 11TH EMPLOYEES
ITS FROM JOB INTERVIEW, WHICH I SIAD ! I DONT KNOW!
January 28, 2013 at 1:36 pm
How about thinking you need the first 11 but you need to discard the first 9. How do you discard rows? EXCEPT, NOT IN, NOT EXISTS, OUTER JOIN?
January 28, 2013 at 1:37 pm
Using the Rank function would be very helpfull for this., possibly a cte.
;WITH Salaries AS (SELECT
EmpID,
EmpLastName,
EmpFirstName,
Salary
RANK() OVER (PARTITION BY EmpID, ORDER BY Salary))
FROM dbo.Employee)AS SalaryRank
SELECT *
FROM Salaries
WHERE (SalaryRank = 10
OR SalaryRank=11)
January 28, 2013 at 1:49 pm
thanks. i will blug in and see if that answers the question
January 28, 2013 at 1:58 pm
You may get an error, misplaced parens and the sort is wrong.
January 28, 2013 at 2:00 pm
you can also do it SQL2000 style(before the wonderful row_number/ranking functions came out) , using nested TOP statements to get the top 11, and then the top 2 of that in the opposite order.
Lowell
January 28, 2013 at 3:24 pm
this should work too...
declare @t table(empname varchar(32),sal money)
insert into @t
select 'patrick',1000
union
select 'john',12000
union
select 'peter',500
union
select 'robert',360
union
select 'steve',810
union
select 'edward',3000
union
select 'sean',1200
union
select 'ricky',500
select * from(select empname,ROW_NUMBER() over (order by sal desc) as position from @t)a where position between 5 and 6
January 28, 2013 at 10:14 pm
Hoping this is late enough not to be useful with your interview but soon enough to help you learn something:
WITH Salaries AS (
SELECT
EmpID,
EmpLastName,
EmpFirstName,
Salary,
rn = ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY Salary DESC)
FROM
dbo.Employee)
SELECT
*
FROM
Salaries
WHERE
rn between 10 and 11;
-- or, without windowing functions ala SQL Server 2000:
select top 2
*
from
(select top 11
EmpID,
EmpLastName,
EmpFirstName,
Salary
from
dbo.Employee
order by
Salary DESC) dt
order by
dt.Salary ASC;
January 29, 2013 at 12:47 am
Lynn Pettis (1/28/2013)
Hoping this is late enough not to be useful with your interview but soon enough to help you learn something
Why does this bring to mind the vision of an interviewee tapping away at an iPad during the interview, posting a question to SSC, in the hopes that it will be answered by the time the interviewer blinks twice?
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply