How to use several top clauses in a database __ database

Source: Internet
Author: User

use of top statements in different databases

Syntax for SQL Server:

SELECT Top number|percentCOLUMN_NAME (s) from table_name

The SQL SELECT top in MySQL and Oracle is equivalent.

MySQL Syntax:

SELECT column_name (s) from table_name LIMIT Number

Example:

SELECT * from Persons LIMIT 5

Oracle Syntax:

SELECT column_name (s) from table_name WHERE rownum <=Number

Example:

SELECT * from Persons WHERE rownum <= 5

The original table (in the example):

Persons table:

Address City
Id LastName FirstName
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing
4 Obama Barack Pennsylvania Avenue Washington

SQL Top Instance

Now we want to select the first two records from the "Persons" table above.

We can use the following SELECT statement:

SELECT Top 2 * from Persons

Results:

Address City
Id LastName FirstName
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York

SQL Top PERCENT instance

Now, we want to select 50% of the records from the "Persons" table above.

We can use the following SELECT statement:

SELECT Top PERCENT * from Persons

Results:

Address City
Id LastName FirstName
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York


Add:

Limit can implement top N query, also can implement m to N ( a paragraph) of the record query, the specific syntax is as follows:

LIMIT Offset, Recnum
Where offset is started from the first few (m+1) Records, Recnum is the number of record bars returned. Cases:

Limit 2, 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.