Wednesday, December 10, 2008

Finding MAX salary from a table

While learning SQL for my testing purpose, I came across many problems.
I know most often in interview there is a question "how to select max, second max or third max salary from table........ect"
I tried my hand on this and came with the final solution taking help from colleague and google devata.
really facing problems finally came with more experience and knowledge.
here I want to share some queries on select statement. Hope these will help specially for beginners
try to execute all the given scenarios , this will help you to understand better.....
1- Create a table first by using the following statement. This will create a table named 'employee' with two columns 'name' and 'salary'.
CREATE TABLE employee (name varchar(256) , salary int)

2- Insert some data in this table. for this use
INSERT INTO employee (name, salary)
VALUES ('vishal' , 32000)
insert more data to this table.for example ('amit', 20000), ('nitin', 25000), ('raju', 30000), ('david', 27000), ('marc', 18000)

3- Selecting max salary from table:
SELECT max(salary) FROM employee

4- Selecting second highest salary from table:
SELECT max(salary) FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee)

5- Selecting third highest salary :
SELECT MAX(salary) FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee))

6- Selecting fourth highest salary:
SELECT MAX(salary) FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee)))
and so on........................
But it seems to heavy and to complicated..............
here I am going to write a query for Nth highest salary....

SELECT salary FROM employee e1
WHERE (N=(SELECT count(distinct(e2.salary)) FROM employee e2 WHERE e2.salary >= e1.salary))

You can put 1, 2 ,3, 4......for first, second, third.......in place of N.

hope you will be happy now.........

6 comments :

  1. Hi vishal,
    This is very helpful post. I am also learning SQL.Please keep posting such a nice and helpful post.

    ReplyDelete
  2. Hi Vishal,
    So now u going to be backend quality analyst, good buddy keep it up, if you need any help m always here for help you as my best.

    ReplyDelete
  3. Thanks Buddy......definitly i am going to add a new skill to my profile. Really I am very thank full to Manish Seth for his motivation and support. I know i can do it...as you are there to help me......

    ReplyDelete
  4. Hi,

    your blog is good, We can write the same query in this way where we dont have to write a big query to find 2,3rd,4th highest salary.

    select employee_id, first_name, last_name, salary,
    rank() over (order by salary desc)
    from employees;

    which gives the ranks in desc order where you can pick nth dalary details.

    we can you use one more command "TOP", which you can refer in W3schools.com.

    Thanks
    Rama

    ReplyDelete
  5. But if you have same salaries for 2 employees u can use this query for continously ranking

    select employee_id, first_name, last_name, salary,
    dense_rank() over (order by salary desc)
    from employees;

    ReplyDelete
  6. I have reading your articles. It is well written. It looks like you spend a large amount of time and effort in writing the blog. I am appreciating your effort.

    STC Technologies|STC Technologies

    ReplyDelete