SQL SELECT TOP Statement

Source: Internet
Author: User
Tags sql create database sql primary key

The SELECT TOP clause is used to specify the number of records to return.

The SELECT TOP clause is useful for large tables with thousands of records.

Note: Not all database systems support the SELECT TOP clause.

SQL server/ms Access Syntax

SELECT TOP number|percent column_name (s) from table_name;

The SQL SELECT TOP in MySQL and Oracle is equivalent

MySQL syntax

SELECT column_name (s) from table_namelimit number;

Instance

SELECT *from Personslimit 5;

Oracle syntax

SELECT column_name (s) from Table_namewhere ROWNUM <= number;

Instance

SELECT *from personswhere ROWNUM <=5;

Demo Database

In this tutorial, we will use the well-known Northwind sample database.

The following is the data selected from the "Customers" table:

City
CustomerID CustomerName ContactName Address PostalCode Country
1
Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. De la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4
Around the Horn Thomas Hardy Hanover Sq. London WA1 1DP UK
5 Berglunds Snabbköp Christina Berglund Berguvsvägen 8 Across S-958 22 Sweden

SQL SELECT TOP instance

The following SQL statement selects the first two records from the "Customers" table:

SELECT TOP 2 * from Customers;

SQL SELECT TOP PERCENT instance

The following SQL statement selects the previous 50% records from the "Customers" table:

SELECT TOP PERCENT * from Customers;

  

Original address: http://www.manongjc.com/sql/sql_top.html

SQL-related learning materials:

    • SQL Advanced
    • SQL SELECT TOP Statement
    • SQL like statement
    • SQL wildcard characters
    • SQL in operator
    • SQL between and statements
    • SQL alias (Aliases)
    • SQL Join Connection
    • SQL INNER JOIN
    • SQL LEFT Join
    • SQL Right Join
    • SQL Full JOIN
    • SQL UNION
    • SQL SELECT into replicate table data
    • INSERT into SELECT to copy table data to another table
    • SQL CREATE DATABASE
    • SQL CREATE TABLE Statement
    • SQL constraints (Constraints)
    • SQL not NULL constraint
    • SQL UNIQUE constraints
    • SQL PRIMARY KEY Constraint
    • SQL FOREIGN KEY Constraint
    • SQL CHECK constraints
    • SQL DEFAULT constraints
    • SQL CREATE INDEX Statement
    • SQL DROP
    • SQL ALTER
    • SQL AUTO INCREMENT Tutorial
    • SQL View (views)
    • SQL Dates Date function
    • SQL null and NOT NULL
    • SQL ISNULL (), NVL (), Ifnull (), and COALESCE () functions
    • SQL Common data types
    • SQL for data from various databases

SQL SELECT TOP Statement

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.