how to check the 3rd max salary from an employee table? One
of the queries used is as follows:
select sal from emp a where 3=(select
count(distinct(sal)) from emp b
where a.sal<=b.sal).
Here in the sub query "select
count(distinct(sal)) from emp b
where a.sal<=b.sal" or
"select count(distinct(sal)) from emp b
where a.sal=b.sal" should reveal the same number of rows is
in't it? Can any one here please explain me how is this
query working perfectly.
However, there is another query to get the 3rd highest of
salaries of employees that logic I can understand. Pls find
the query below.
"select min(salary) from emp where salary in(select
distinct
top 3 salary from
emp order by salary desc)"
Please explain me how
"select sal from emp a where 3=(select
count(distinct(sal)) from emp b
where a.sal<=b.sal)" works
source:http://www.allinterview.com/showanswers/33264.html.
Thanks in advance
Regards,
Karthik.
Answers were Sorted based on User's Feedback
Answer / masthan
SELECT MAX(SAL) FROM
EMP WHERE SAL<(SELECT MAX(SAL) FROM EMP WHERE SAL<(SELECT MAX(SAL) FROM EMP))
| Is This Answer Correct ? | 3 Yes | 2 No |
Answer / s.panda
select level,max(sal) from emp where level=&levelno
connect by prior sal>sal group by level
By entering the level no,Accordingly we can get the nth highest salay of a table.
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / gaurav
If you use "select count(distinct(sal)) from emp b
where a.sal=b.sal" It will always give output as 1. because for every salary in emp a, there will be one distinct sal in emp b.
So when we use a.sal <= b.sal, we are counting the number of people who have salary greater than or equal to your salary. So comparing it with 3 will give us 3rd maximum salary.
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / aalee
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 2 , 1
its so simple!!!!!!
| Is This Answer Correct ? | 6 Yes | 8 No |
What are the types of index in sql?
What is Collation Sensitivity ? What are the various type ?
display records from 5 to 9 using rowid or rownum
Differences between Oracle 9i and 10g (Probably in terms of SQL and PL/SQL)?
What is coalesce sql?
Like shown below i have 3 columns(Name,No,Address). The values in name column i want to modity. Requirement : Keep only once space between two words (Fname,Lname) in the Name column. For this what is the query? Please answer me. Advance Thanks. Name No Address Reference manoj kumar kumar raja vinzay kumar rajendra prasad gowri nath -- -- --
What is the purpose of the sql select top clause?
How to disable a trigger name update_salary?
what is a scheduled jobs or what is a scheduled tasks? : Sql dba
four procedures is are there should i write their in a package
How many commands are there in sql?
Can you selectively load only those records that you need? : aql loader
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)