SQL Tutorial



LIMIT/TOP CLAUSE


🔻 LIMIT / TOP Clause in SQL

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.).

🧠 MySQL Syntax:
SELECT column1, column2 FROM table_name
ORDER BY column_name DESC
LIMIT number;

🧠 SQL Server Syntax:
SELECT TOP number column1, column2
FROM table_name
ORDER BY column_name DESC;

🎓 Example: Top 3 Students by Marks (MySQL)

SELECT name, marks
FROM Students
ORDER BY marks DESC
LIMIT 3;
  

This returns only the top 3 students with the highest marks.


🛒 Products Table

ID Name Category Price
1TabletElectronics25000
2LaptopElectronics55000
3PhoneElectronics30000
4MouseAccessories800
5KeyboardAccessories1200

🧩 Try It Yourself!

Write a query to display the top 2 most expensive products using MySQL syntax.


⚖️ MySQL vs SQL Server

FeatureMySQLSQL Server
KeywordLIMITTOP
PlacementEnd of queryAfter SELECT
ExampleLIMIT 5SELECT TOP 5 ...

🧠 Summary

  • Use LIMIT to restrict rows in MySQL.
  • Use TOP to restrict rows in SQL Server.
  • Always combine with ORDER BY to control which rows appear.

Next ➡️: Learn about GROUP BY to group similar data!


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review