πŸ† TOP – Get Only the First Few Rows in SQL

πŸ† TOP – Get Only the First Few Rows in SQL

πŸ”§ Quick Syntax

SELECT TOP number column1, column2
FROM table_name
ORDER BY column_name DESC;

This tells SQL:

β€œGive me just the first few rows from the sorted results β€” like a highlight reel.”

Note: TOP works in SQL Server. For MySQL or PostgreSQL, use LIMIT (we’ll cover that too).


🧾 Here’s the Table You’re Working With

Imagine you’ve got an orders table like this:

order_id customer_name total_amount
1Brill Venn15.99
2Larka Tove45.00
3Brill Venn15.99
4Milo Kresh72.50
5Vesh Luro10.00
6Tola Jinn29.99

βœ… Suppose You Want to See the Top 3 Highest Orders

SELECT TOP 3 customer_name, total_amount
FROM orders
ORDER BY total_amount DESC;

πŸ’‘ Output:

Milo Kresh    72.50
Larka Tove    45.00
Tola Jinn     29.99

Here you sorted the orders from highest to lowest, and TOP 3 gave you the first three rows. Great for dashboards, leaderboards, or best-of lists.


βœ… Suppose You Want the 2 Smallest Orders

SELECT TOP 2 customer_name, total_amount
FROM orders
ORDER BY total_amount ASC;

πŸ’‘ Output:

Vesh Luro     10.00
Brill Venn    15.99

This one sorted the data from smallest to largest and gave you the two cheapest orders. Same logic, just in reverse.


πŸ§ƒ Recap – What You Learned

  • TOP limits how many rows your query returns
  • Combine with ORDER BY to choose what β€œtop” really means
  • Great for bestsellers, highest spenders, top viewed items, and more
  • Works in SQL Server – for MySQL/PostgreSQL use LIMIT instead

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top