SQL Query to Find Top 3 Salaries Without LIMIT or TOP

“`html

πŸ† 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_idemployee_namedepartmentsalary
101John SmithHR75000
102Alice BrownIT85000
103David WhiteFinance95000
104Linda BlackSales65000
105Mike DavisMarketing90000

πŸ’» 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 LIMIT or TOP β€” 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 salary and department columns 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

πŸ’‘ These books help you go from SQL fundamentals to advanced data visualization with Power BI and AI engineering.


πŸ”— Related SQL Tutorials


πŸ’¬ 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
“`