Difference between Top and Limit Keyword in SQL

后端 未结 6 1402
你的背包
你的背包 2020-11-27 04:41

A quick Question. Suppose I have the following two queries:

SELECT TOP 2 * FROM Persons;

and

SELECT * FROM Persons limit 2;         


        
相关标签:
6条回答
  • 2020-11-27 05:10

    There is no difference. The TOP and LIMIT keywords function identically, and will return the same thing.

    0 讨论(0)
  • 2020-11-27 05:14

    If you are using SQL Server use TOP if you are using MySQL or Postgres use Limit!

    AFAIK there is no product that currently supports both. Here's one list of current implementations and here's another (covers more products but in less detail)

    0 讨论(0)
  • 2020-11-27 05:17

    As stated in my comment for Martin Smith's answer above, there are products that support both, LIMIT and TOP (as you can see here). The difference is that TOP only selects the first n records, but LIMIT allows the definition of an offset to retrieve a specific range of records:

    SELECT * FROM ... LIMIT 5 OFFSET 10
    

    This statement selects the first 5 records, after skipping 10 records and this isn't possible with TOP.

    The example I posted is only checked against the DBS I linked above. I didn't check a SQL standard, because of a lack of time.

    0 讨论(0)
  • 2020-11-27 05:17

    limit works on MySQL and PostgreSQL, top works on SQL Server, rownum works on Oracle.

    0 讨论(0)
  • 2020-11-27 05:19

    one big mistake, LIMIT is slowly because select is return full and then database server return only limited data. When it is posible used to TOP.

    0 讨论(0)
  • 2020-11-27 05:21

    TOP & LIMIT both work on amazon Redshift

    0 讨论(0)
提交回复
热议问题