SQL Query to find Nth highest salary [duplicate] - sql

This question already has answers here:
How to fetch the nth highest salary from a table without using TOP and sub-query?
(18 answers)
SQL query to find Nth highest salary from a salary table
(11 answers)
Closed 9 years ago.
How to find Nth Highest Salary without using any subquery in MS SQL?

;WITH cte1
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY SALARY DESC) AS RN, * FROM Salaries
)
SELECT *
FROM cte1
WHERE RN = 5 <-- Nth highest

Check out the row_number function. :)
http://msdn.microsoft.com/en-us/library/ms186734.aspx

Related

SHOW TOP 1 in ORACLE [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 2 months ago.
Here's the query
SELECT concat(FIRSTNAME,LASTNAME) AS NAME, HIREDATE from EMPLOYEE ORDER BY HIREDATE
WHERE ROWNUM = 1;
I've been trying to use rownum function but I keep getting the error - "ORA-00933: SQL command not properly ended".
You can try like this:
SELECT *
FROM (SELECT concat(FIRSTNAME,LASTNAME) AS NAME, HIREDATE from EMPLOYEE ORDER BY HIREDATE )
WHERE ROWNUM = 1;

Select 2 lowest values from a query in SQL [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 7 years ago.
I am trying get information of 2 lowest salary holders from default "scott" table.
This is the query I am trying:
SELECT TOP 2 * FROM emp ORDER BY sal ASC
But I'm getting this error:
ORA-00923: FROM keyword not found where expected
Screenshot:
In the most recent versions of Oracle, you can use the ANSI standard:
SELECT emp.*
FROM emp
ORDER BY sal ASC
FETCH FIRST 2 ROWS ONLY;
In older versions:
SELECT e.*
FROM (SELECT emp.*
FROM emp
ORDER BY sal ASC
) e
WHERE rownum <= 2;
You can use ROWNUM in Oracle to get two first rows of your query results.
SELECT EMP1.* FROM (SELECT * FROM EMP ORDER BY SAL ASC) EMP1 WHERE ROWNUM < 3;
TOP X is used by SQL Server
Fetch first is used by Oracle and DB2
(rownum is also available in Oracle)
Limit is used by mysql
Many can also use a windowing function (ROW_NUMBER()) in a sub query.
You want
select * from emp ORDER BY sal ASC
fetch first 2 rows only

SQL query in Oracle and SQL server to get 3rd highest salary

In oracle will the below query works for getting 3rd highest salary.
select empname, salary
from (select empname,salary from employee order by salary desc)
where rownum==3
I have seen answers in other threads but they seem to be complicated. If above query works then it is simple solution for oracle
What will be the query for SQLServer?
I don't have Oracle and SQLServer software to try out these queries.
Please let me know
For SQL server the solution would be like this:
Select TOP 1 Salary as '3rd Highest Salary'
from (SELECT DISTINCT TOP 3 Salary from Employee ORDER BY Salary DESC)
a ORDER BY Salary ASC
Use Dense_Rank() ranking function in SQL SERVER to find the 3rd highest salary.
Row_number() returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.The ORDER BY clause determines the sequence in which the rows are assigned their unique ROW_NUMBER
SELECT empname,
salary
FROM (SELECT Dense_Rank() OVER(ORDER BY salary DESC) rn,
empname,
salary
FROM employee) A
WHERE rn = 3
I did some exploration and found below link which explains the difference between row_number(), rank() and dense_rank(). From the below link I found dense_rank() is suitable answer for this question.
http://www.dwbiconcepts.com/tutorial/24-interview-questions/190-top-20-sql-interview-questions-with-answers.html
row_number() does not give expected results if two or more empolyees has same salary.
Below query works for both Oracle and SQL Server when tried on http://sqlfiddle.com/. The following query gives you the list of employees with third highest salary
SELECT empname, salary FROM (SELECT empname, salary, dense_rank() over(order by salary desc) dense_rank_by_sal
FROM EMPLOYEE ) A where dense_rank_by_sal=3;

How to use rownum [duplicate]

This question already has answers here:
How to get second largest or third largest entry from a table [duplicate]
(12 answers)
SELECTing top N rows without ROWNUM?
(5 answers)
Closed 9 years ago.
I have a employee table in oracle with name,salary and other details.
I am trying to get the second highest salary but not able to fetch.
This one working fine
with e_salary as (select distinct salary from employee)
select salary from e_salary
order by salary desc
And gives output:
450000
61000
60000
50000
40000
30000
20000
6000
but when i am using the same query to fetch second highest row not getting any output
select salary
from ( with e_salary as (select distinct salary from employee)
select salary from e_salary order by salary desc)
where rownum = 2
but as i replace the rownum=2 with rownum<2 it gives output of first two records. Please someone explain why rownum=2 is not working
This will work:
select salary from ( select salary , rownum as rn from (select salary
from e_salary order by salary desc)) where rn = 2;
Why it doesn't work:
When assigning ROWNUM to a row, Oracle starts at 1 and only only increments the value when a row is selected; that is, when all conditions in the WHERE clause are met. Since our condition requires that ROWNUM is greater than 2, no rows are selected and ROWNUM is never incremented beyond 1.
Hope u are clear right now.
select ename ,sal ,rank() over (order by sal desc) ranking from emp;
Try this one.
Follow this link, all the things regarding nth highest row is given over here in oracle:
http://www.oratable.com/nth-highest-salary-in-oracle/
Use of rownum is a tricky affair. Safest bet is to use it only when you want to limit the number of results to be shown. For example rownum<2 or rownum<=5.
Why rownum=2 will not work?
Read here - http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
In summary, this is how oracle execute a query
The FROM/WHERE clause goes first.
ROWNUM is assigned and incremented to each output row from the FROM/WHERE clause.
SELECT is applied.
GROUP BY is applied.
HAVING is applied.
ORDER BY is applied.
rownum<=2 clause will get converted to
ROWNUM = 1
for x in
( select * from emp )
loop
exit when NOT(ROWNUM <= 2)
OUTPUT record to temp
ROWNUM = ROWNUM+1
end loop
SORT TEMP
if you change exit when NOT(ROWNUM <= 2) with rownnum=2, you can see it will fail in the first run itself
So if I cannot use rownum, what can I use. Try using row_number() http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions137.htm
It works something like
SELECT last_name FROM
(SELECT last_name, ROW_NUMBER() OVER (ORDER BY last_name) R FROM employees)
WHERE R BETWEEN 51 and 100;
rownum in a condition stops evaluating the first time it fails. On the first row returned, rownum is 1, therefore it fails the rownum = 2 test and stops trying. There's an excellent post about it here.
To get the second-highest salary, use the Oracle analytical DENSE_RANK function:
SELECT DISTINCT Salary FROM (
SELECT Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) AS SalaryRank
FROM e_salary)
WHERE SalaryRank = 2
Note that if there's a tie for second, the query could return more than one value. That's why the outer SELECT is a SELECT DISTINCT.
First you should understand what the rownum is. Let me give you an example,
you want to get data with a filter and rownum=2,
first Oracle executes the sql with filter and get the first record,
give it the rownum 1,
and then compare it the rownum filter rownum=2, which doesn't match, so discard record,
then get second record, give it rownum=1(if the first record is matched then the rownum will be 2) too, then do the compare............
So you can find the reason.
Without using rownum command you can get the second highest salary by using this query:
select MAX(Salary) from Employee
WHERE Salary NOT IN
(select MAX(Salary) from Employee )
or,
select MAX(Salary) from Employee
WHERE Salary <>
(select MAX(Salary) from Employee )
query for nth highest:
SELECT * FROM Employee Emp1
WHERE (N-1) =
(SELECT COUNT(DISTINCT(Emp2.Salary))FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
From what I understand, rownum numbers the rows in a result set.
So, in your example:
select * from table1 where rownum=2
How many rows are there going to be in the result set? Therefore, what rownum would be assigned to such a row? Can you see now why no result is actually returned?
In general, you should avoid relying on rownum, or any features that imply an order to results. Try to think about working with the entire set of results.
With that being said, I believe the following would work:
select * from (select rownum as rn,table1.* from table1) as t where t.rn = 2
Because in that case, you're numbering the rows within the subquery.

query for selecting top 5 salaries from table [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to select top N from a table
How can a write a query for selecting the top 5 salaries from table ?
SELECT TOP 5 Salary
FROM SalariesTable
ORDER BY Salary DESC
SELECT TOP 5 Salary
FROM Salaries
ORDER BY Salary DESC
To get the TOP 5 highest Salaries:
SELECT DISTINCT TOP 5 MAX(Salary)
FROM Salaries
GROUP
BY Salary
ORDER
BY Salary DESC;