Sql Query to Retrieve Nth Highest Salary From Table in SQL Server

SQL Server Faqs

SQL Query to find Nth Highest Salary in SQL Server.

SELECT * FROM tablename e1 WHERE N = (SELECT COUNT(DISTINCT (e2.sal))
FROM tablename e2 WHERE e2.sal >= e1.sal)

For example to get 3rd highest salary:
SELECT * FROM emp e1 WHERE 3 = (SELECT COUNT(DISTINCT (e2.sal))FROM emp e2 WHERE e2.sal >= e1.sal)

Similarly to get 2nd highest salary:
SELECT * FROM emp e1 WHERE 2 = (SELECT COUNT(DISTINCT (e2.sal))FROM emp e2 WHERE e2.sal >= e1.sal)

SQL Server Faqs
SQL Query to copy only structure of one table to other table in SqlServer
SQL Query to get structure of table in SqlServer

Movie Songs Lyrics
Software Testing-QTP Scripts,Testing Faqs

How to Retrieve a Random Record from a Table in SqlServer

SQL Query to display a random record from table in sqlserver.

Syntax:Select Top 1 * From tablename Order By NewId()

Example:Select Top 1 * From emp Order By NewId()

Above query will display one random record.

SQL Query to copy structure and data of one table to other in Sql Server.

Software Testing-QTP,Testing Tools,Testing Faqs

Collection of SQL Queries

Recent Posts