Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Top n Using Scalar Subqueries

Top n

Top n Using Scalar Subqueries

by  MalcolmW  Posted    (Edited  )
Code:
SELECT (SELECT MAX (Salary)
          FROM Employees) AS S1,
       (SELECT MAX (Salary)
          FROM Employees
         WHERE Salary NOT IN (S1)) AS S2,
       (SELECT MAX (Salary)
          FROM Employees
         WHERE Salary NOT IN (S1, S2)) AS S3,
           ...
       (SELECT MAX (Salary)
          FROM Employees
         WHERE Salary NOT IN (S1, S2, ... S[n-1])) AS Sn
FROM DummyTable
DummyTable can be any existing table, empty or not.
This example is from SQL For Smarties, by Joe Celko. In return for stealing his example, I will plug his book as excellent reference for SQL.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top