🧚 Ακούστε! Το Beekeeper Studio είναι ένα γρήγορο, μοντέρνο και ανοιχτού κώδικα GUI βάσης δεδομένων Λήψη
March 14, 2025 Από Matthew Rathbone

How to Select the Top N Rows in MySQL

Selecting the top N rows from a table is a fundamental task when working with databases. In this tutorial, we’ll explore how to accomplish this in MySQL with practical examples.

Using the LIMIT Clause

The LIMIT clause in MySQL is a straightforward way to specify the number of rows to return from a query result set.

Example: Select the Top 5 Rows

Suppose you have a table named employees with the following schema:

CREATE TABLE employees (
    id INT,
    name VARCHAR(100),
    salary DECIMAL(10, 2)
);

You want to select the top 5 employees based on salary in descending order.

SELECT id, name, salary 
FROM employees 
ORDER BY salary DESC 
LIMIT 5;

Why it Works:

  1. ORDER BY salary DESC sorts the rows from highest to lowest salary.
  2. LIMIT 5 restricts the output to the first 5 rows after sorting.

Expected Output:

+----+----------+--------+
| id | name     | salary |
+----+----------+--------+
|  3 | Alice    | 90000  |
|  5 | Bob      | 85000  |
|  2 | Charlie  | 75000  |
|  6 | Diana    | 70000  |
|  4 | Edward   | 68000  |
+----+----------+--------+

Handling Tie Scores with LIMIT

When scores are tied, simply using LIMIT may not provide consistent results without additional handling of tie conditions. ORDER BY more columns to break ties.

Example: Handling Ties by Adding a Secondary Sort

To handle tied salaries, sort by the employee’s id as a secondary sort criterion:

SELECT id, name, salary 
FROM employees 
ORDER BY salary DESC, id ASC 
LIMIT 5;

Why it Works:

  • Adding id ASC ensures that when salaries are tied, employees with a lower id are chosen consistently.

Using LIMIT with Pagination

Beyond just selecting the top N rows, often we need to paginate results by selecting a specific range of rows.

Example: Select the Next 5 Rows

To paginate results and select rows 6-10 based on salary:

SELECT id, name, salary 
FROM employees 
ORDER BY salary DESC 
LIMIT 5 OFFSET 5;

Why it Works:

  • LIMIT 5 specifies the number of rows to return.
  • OFFSET 5 skips the first 5 rows.

Expected Output:

The next set of 5 employees after the top 5:

+----+---------+--------+
| id | name    | salary |
+----+---------+--------+
|  1 | Frank   | 67000  |
|  7 | Grace   | 64000  |
|  8 | Hannah  | 60000  |
|  9 | Ivan    | 59000  |
| 10 | Jane    | 58000  |
+----+---------+--------+

Conclusion

Selecting the top N rows in MySQL using the LIMIT clause is efficient and easy. When faced with potential ties, ensure a stable sort order by using additional columns in the ORDER BY. This simple technique is essential for many applications, including analytics and reporting, especially when dealing with large datasets.

By mastering these techniques, you’ll have greater control over data retrieval and manipulation in MySQL.

Το Beekeeper Studio Είναι Ένα Δωρεάν & Ανοιχτού Κώδικα GUI Βάσης Δεδομένων

Το καλύτερο εργαλείο SQL query & editor που έχω χρησιμοποιήσει. Παρέχει όλα όσα χρειάζομαι για να διαχειριστώ τη βάση δεδομένων μου. - ⭐⭐⭐⭐⭐ Mit

Το Beekeeper Studio είναι γρήγορο, διαισθητικό και εύκολο στη χρήση. Το Beekeeper υποστηρίζει πολλές βάσεις δεδομένων και λειτουργεί εξαιρετικά σε Windows, Mac και Linux.

Η έκδοση Linux του Beekeeper είναι 100% πλήρης, χωρίς περικοπές και χωρίς συμβιβασμούς στα χαρακτηριστικά.

Τι Λένε Οι Χρήστες Για Το Beekeeper Studio

★★★★★
"Το Beekeeper Studio αντικατέστησε εντελώς την παλιά μου ροή εργασίας SQL. Είναι γρήγορο, διαισθητικό και κάνει τη δουλειά με βάσεις δεδομένων απολαυστική ξανά."
— Alex K., Προγραμματιστής Βάσεων Δεδομένων
★★★★★
"Έχω δοκιμάσει πολλά GUIs βάσεων δεδομένων, αλλά το Beekeeper βρίσκει την τέλεια ισορροπία μεταξύ χαρακτηριστικών και απλότητας. Απλά δουλεύει."
— Sarah M., Full Stack Μηχανικός

Έτοιμοι να Βελτιώσετε τη Ροή Εργασίας σας με SQL;

download Δωρεάν Λήψη