SQL Limit: A Beginner's Guide (2024)

The SQL LIMIT statement restricts how many rows a query returns. A LIMIT statement appears at the end of a query, after any ORDER BY statements. You can start a LIMIT statement at a particular row using the offset argument.

When you’re working in SQL, you may only want to retrieve a specific number of rows from a query.

Find your bootcamp match

GET MATCHED

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunitiesfrom Career Karma by telephone, text message, and email.

X

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

For example, you may want to retrieve the top three employees based on the number of “employee of the month” awards they have earned. This is easier to read than a list of all employees and how many “employee of the month” awards they have earned.

There is a built-in SQL function that allows you to perform this action: SQL LIMIT. Limit allows you to limit the number of records a query to a certain amount.

In this tutorial, we are going to discuss how to use the SQL LIMIT command. We’ll also discuss how to use TOP, the equivalent of LIMIT on SQL Server instances.

Query Refresher

To retrieve information from a database, you have to write SQL queries. Queries almost always begin with the SELECT statement. We use this statement to tell the database which columns we want to retrieve. Queries often include the FROM term, which is used to state which table the statement will get its data from.

Here is the syntax for an SQL query:

SELECT column_name FROM table_name WHERE conditions_apply;

Here is an example of an SQL query that will return a list of the names of all employees in the employees table:

SELECT name FROM employees;

Our query returns the following:

name
Luke
Mike
Hannah
Geoff
Alexis
Emma
Jonah
Adam

(8 rows)

What is SQL LIMIT?

The SQL LIMIT clause restricts how many rows are returned from a query. The syntax for the LIMIT clause is: SELECT * FROM table LIMIT X;. X represents how many records you want to retrieve.

For example, you can use the LIMIT clause to retrieve the top five players on a leaderboard. Or, you could use this clause the top five customers who have ordered the most products.

Here’s the syntax for a query that uses the LIMIT clause:

SELECT column_name FROM table_name LIMIT number_to_limit;

The LIMIT clause is only compatible with the SQL SELECT statement. You cannot use a LIMIT clause on an SQL UPDATE statement, for instance. Or you could not use a LIMIT clause on an SQL DROP or SQL ALTER TABLE statement.

Your LIMIT number must be positive. Say you want to retrieve records from the bottom of the list. You should use the SQL ORDER BY statement to order them in descending order. Then, you should use a LIMIT statement:

SELECT * FROM table ORDER BY column DESC LIMIT X;

This is the syntax for ordering a table in descending order and limiting the output of the query. We’ve used an ORDER BY clause to retrieve only the first X records.

SQL LIMIT Rows Example

Let’s use an example to illustrate the SQL LIMIT clause in action. Say that we want to get a list of the names of the top three longest-serving employees within our organization.

We also want to retrieve the dates they were hired. We could use the following query to retrieve that information:

SELECT name, hired_date FROM employees ORDER BY hired_date DESC LIMIT 3;

Our query returns the following:

namehired_date
Alexis2014-04-01
Geoff2012-03-17
Hannah2011-09-30

(3 rows)

Our query has returned the top three longest-serving employees by the date on which they were hired.

Offset the SQL LIMIT Clause

If you want to offset the LIMIT clause—change the starting point—you can specify a second parameter. Here is the syntax for an SQL LIMIT query that offsets a query:

SELECT column_name FROM table_name LIMIT starting_point, rows_to_return;

The starting point is the offset for the query (where your results will start). The “rows_to_return” is how many rows you want your query to retrieve.

Let’s say that we want to get the name of the fifth-highest paid employee. We could use the LIMIT clause to specify that we want to get only one record, which should be the fifth highest-paid employee. Here’s an example of such a query:

SELECT name FROM employees ORDER BY salary DESC LIMIT 4, 1;

We specify that we want to get the item which comes fifth in our list—remember, the first row is. We only want to get one record. Here’s the result of our query:

name
Alexis

(1 row)

The offset clause only works in MySQL. If you want to run an offset operation in PostgreSQL, you’ll need to use the OFFSET operator like so:

SQL Limit: A Beginner's Guide (1)

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

SELECT name FROM employees ORDER BY salary DESC OFFSET 4 LIMIT 1;

SQL TOP

In SQL Server and MS Access the SQL TOP clause is used to limit the number of rows returned by a query. Here is the syntax for an SQL query that uses TOP:

SELECT TOP number column_names FROM table_name;

Here’s an example of a query that will return the top two highest-paid employees from the employees table:

SELECT TOP 2 * FROM employees ORDER BY salary DESC;

Our query returns the following:

name
Jonah
Emma

(2 rows)

The TOP clause can be followed by the PERCENT keyword, which retrieves the percentage of rows instead of the number of fixed rows. So, if you want to get the top 25 percent of employees based on their salaries, you could use this query:

SELECT TOP 25 PERCENT * FROM employeesORDER BY salary DESC;

Our query on the database with eight records returns the following:

name
Jonah
Emma

(2 rows)

Conclusion

You can use the SQL LIMIT operator (or TOP in SQL Server and MS Access) to limit the number of rows a query returns. This is useful if you only need to see the top or bottom entries from a query.

For example, you may want to get the records of the five employees with the highest salaries. In this tutorial, we discussed how queries are structured in SQL, and how to use the LIMIT operator.

Do you want to learn more about SQL? Read our complete How to Learn SQL guide for expert advice and tips, and for guidance on online courses.

SQL Limit: A Beginner's Guide (2024)

FAQs

How do I limit the number of responses in SQL? ›

To to limit the number of rows in a MySQL result set, use the LIMIT clause. This clause takes two arguments: the number of rows to return, and an optional offset.

How can you limit the number of results returned by a query? ›

SQL Server provides a simple way to limit the number of rows returned by a query using the TOP keyword. The TOP keyword allows you to specify the number of rows you want to retrieve from a query.

How to limit SQL results to 100? ›

In Mode's Query Editor, you may have noticed the checkbox next to "Run" that says "Limit 100". As you might expect, the limit restricts how many rows the SQL query returns. The default value is 100; when this box is checked, it's telling the database to only return the first 100 rows of the query.

How to limit values in SQL? ›

Syntax. SELECT * FROM table_name ORDER BY column_name LIMIT X,Y; The first value X is the offset value and the second value Y is the LIMIT value.

How do you limit the result set in SQL? ›

To limit rows in the result set, use ORDER BY with the optional OFFSET and FETCH clauses. First, the query sorts the rows ( ORDER BY ). You then tell SQL Server which should be the first row in the result set ( OFFSET... ROWS ) and how many rows to return ( FETCH...

How to limit the number of outputs in SQL? ›

The SQL LIMIT clause constrains the number of rows returned by a SELECT statement. For Microsoft databases like SQL Server or MSAccess, you can use the SELECT TOP statement to limit your results, which is Microsoft's proprietary equivalent to the SELECT LIMIT statement.

Which query expression is used to limit the number of results? ›

To restrict the result, we can use the LIMIT clause in SQL. The LIMIT clause in the SQL is used to restrict the number of records. Simply put, it sets an upper limit on the number of tuples returned for any given query. The limit expression must be a non-negative integer.

What SQL clause limits the number of results? ›

The MySQL LIMIT Clause

The LIMIT clause is used to specify the number of records to return. The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

What allows you to limit the number or percentage of rows returned by a query? ›

The TOP clause allows us to limit the result set of the queries according to the number of rows or the percentage of rows.

How do I LIMIT SQL usage? ›

Use SQL Server Management Studio
  1. In Object Explorer, right-click a server and select Properties.
  2. Select the Memory page of the Server Properties window. ...
  3. In Server memory options, enter desired numbers for Minimum server memory and Maximum server memory.
Aug 21, 2023

What does LIMIT 1:1 do in SQL? ›

In this syntax, the LIMIT n-1, 1 clause returns 1 row that starts at the row n. For example, the following query returns the employee information who has the second-highest income: SELECT emp_name, city, income FROM employees.

What is the alternative to LIMIT in SQL? ›

The FETCH clause is basically Oracle's equivalent of the LIMIT clause that's seen in many other SQL databases. It's implemented specifically as a row limiting clause. It enables us to limit the output of our queries to a specific number or percentage of rows.

How do I resolve too many values in SQL? ›

Check the number of items in each set and change the SQL statement to make them equal.

How do you max a value in SQL? ›

The MAX function in SQL is designed to identify the maximum value of a specified column. This function is particularly useful in retrieving the highest value from sets of data. The syntax for utilising the MAX function is as follows: SELECT MAX(column_1) FROM table.

How do you set size limit in SQL? ›

SQL limits
String LimitsDb2 for i Limit
Maximum length of BINARY (in bytes)327656
Maximum length of VARBINARY (in bytes)327396
Maximum length of BLOB (in bytes)2 147 483 647
Maximum length of serialized XML (in bytes)2 147 483 647
19 more rows

What is the limit response in SQL? ›

SQL LIMIT is a SQL clause used to restrict the number of rows returned by a query. It is particularly useful when you want to control the size of your result set to avoid excessive data retrieval. The LIMIT clause is often used in conjunction with the SELECT statement.

Can we limit the number of rows in SQL? ›

We can limit the number of records or display only the same number of records which is there mentioned in the LIMIT clause. This SQL statement cannot be used in all RDBMS systems like SQL Server, Oracle, etc. but all these RDBMS systems have their query statement to limit the rows returned in the output result.

What method is used to limit the number records returned from an SQL query? ›

You can use the TOP() clause in the SELECT statement.

Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 6277

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.