Discussion Locked

sql query

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
what is the sql query to find the second maximum sal of emp table.sal is a numeric column of emp table.

6 replies

by glen_mcleod

Try this (there are probably many others):

Select TOP 1 e1.sal
From emp e1
where e1.sal < (Select Max(e2.sal) From emp e2)
order by e1.sal DESC.

The sub-query will select the max first(1 value), then use it as the criteria for the main query.

This will work in both Access and SQL Server 6.5+, but not in Oracle. In Oracle there's a ROWID property (I think that's what it's called, it's been a while) that will let you limit the number of records returned by using it in the Where clause (ROWID = 1).

You also might try using Max(sal) in the main query instead of TOP 1, I haven't tried it, but it should work.

Glen

by glen_mcleod

Ok, just tried it and it should work anywhere:

Select Max(e1.sal)
From emp e1
where e1.sal < (Select Max(e2.sal) From emp e2)

Glen

This discussion is closed to new replies.