In SQL, the SELECT
statement is used to query a database table and return a result set based on specified conditions. The TOP
clause is used to limit the number of rows returned by a SELECT
statement.
Syntax
The basic syntax of the SELECT
statement with the TOP
clause is as follows:
SELECT TOP expression column1, column2, ...
FROM table
WHERE condition;
Here, expression
represents the number of rows to be returned.
Example
Suppose we have a table called employees
with columns id
, name
, age
, and salary
. We want to retrieve the top 5 employees based on their salary. Here’s how we can do it:
SELECT TOP 5 name, salary
FROM employees
ORDER BY salary DESC;
In the above example, the TOP 5
clause specifies that we want to retrieve only the top 5 records. The ORDER BY salary DESC
clause sorts the records in descending order based on the salary
column.
Conclusion
The SELECT TOP
clause is a powerful feature in SQL that allows you to limit the number of rows returned by a SELECT
statement. It is useful when you only need a certain number of records from a large result set. By using the TOP
clause, you can efficiently retrieve the required data from a database table.
#SQL #SELECT #TOP