📜  select top 3 sql (1)

📅  最后修改于: 2023-12-03 15:34:54.321000             🧑  作者: Mango

Introduction to SQL SELECT TOP 3

In SQL, the SELECT statement is used to query a database table and retrieve data. The TOP clause is used to limit the number of rows returned by the query. When used with the number 3, it limits the returned rows to the first 3 rows.

Syntax
SELECT TOP 3 * FROM table_name;

The SELECT statement is followed by the TOP clause and the number of rows to be returned.

Example

Consider the following table 'students':

| id | name | age | grade | |----|---------|-----|-------| | 1 | Alice | 18 | A | | 2 | Bob | 19 | B | | 3 | Charlie | 18 | C | | 4 | Dave | 20 | A | | 5 | Eve | 19 | B |

To retrieve the top 3 students based on their age, we can use the following SQL command:

SELECT TOP 3 * FROM students ORDER BY age DESC;

This command selects the top 3 rows from the table 'students' based on their age in descending order.

Markdown Code Block
SELECT TOP 3 * FROM table_name;
SELECT TOP 3 * FROM students ORDER BY age DESC;
Conclusion

The SELECT TOP 3 clause is a useful tool in SQL for retrieving a limited amount of data from a database table. By using the ORDER BY clause, you can choose which rows to retrieve based on different criteria such as age, grade, or name.