I need to work out this with out using TOP
I use the following query to display top 10 salaries
SELECT Salary
from
(
SELECT Salary, Row_Number() OVER(ORDER BY SALARY DESC) AS 'Salaries'
FROM User2
)#emp
WHERE Salaries <=10
But i am getting the list as 9000,8000,7000,6000,5000,500,4000,3000,2000,10000..
1000 is missing here
What to do i tried by making
WHERE Salaries <10 (but 10000 is not displaying)
What's wrong i made can any give me the appropriate one
First off I see 500 in your list. If the salaries are not numeric, then a string sort is used. So it would be 10000, 9000,8000,7000,6000,5000,4000,3000,2000,1000. You really should change the column datatype, but if you can't you will need to use the convert function e.g. convert(numeric(9,2),Salary).
Also,I think you would be better off using the Rank function - http://msdn.microsoft.com/en-us/library/ms176102.aspx since it does what you are trying to do. Then if you need the top 15, you only need to make that change.
Example:
SELECT Salary
FROM
(SELECT Salary
,RANK() OVER
(ORDER BY SALARY DESC) AS 'RANK' From User2) Salaries
WHERE
Salaries.RANK <= 15
What's wrong with:
SELECT TOP 10 Salary
FROM User2
ORDER BY Salary DESC
You appear to be over complicating things.
Try the folowing instead.
SELECT TOP 10 Salary FROM User2 ORDER BY Salary DESC
Related
How retrieve other attribute with max function like max salary with name.
to retrieve max salary along with their name please help anyone.
If my understanding of the question is correct, you can use something like this:
select name, salary from table where salary = (select max(salary) from table)
With SalaryOrder AS (
Select name, salary, Row_Number() Over(Order By salary Desc) RN
From table
)
Select /*the top clause is needed as described below*/ Top (1) *
From SalaryOrder Where RN = 1
this query may return more than a record when existing people with the same salary.
To solve this you can add more orders in CTE or use Top one to pick a random record.
RANK() should be an efficient way to get name where salary is MAX. Using sub-query will also get us the same result but it will take more time than RANK() in some cases.
Query:
SELECT name, salary
FROM
(
SELECT name, salary, RANK() over(ORDER BY salary DESC) AS rnk
FROM your_table
) AS a
WHERE rnk=1
Look at the db<>fiddle with time consumption. (The time may vary for different runs)
My task is:
To make a query which will get Employees, who earn the biggest salary for their working experience. In other words, the Employee who earns the biggest salary with the biggest experience.
As I consider, I need to make a query with two conditions:
select * from employee where salary in (select max(salary) from employee) and
hire_date in (select min(hire_date) from employee)
I think this was what you're trying to do:
select * from (
select *,
datediff(day,hire_date,getdate()) [Days_Worked],
dense_Rank() over(Partition by datediff(day,hire_date,getdate()) order by salary desc) [RN]
from employee
)a
where a.RN = 1
order by Days_Worked DESC
So that'll give you a list of employees with the highest salary against the employees that have worked the same number of days.
Just note that with dense rank if for example there are 2 employees that have worked 88 days and both earn $50000 (higher than anyone else) it will list both employees, use ROW_NUMBER() instead of DENSE_RANK() if you want to restrict examples like that to 1 employee.
If I get it correctly, this query solves your problem.
SELECT TOP 1 WITH TIES * FROM
employee
ORDER BY
hire_date ASC,
salary DESC
Does Presto SQL really lack TOP X functionality in SELECT statements?
If so, is there a workaround in the meantime?
https://prestodb.io/
If you simply want to limit the number of rows in the result set, you can use LIMIT, with or without ORDER BY:
SELECT department, salary
FROM employees
ORDER BY salary DESC
LIMIT 10
If you want the top values per group, you can use the standard SQL row_number() window function. For example, to get the top 3 employees per department by salary:
SELECT department, salary
FROM (
SELECT department, salary row_number() OVER (
PARTITION BY department
ORDER BY salary DESC) AS rn
FROM employees
)
WHERE rn <= 3
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.
I am really confused how to write query for the following statement.
How to display top 5 employees with the highest number of sales (total) and display position as a field. Note that if both of employees have the same total sales values they should receive the same position, in other words Top 5 employees might return more than 5 employees?
Anyone can suggest an answer for this?
Something like:
WITH cte AS (
SELECT
Rank() OVER (ORDER BY SUM(salary) DESC ) AS [Rank]
, SUM(salary) as sum
, Employee
FROM
[table]
GROUP BY
Employee
)
SELECT *
FROM cte
WHERE [RANK] < 5
ORDER BY sum DESC
I re-read your question and it seems that you're looking for the top count of sales rather than sum of salary, but of course, in that case you can just use COUNT(sales) in the OVER statement.