Software Testing | How to use SQL TOP to extract top data?

Introduction

In SQL query language, TOP clause is a very useful feature that allows us to extract a specified number of top data records from the database. This article will delve into the use of SQL TOP clauses, as well as some common scenarios and techniques in practical applications.

SQL TOP

SQL is a powerful language used to manage and operate relational databases, and the TOP clause is one of its important functions. Through the TOP clause, we can easily limit the number of records returned by the query results, which is very useful when we need to quickly obtain the first few data in the database or obtain specific data according to a certain sorting rule.

In SQL, the specific syntax and usage of the TOP clause may vary, depending on the database management system (DBMS) used. Here are some example usages of common SQL TOP clauses:

  1. Extract the first N records:
SELECT TOP N * FROM 表名;

This query statement will return the first N records from the specified table, where N is a positive integer.

  1. Extract the top N percent of records:
SELECT TOP N PERCENT * FROM 表名;

This query will return a specified percentage of records in the table, where N is a number between 0 and 100.

  1. Extract data according to sorting rules:
SELECT TOP N * FROM 表名 ORDER BY 列名;

By combining the TOP clause and the ORDER BY clause in the query, we can extract the top N records according to the sorting rules of the specified column.

Special usage

In addition to the basic syntax, there are some additional techniques and usages that allow us to make better use of the TOP clause, as follows:

  1. Paging query

By combining the TOP clause with the OFFSET clause, we can implement paging queries. For example:

SELECT TOP N * FROM 表名 ORDER BY 列名 OFFSET M ROWS FETCH NEXT K ROWS ONLY;

This query will start from the Mth row in the table sorted by the specified column and return the subsequent K rows of records.

  1. Use the TOP clause in combination with other query conditions

The TOP clause can be used in conjunction with other query conditions to obtain the top data that meets specific conditions. For example:

SELECT TOP N * FROM 表名 WHERE 条件;

By adding appropriate conditions in the WHERE clause, we can extract the top N records that meet certain conditions.

  1. Use subquery and TOP clause:

We can nest TOP clauses within subqueries to obtain more complex result sets. For example:

SELECT * FROM (SELECT TOP N * FROM 表名 ORDER BY 列名) AS 子查询别名;

This query will first sort based on the specified column, and then extract the top N records from the sorted result set.

Usage example

Let's still take Customersthe table as an example, which contains columns such as CustomerId, CustomerName, Country, City, Ageetc. The following is our Customerstable:

+----------+----------------+---------+-------------+---------+
|CustomerId|CustomerName    | Country |   City      |   Age   |
|00001     |Muller Cheng    | China   | Shanghai    |   25    |
|00002     |Kevin Durant    | USA     | Phoenix     |   34    |
|00003     |Libin Tian      | China   | Kaifeng     |   31    |
|00004     |Junye  Li       | China   | Guangzhou   |   32    |
|00005     |Leborn James    | USA     | LosAngles   |   39    |
|00006     |Stephen Curry   | USA     | SanFrancisco|   35    |
|00007     |Humphrey Appleby| UK      | London      |   56    |
|00008     |Jim Hacker      | UK      | London      |   60    |
|00009     |Thomas Muller   | Germany | Munich      |   34    |
|00010     |Miro Klose      | Germany | Berlin      |   45    |
  1. To extract the first 3 records, the statement is as follows:
SELECT TOP 3 * FROM Customers;

The result is as follows:

+----------+----------------+---------+-------------+---------+
|CustomerId|CustomerName    | Country |   City      |   Age   |
|00001     |Muller Cheng    | China   | Shanghai    |   25    |
|00002     |Kevin Durant    | USA     | Phoenix     |   34    |
|00003     |Libin Tian      | China   | Kaifeng     |   31    |
  1. To extract the first 40% of records, the statement is as follows:
SELECT TOP 40 PERCENT * FROM Customers;

The result is as follows:

+----------+----------------+---------+-------------+---------+
|CustomerId|CustomerName    | Country |   City      |   Age   |
|00001     |Muller Cheng    | China   | Shanghai    |   25    |
|00002     |Kevin Durant    | USA     | Phoenix     |   34    |
|00003     |Libin Tian      | China   | Kaifeng     |   31    |
|00004     |Junye  Li       | China   | Guangzhou   |   32    |

Precautions

  1. The specific syntax and usage of the TOP clause may vary depending on the database management system, please consult the corresponding documentation for accurate syntax rules.
  2. When the same sorting value exists, the return result of the TOP clause may not be uniquely determined. Therefore, in scenarios where precise sorting is required, it is recommended to use appropriate sorting rules.
  3. The TOP clause is usually used at the beginning of a query statement to ensure that the number of records returned meets the requirements.

Summarize

SQL TOP clause is a very useful feature that allows us to easily extract a specified number of top data records from the database. With appropriate syntax and techniques, we can achieve on-demand data extraction, paging queries, and more complex result set operations. Understanding and mastering the use of TOP clauses will enable us to operate the database more flexibly and efficiently in practical applications.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/2301_78276982/article/details/135410690