SQL Select Top is a command used to retrieve a specific number of rows from a table. It is commonly used to retrieve the top or highest values from a table based on a certain criteria. The SELECT TOP statement is supported by most relational database management systems (RDBMS) such as MySQL, Oracle, and Microsoft SQL Server.
The SELECT TOP statement is used in conjunction with the SELECT statement to retrieve a specific number of rows from a table. The syntax for the SELECT TOP statement is as follows:
SELECT TOP number|percent column_name(s) FROM table_name WHERE condition;
The number
parameter specifies the number of rows to be retrieved, while the percent
parameter specifies the percentage of rows to be retrieved. The column_name(s)
parameter specifies the columns to be retrieved, and the table_name
parameter specifies the table from which the rows are to be retrieved. The condition
parameter specifies the condition that must be met for the rows to be retrieved.
Here are some examples of how the SELECT TOP statement can be used:
SELECT TOP 5 * FROM customers;
This statement retrieves the top 5 rows from the customers
table.
SELECT TOP 10 PERCENT * FROM orders WHERE order_date >= '2021-01-01';
This statement retrieves the top 10% of rows from the orders
table where the order_date
is greater than or equal to January 1, 2021.
SELECT TOP 3 product_name, unit_price FROM products ORDER BY unit_price DESC;
This statement retrieves the top 3 highest values from the products
table based on the unit_price
column.
The SELECT TOP statement is a powerful tool for retrieving specific rows from a table. It can be used to retrieve the top or highest values from a table based on a certain criteria. By using the SELECT TOP statement, you can easily retrieve the data you need from a table without having to retrieve all the data and then filter it manually.