Optimize SQL queries with LIMIT | Atlassian (2024)

Table of Contents
LIMIT OFFSET

Last modified: December 09, 2019

LIMIT

If want to LIMIT the number of results that are returned you can simply use the LIMIT command with a number of rows to LIMIT by.

SELECT*FROMartistsLIMIT[NumbertoLimitBy];

For example

SELECT*FROMartistsLIMIT3;

This ensures only the first 3 results are returned. Besides returning less results, LIMITing queries can greatly reduce the time they take to run and make your database administrator a lot less angry with you.

Give it a try by fetching yourself the first6rows of theartiststable:

SELECT*FROMartistsLIMIT6;

OFFSET

You can also specify an OFFSET from where to start returning data.

SELECT*FROMartistsLIMIT5OFFSET[Numberofrowstoskip];

Say you want to get 5 artists, but not the first five. You want to get rows 3 through 8. You’ll want to add an OFFSET of 2 to skip the first two rows:

SELECT*FROMartistsLIMIT5OFFSET2;

Here’s a challenge for you. Write a query to fetch the Artists in rows 10 through 20.

Written by:Dave Fowler
Reviewed by:Matt David

Optimize SQL queries with LIMIT | Atlassian (2024)
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5651

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.