How to display nth highest record in a table for example? How to display 4th highest (salary) record from customer table?

Showing Answers 1 - 75 of 107 Answers

Ben

  • May 9th, 2006
 

select max(salary) from tablename where salary not in (select salary from tablename order by salary desc limit n,q)

  Was this answer useful?  Yes

abdul rasheed

  • May 19th, 2006
 

select ename,sal from ema where

&n=(select count (distinct(sal)) from empb where

a.sal<=b.sal;

  Was this answer useful?  Yes

webgurru

  • May 30th, 2006
 

Query: SELECT sal FROM `emp` order by sal desc limit (n-1),1If the question: "how to display 4th highest (salary) record from customer table."The query will SELECT sal FROM `emp` order by sal desc limit 3,1

smitha m vincent

  • Oct 30th, 2006
 

Query as:
select distinct(salary) from employee order by salary desc limit n-1,1
n->find the nth largest

to find 3rd largest query it as
select distinct(salary) from employee order by salary desc limit 2,1

Piyush Patel

  • Sep 11th, 2007
 

Suppose two record are same for eg: two record has same value, than how we can find second highest record using same query ?

  Was this answer useful?  Yes

prisharma

  • Dec 17th, 2007
 

Select sal From emp X Where n =

( Select Count(Distinct sal) From emp Where sal >=X.sal )



YOu can change the value of n and get the 4th , 5th.... sal of the employee...

I got the answer using this query, but seems lengthy. Can anybody suggest shorten this??

for nth highest sal from emp table

Code
  1.  SELECT * FROM (SELECT * FROM (SELECT * FROM emp ORDER BY sal) WHERE rownum <=n ORDER BY sal desc) WHERE rownum = 1 ;

  2.  

  Was this answer useful?  Yes


Code
  1.  SELECT e1.ename,e1.empno,e1.sal FROM emp e1 WHERE n-1=(SELECT count(DISTINCT sal) FROM emp e2 WHERE e2.sal>e1.sal)

  2.  


Try this

  Was this answer useful?  Yes

quick

  • Jul 17th, 2008
 

FOR ORACLE DATABASE

Code
  1. SELECT rownum,sal

  2. FROM (SELECT sal

  3. FROM emp

  4. ORDER BY sal desc)

  5. WHERE rownum < 5

  6.  



FOR POSTGRES I AM ABLE TO FIND HIGHEST 4 RECORDS:

Code
  1. SELECT emp_salary FROM salary_detail

  2. ORDER BY emp_salary desc

  3. LIMIT 4



BUT I AM NOT ABLE TO GET THE FOURTH HIGHEST RECORD, CAN ANY BUDDY PLEASE HELP

THANKS IN ADVANCE

  Was this answer useful?  Yes

select max(sal) from emp where sal not in
(select top 1 sal from emp group by sal order by sal desc)
--This displays 2nd highest salary if you want to display 5th highsest record then replace top 1 with top 4 for the N th record top N-1

  Was this answer useful?  Yes

cnikita

  • Nov 10th, 2008
 

Code
  1. SELECT b.salary FROM

  2. (SELECT DISTINCT salary FROM emp) a,

  3. (SELECT DISTINCT salary FROM emp) b

  4. WHERE a.salary >= b.salary

  5. GROUP BY b.salary

  6. HAVING COUNT(b.salary) = 3;

  7.  


Code
  1. SELECT salary FROM emp e WHERE 2 = (SELECT count(DISTINCT(salary)) FROM emp d WHERE e.salary < d.salary ) ORDER BY salary asc

  Was this answer useful?  Yes

Elayaraja

  • Jul 27th, 2011
 

Its very easy to find Nth largest value in column in table.

Code
  1. SELECT DISTINCT user_id FROM `tbl_general` ORDER BY user_id DESC LIMIT (n-1),1

  Was this answer useful?  Yes

saiprathapreddy

  • Aug 23rd, 2011
 

select column_name from table_name order by column_name desc limit 3;

eg;select sal from emp order by sal desc limit 4;

  Was this answer useful?  Yes

lucky

  • Aug 28th, 2011
 

Code
  1. SELECT max(salary) FROM empolyee e1 WHERE 4<(SELECT count(*) FROM employee e2) WHERE e1.salary <e2.salary

  Was this answer useful?  Yes

satish

  • Sep 9th, 2011
 

To get 4th highest sal:

Code
  1. SELECT top 1 sal FROM sal

  2. WHERE sal IN (SELECT top 4 * FROM sal

  3. ORDER BY sal DESC)

  4. ORDER BY sal ASC

  Was this answer useful?  Yes

mahendra pandey

  • Sep 12th, 2011
 

This is the second highest salary .
you can find third and fourth highest record as well as.....

Code
  1. <?php

  2. $conn=mysql_connect("localhost","root","") or die(mysql_error());

  3. mysql_select_db("test",$conn) or die(mysql_error());

  4. $sql1 = "select salary from emp group by salary order by salary desc Limit 1,1";

  5. $sql3="select name,salary from emp where salary=(".$sql1.")";

  6. $rs3 = mysql_query($sql3) or die(mysql_error());

  7. $res=mysql_fetch_array($rs3);

  8. $sal=$res['salary'];

  9. echo $sal;

  10. $s="select max(salary) from emp where salary <'$sal'";



  11. print_r($out);

  12. ?>

  Was this answer useful?  Yes

Hariharan Subramoniam

  • Sep 16th, 2011
 

Code
  1. SELECT name, salary

  2. FROM table

  3. WHERE salary < (

  4. SELECT max( salary )

  5. FROM table )

  6. ORDER BY salary DESC

  7. LIMIT (n-1) , 1

  Was this answer useful?  Yes

YASHPAL CHAHAR

  • Sep 17th, 2011
 

Code
  1. SELECT * FROM EmployeeInfo as e1 WHERE n=(SELECT count(*) FROM EmployeeInfo as e2 WHERE e1.salary <= e2.salary );


where n is number that you want to find out which highest salary.

  Was this answer useful?  Yes

Santosh

  • Sep 21st, 2011
 

Code
  1. SELECT MAX(salary) FROM employment LIMIT 3,1

  Was this answer useful?  Yes

Jit

  • Sep 28th, 2011
 

The answer is...

Code
  1. SELECT `Salary` FROM `customer`ORDER BY `Salary` DESC  LIMIT 5,1

  Was this answer useful?  Yes

fahim

  • Mar 31st, 2012
 

If the highest column is salary, you do this:

Code
  1. SELECT salary FROM employee

  2. ORDER BY salary desc

  3. LIMIT 2

  4.  

  Was this answer useful?  Yes

Zafar Malik

  • Apr 21st, 2012
 

4th higest salary :

Code
  1. SELECT a.salary FROM customer a WHERE 3=(SELECT count(b.salary) FROM customer b WHERE b.salary>a.salary)

  Was this answer useful?  Yes

Bhaskar

  • Jul 7th, 2012
 

Code
  1. SELECT MIN(salary)

  2. FROM Customer

  3. WHERE salary IN

  4. (

  5. SELECT TOP 4 salary

  6. FROM Customer

  7. ORDER BY salary DESC

  8. )

  Was this answer useful?  Yes

Sathish Kumar

  • Jul 10th, 2012
 

ITS a basic query, then just use LIMIT:
-- get the 4th highest salary

Code
  1. SELECT salary FROM customer

  2. ORDER BY salary DESC

  3. LIMIT 4,1

  Was this answer useful?  Yes

Vinit

  • Sep 13th, 2012
 

Code
  1. SELECT salary FROM table_name ORDER BY salary DESC LIMIT 1 OFFSET 3

  Was this answer useful?  Yes

Nadeem

  • Apr 2nd, 2013
 

SELECT emp_salary FROM salary_detail ORDER BY emp_salary desc LIMIT 3,1

  Was this answer useful?  Yes

Above query is right but we should use also Group by because many employee can be same salary.

SELECT emp_salary FROM salary_detail GROUP BY emp_salary ORDER BY emp_salary desc LIMIT 3,1

  Was this answer useful?  Yes

mukul Bhardwaj

  • May 17th, 2014
 

SELECT max(salary) FROM `testtable` where salary <(SELECT max(salary) FROM `testtable` where salary <(SELECT max(salary) FROM `testtable` where salary <(SELECT max(salary) FROM `testtable` having salary < max(salary)))) limit 1

Code
  1.  

  Was this answer useful?  Yes

Arun kumar yadav

  • Nov 13th, 2014
 

SELECT DISTINCT SALARY FROM TABLENAME ORDER BY DESC SALARY LIMIT 3,1;

  Was this answer useful?  Yes

raj kumar saw

  • Aug 2nd, 2015
 

Yes, it is. But this answer is not the best approach.

  Was this answer useful?  Yes

Fawwad

  • Feb 26th, 2016
 

Code
  1. SELECT * FROM SALARY ORDER BY SALARY DESC LIMIT N-1,1


  Was this answer useful?  Yes

anish

  • May 11th, 2016
 

Code
  1. SELECT * FROM ( SELECT * FROM order1 ORDER BY o_id LIMIT 5 ) AS T ORDER BY o_unit DESC LIMIT 3;

  Was this answer useful?  Yes

Hitender

  • Jan 27th, 2017
 

SELECT DISTINCT(`salary`) FROM `customer` ORDER BY `salary` DESC LIMIT 3,1;

  Was this answer useful?  Yes

Dan Rusk

  • Apr 30th, 2017
 

Code
  1. SELECT *

  2. FROM (

  3.        SELECT

  4.          c.*,

  5.          @rownum := @rownum + 1 AS rank

  6.        FROM customer c,

  7.          (SELECT @rownum := 0) r

  8.        ORDER BY c.salary DESC

  9.      )

  10.      t

  11. WHERE t.rank = 4;

  Was this answer useful?  Yes

Amith L

  • Jul 11th, 2017
 

You can find out nth highest salary,
In the below code just change the where condition i.e., if you want to get 2nd highest salary, use where (2)

Code
  1. SELECT * FROM CUSTOMER CUS1

  2. WHERE (4) =

  3. (SELECT COUNT(CUS2.SALARY) FROM CUSTOMER CUS2 TABLE

  4. WHERE CUS2.SALARY >= CUS1.SALARY)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions