The SELECT TOP clause is used to specify the number of records to return. Let’s say you have a blog site & you want to display top 5 popular articles from Article table. Then we need to use SQL TOP clause to return a specific number of records or rows. SQL Server also provides us to get percentile basis records. So TOP clause is a reach clause of SQL SELECT statement.
Basic Syntax of SQL TOP Clause:
SELECT TOP number|percent column_name_list FROM your_table_name;
Create a Table Like Below:
SQL TOP Example:
If we want only 3 Records from tblStock table then the SQL query should be:
SELECT TOP 3 * FROM tblStock
SELECT LAST or BOTTOM Nth Records:
Now if we want to select 3 products based on lowest quantity then simple TOP won’t give us appropriate result. We need to sort products in ascending order & after that need to select 3 products. So the query should be:
SELECT TOP 3 * FROM tblStock ORDER BY PhysicalQuantity
Note: If you want opposite result then just change the sorting order to DESC.
SELECT TOP x Percent:
If we want to select TOP 20% records with highest quantity then the query will be:
SELECT TOP 20 PERCENT * FROM tblStock ORDER BY PhysicalQuantity DESC
So TOP clause is a necessary clause for most of the financial records.