LIMIT / FETCH (2024)

Categories:

Query syntax

Constrains the maximum number of rows returned by a statement or subquery. Both LIMIT (PostgreSQL syntax) and FETCH (ANSI syntax) are supported, and produce the same result.

See also:

TOP <n>

Syntax

PostgreSQL syntax

SELECT ...FROM ...[ ORDER BY ... ]LIMIT <count> [ OFFSET <start> ][ ... ]

Copy

ANSI syntax

SELECT ...FROM ...[ ORDER BY ... ][ OFFSET <start> ] [ { ROW | ROWS } ] FETCH [ { FIRST | NEXT } ] <count> [ { ROW | ROWS } ] [ ONLY ][ ... ]

Copy

Parameters

count

The number of rows returned. Must be a non-negative integer constant.

The values NULL, empty string (''), and $$$$ are also accepted and are treated as“unlimited”; this is useful primarily for connectors and drivers (such as the JDBC driver) if theyreceive an incomplete parameter list when dynamically binding parameters to a statement.

OFFSET start

The row number after which the limited/fetched rows are returned. Must be a non-negative integer constant.

If OFFSET is omitted, the output starts from the first row in the result set.

The values NULL, empty string ('') and $$$$ are also accepted and are treated as 0(i.e. do not skip any rows); this is useful primarily for connectors and drivers (such as the JDBCdriver) if they receive an incomplete parameter list when dynamically binding parameters to a statement.

ONLY

Optional keyword that does not affect the output. It is used for emphasis to thehuman reader.

Usage notes

  • An ORDER BY clause is not required; however, without an ORDER BY clause, the results are non-deterministic because query results are not necessarily in any particular order. To control the results returned, use an ORDER BY clause.

  • TOP n and LIMIT count are equivalent.

Examples

The following examples show the effect of LIMIT. For simplicity, thesequeries omit the ORDER BY clause and assume that the output order isalways the same as shown by the first query. Real-world queries shouldinclude ORDER BY.

select c1 from testtable;+------+| C1 ||------|| 1 || 2 || 3 || 20 || 19 || 18 || 1 || 2 || 3 || 4 || NULL || 30 || NULL |+------+select c1 from testtable limit 3 offset 3;+----+| C1 ||----|| 20 || 19 || 18 |+----+select c1 from testtable order by c1;+------+| C1 ||------|| 1 || 1 || 2 || 2 || 3 || 3 || 4 || 18 || 19 || 20 || 30 || NULL || NULL |+------+select c1 from testtable order by c1 limit 3 offset 3;+----+| ID ||----|| 2 || 3 || 3 |+----+

The following example demonstrates the use of NULLs to indicate

  • No limit to the number of rows.

  • Start at row 1 (do not skip any rows)

    CREATE TABLE demo1 (i INTEGER);INSERT INTO demo1 (i) VALUES (1), (2);

    Copy

    SELECT * FROM demo1 ORDER BY i LIMIT NULL OFFSET NULL;+---+| I ||---|| 1 || 2 |+---+

    Copy

    SELECT * FROM demo1 ORDER BY i LIMIT '' OFFSET '';+---+| I ||---|| 1 || 2 |+---+

    Copy

    SELECT * FROM demo1 ORDER BY i LIMIT $$$$ OFFSET $$$$;+---+| I ||---|| 1 || 2 |+---+

    Copy

LIMIT / FETCH (2024)
Top Articles
NEFT vs RTGS vs IMPS: Key differences, Advantages & Benefits
Unstoppable Domains Reaches Unicorn Status After Latest Raise
jazmen00 x & jazmen00 mega| Discover
El Paso Pet Craigslist
Maria Dolores Franziska Kolowrat Krakowská
Toyota Campers For Sale Craigslist
Hertz Car Rental Partnership | Uber
Kristine Leahy Spouse
5 Bijwerkingen van zwemmen in een zwembad met te veel chloor - Bereik uw gezondheidsdoelen met praktische hulpmiddelen voor eten en fitness, deskundige bronnen en een betrokken gemeenschap.
Call Follower Osrs
Gunshots, panic and then fury - BBC correspondent's account of Trump shooting
Hallowed Sepulchre Instances &amp; More
Ohiohealth Esource Employee Login
Which Is A Popular Southern Hemisphere Destination Microsoft Rewards
Ella Eats
Colts seventh rotation of thin secondary raises concerns on roster evaluation
charleston cars & trucks - by owner - craigslist
Who called you from 6466062860 (+16466062860) ?
How To Cut Eelgrass Grounded
Katherine Croan Ewald
Marvon McCray Update: Did He Pass Away Or Is He Still Alive?
Odfl4Us Driver Login
Golden Abyss - Chapter 5 - Lunar_Angel
Keck Healthstream
Empire Visionworks The Crossings Clifton Park Photos
Azur Lane High Efficiency Combat Logistics Plan
Rubber Ducks Akron Score
Bill Remini Obituary
Naya Padkar Gujarati News Paper
Maine Racer Swap And Sell
J&R Cycle Villa Park
Rvtrader Com Florida
Www.craigslist.com Syracuse Ny
RFK Jr., in Glendale, says he's under investigation for 'collecting a whale specimen'
Adecco Check Stubs
Ma Scratch Tickets Codes
Rogers Centre is getting a $300M reno. Here's what the Blue Jays ballpark will look like | CBC News
Louisville Volleyball Team Leaks
Wsbtv Fish And Game Report
Craigslist Mexicali Cars And Trucks - By Owner
Let's co-sleep on it: How I became the mom I swore I'd never be
The All-New MyUMobile App - Support | U Mobile
8776725837
Mychart University Of Iowa Hospital
Toomics - Die unendliche Welt der Comics online
2294141287
The Machine 2023 Showtimes Near Roxy Lebanon
Craigslist Chautauqua Ny
Windy Bee Favor
Fredatmcd.read.inkling.com
Craigslist Cars And Trucks For Sale By Owner Indianapolis
Optimal Perks Rs3
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 5762

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.