The LIMIT
(in MySQL, PostgreSQL) or TOP
(in SQL Server) clause is used to restrict the number of rows returned by a query. It's useful when you want only the first few results (like top 5 students, top 10 products, etc.).
SELECT column1, column2 FROM table_name
ORDER BY column_name DESC
LIMIT number;
SELECT TOP number column1, column2
FROM table_name
ORDER BY column_name DESC;
SELECT name, marks FROM Students ORDER BY marks DESC LIMIT 3;
This returns only the top 3 students with the highest marks.
ID | Name | Category | Price |
---|---|---|---|
1 | Tablet | Electronics | 25000 |
2 | Laptop | Electronics | 55000 |
3 | Phone | Electronics | 30000 |
4 | Mouse | Accessories | 800 |
5 | Keyboard | Accessories | 1200 |
Write a query to display the top 2 most expensive products using MySQL syntax.
Feature | MySQL | SQL Server |
---|---|---|
Keyword | LIMIT | TOP |
Placement | End of query | After SELECT |
Example | LIMIT 5 | SELECT TOP 5 ... |
LIMIT
to restrict rows in MySQL.TOP
to restrict rows in SQL Server.ORDER BY
to control which rows appear.Next ➡️: Learn about GROUP BY
to group similar data!
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!