Using the SQL Limit Keyword (2024)

Jul 30, 2019 by Robert Gravelle

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. However, for most relational databases (DBMSes), including MySQL/MariaDB, PostgreSQL, and Oracle, the SQL LIMIT clause can solve several problems. In today's blog, we'll explore a few of these, using Navicat for PostgreSQL.

In many production and test databases, table sizes routinely reach millions of rows and have dozens of columns. For that reason, it's never a good idea to run SELECT * queries against your database(s). Keeping the results down to one hundred or one thousand helps keep result sets down to a size that's more easily digestible.

Navicat development and administration tools automatically limit result sets by default in order to prevent straining your database server(s). You can see it in action when you open a table. At the bottom of the application window, the SQL that Navicat executed to fetch the table rows is displayed. It ends with the "LIMIT 1000 OFFSET 0", which means that only the first 1000 records are displayed.

Using the SQL Limit Keyword (1)

You can change the default number of records to show or turn off limiting entirely on the RECORDS Options screen:

Using the SQL Limit Keyword (2)

As the name implies, top-N queries are those that attempt to find the top number of records from a result set. This could be top 1, top 3, top 5, top 10, or top [any] number. Some common examples are:

  • Find the top 10 highest paid employees
  • Find the top 20 most profitable customers
  • Find the top 3 users on the system

These queries are hard to do with just an ORDER BY and WHERE clause alone, but not using the LIMIT clause. Here's an example:

Top 5 Unique Job IDs

See Also
LIMIT Clause

Let's say that we wanted to find the top unique Job IDs in a table. Here's a query that does just that:

Using the SQL Limit Keyword (3)

The DISTINCT keyword makes sure that duplicate IDs are removed from the results.

It is possible to locate rows closest to a given date using LIMIT. You just have to compare row dates to the given date, order the results, and limit the results to the number of rows that you'd like to see. Here's a query that returns rows whose creation_date is greater than '2018-01-01':

Using the SQL Limit Keyword (4)

In this case, 2018-01-02 was the closest later date.

The corollary of top N queries are bottom N queries. These are queries that attempt to find the bottom number of records from a result set. We can convert our Top queries into their Bottom equivalents quite easily!

Bottom 5 Unique Job IDs

To return the bottom 5 unique job IDs, all you need to do is remove the DESC modifier in the ORDER BY clause. That will order records in ascending (ASC) order, as is default:

Using the SQL Limit Keyword (5)

Locating the closest rows before a given date is likewise fairly easy. We just need to change the greater than '>' operator to less than '<' and reorder results in descending (DESC) order:

Using the SQL Limit Keyword (6)

In today's blog, we explored a few uses for the LIMIT clause, using Navicat for PostgreSQL. Like to give Navicat for PostgreSQL a try? You can evaluate it for 14 days completely free of charge!

Using the SQL Limit Keyword (2024)

FAQs

How do I limit responses 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.

How do you use limit keyword in SQL? ›

Example - Using LIMIT keyword

For example: SELECT contact_id, last_name, first_name FROM contacts WHERE website = 'TechOnTheNet.com' ORDER BY contact_id DESC LIMIT 5; This SQL SELECT LIMIT example would select the first 5 records from the contacts table where the website is 'TechOnTheNet.com'.

How do I limit the number of results in a SQL query? ›

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 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 do I restrict values in SQL? ›

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

What is the alternative to limit in SQL Server? ›

The LIMIT , SELECT TOP or ROWNUM command is used to specify the number of records to return. Note: SQL Server uses SELECT TOP . MySQL uses LIMIT , and Oracle uses ROWNUM .

Which SQL keyword is used to describe a maximum value? ›

The SQL keyword used to retrieve the maximum value from a column is “MAX.” You can use it in conjunction with the SELECT statement to find the maximum value in a specific column of a table.

Can we use limit with count in SQL? ›

Since the LIMIT clause is executed as the very last step, after the FROM, WHERE, (possibly) GROUP BY and SELECT clauses (in that order), its effect will be to show only some of the count(*) values. Since without a GROUP BY the result would be exactly a single row, the LIMIT would have no effect at all in that case.

What is the difference between top and limit in SQL? ›

Both TOP and LIMIT are used to limit number of records returned from a SELECT query, the difference would be that TOP is implemented in MSSQL, and LIMIT is implemented in MySQL, MariaDB, and PostgreSQL.

How do you SELECT max result in SQL? ›

To fetch the rows that have the Max value for a column for each distinct value of another column in SQL typically involves combining the rows with the help of the GROUP BY clause with aggregate functions such as MAX() which gives the maximum values of a column to achieve the desired results.

How do I restrict query results in Access? ›

You can use query criteria in Access to limit the results based on specific text values. For example the criterion, = "Chicago" shows all items that have the text Chicago.

How to use limit with SQL? ›

The LIMIT in SQL is a clause that enables multi-page outcomes or SQL pagination to be easily coded and is very helpful on large tables. A lot of records can influence success when they are returned. If the question conditions are satisfied for numerous tuples, only a few can be seen at a time.

How do you write a limit query? ›

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 I limit specific rows in SQL? ›

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 restrict data in SQL query? ›

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.

How do you limit rows returned in SQL 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 do I only allow one row in SQL? ›

In order to get a limit of 1 row for the table you should choose a calculated column with a fixed value as id. You need to declare this column as PERSISTED in order to use it as PRIMARY KEY. And that's it.

How do I reduce response time in SQL? ›

Minimizing SQL query response times
  1. Avoid complex join and filter expressions. ...
  2. Reduce explicit or implicit data type conversions. ...
  3. Avoid using SQL expressions to transpose values. ...
  4. Avoid unnecessary outer joins. ...
  5. Make use of constraints on tables in data servers. ...
  6. Use indexes and table organization features.

Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 6699

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.