Hàm select top 1 from table trong sql năm 2024

CodeLearn is an online platform that helps users to learn, practice coding skills and join the online coding contests.

Links

Learning

Training

Fights

Information

About Us

Terms of Use

Help

Help

Discussion

Powered by CodeLearn © 2024. All Rights Reserved. rev 2/5/2024 5:31:56 PM

The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

Example

Select only the first 3 records of the Customers table:

SELECT TOP 3 * FROM Customers;

Try it Yourself »

Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM.

SQL Server / MS Access Syntax:

SELECT TOP number|percent columnname[s] FROM tablename WHERE condition;

MySQL Syntax:

SELECT columnname[s] FROM tablename WHERE condition LIMIT number;

Oracle 12 Syntax:

SELECT columnname[s] FROM tablename ORDER BY columnname[s] FETCH FIRST number ROWS ONLY;

Older Oracle Syntax:

SELECT columnname[s] FROM tablename WHERE ROWNUM

Chủ Đề