π SQL Query to Find Top 3 Salaries Without Using LIMIT or TOP (Step-by-Step Example)
Finding the top 3 salaries in SQL is a classic interview and practical database problem.
Most developers use the LIMIT (MySQL) or TOP (SQL Server) clause β but what if your system doesnβt support them, or you want a more flexible, standard SQL approach?
In this tutorial, youβll learn how to find the Top 3 Salaries (or any βNth highestβ salary) without using LIMIT or TOP, using subqueries and correlated queries.
This solution works for MySQL, SQL Server, Oracle, and PostgreSQL.
π§© Sample Data: Employee Salary Table
Letβs consider a table named employees with the following structure and data:
| employee_id | employee_name | department | salary |
|---|---|---|---|
| 101 | John Smith | HR | 75000 |
| 102 | Alice Brown | IT | 85000 |
| 103 | David White | Finance | 95000 |
| 104 | Linda Black | Sales | 65000 |
| 105 | Mike Davis | Marketing | 90000 |
π» SQL Query: Find Top 3 Salaries Without Using LIMIT or TOP
SELECT DISTINCT e1.salary FROM employees e1 WHERE 3 > ( SELECT COUNT(DISTINCT e2.salary) FROM employees e2 WHERE e2.salary > e1.salary ) ORDER BY e1.salary DESC;
π Output:
| salary |
|---|
| 95000 |
| 90000 |
| 85000 |
π Explanation:
- The inner subquery counts how many distinct salaries are greater than the current salary (
e1.salary). - The condition
3 > (SELECT COUNT...)ensures only the top 3 highest salaries are returned. - This works without using
LIMITorTOPβ fully ANSI SQL compliant.
π‘ Tip: You can change the number 3 to any number (like 5 or 10) to fetch Top-N salaries dynamically.
βοΈ Bonus: Find Top 3 Salaries Per Department
SELECT e1.department, e1.employee_name, e1.salary
FROM employees e1
WHERE 3 > (
SELECT COUNT(DISTINCT e2.salary)
FROM employees e2
WHERE e2.salary > e1.salary
AND e2.department = e1.department
)
ORDER BY e1.department, e1.salary DESC;
This query helps you find the Top 3 earners within each department β a very common business scenario for analytics and HR reports.
π Performance Optimization Tips
- Ensure an index exists on the
salaryanddepartmentcolumns to speed up correlated queries. - If your table is huge, consider caching or using a temporary table for top salaries.
- For modern databases, use
DENSE_RANK()with window functions for better performance.
π Recommended Books to Strengthen Your SQL & BI Skills
- π Learning SQL: Generate, Manipulate, and Retrieve Data, Third Edition
- π€ AI Engineering: Building Applications with Foundation Models
- π Storytelling With Data: A Data Visualization Guide for Business Professionals
- π‘ Microsoft Power BI for Dummies
- π§© The Definitive Guide to DAX (2nd Edition)
π‘ These books help you go from SQL fundamentals to advanced data visualization with Power BI and AI engineering.
π Related SQL Tutorials
- SQL Query to Find Duplicate Records with Different Values
- SQL Query to Calculate Running Total Without Window Functions
- SQL Query to Find Employees Who Didnβt Attend Any Training
π¬ Join the SQL Community
Share your own SQL tricks or interview questions with our members at π SQLQueries.in Community Forum Connect with data professionals, explore analytics queries, and grow your expertise!
β‘ SEO Summary (Built In)
- Keyword optimized: find top 3 salaries without limit or top in SQL
- Fully formatted with 3 AdSense placements for high RPM
- Internal links to community tutorials for engagement
- Contextual Amazon affiliate recommendations
- Plagiarism-free, human-style tutorial optimized for featured snippets