Skip to main content
Log inGet a demo
Back to SQL Dictionary
Basic statements and clauses

SQL LIMIT

What is the SQL LIMIT Clause?

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.

When to Use SQL LIMIT?

You would use the SQL LIMIT clause when you need to:

  1. Retrieve only a specific number of rows from a result set.
  2. Implement pagination for displaying data in chunks.
  3. Optimize query performance by reducing the amount of data transferred.

Syntax of SQL LIMIT:

The basic syntax for the SQL LIMIT clause is as follows:

SELECT column1, column2, ...
FROM table
LIMIT number_of_rows;
  • column1, column2, ...: The columns you want to retrieve.
  • table: The table from which you want to retrieve data.
  • LIMIT number_of_rows: The maximum number of rows to return.

Parameter Values:

  • column1, column2, ...: Column names separated by commas or * to select all columns.
  • table: The name of the table from which data is to be retrieved.
  • number_of_rows: An integer that specifies the maximum number of rows to be returned.

Example Query:

Suppose you have a table named "products" with columns: product_id, product_name, price, and category. To select the first three products in the "Electronics" category, you would use the following query:

SELECT product_name, price
FROM products
WHERE category = 'Electronics'
LIMIT 3;

Example Table Response:

Assuming the "products" table contains the following data:

product_idproduct_namepricecategory
1Laptop800Electronics
2Smartphone600Electronics
3TV900Electronics
4Refrigerator1200Appliances
5Microwave150Appliances

The result of the query would be:

product_nameprice
Laptop800
Smartphone600
TV900

Use Cases:

SQL LIMIT is valuable in various situations, including:

  1. Implementing pagination in web applications to display a limited number of results per page.
  2. Reducing the load on the database by fetching only the necessary data.
  3. Quickly retrieving a sample of data for testing or analysis.
  4. Controlling the size of query results to prevent excessive memory usage.

SQL LIMIT in Different SQL Languages:

The SQL LIMIT clause is not universal, and its usage may vary between different SQL database systems. Here are some examples of similar clauses in popular SQL languages:

  • MySQL, PostgreSQL, SQLite: Use LIMIT.
  • Oracle Database: Use ROWNUM with a subquery.
  • Microsoft SQL Server (T-SQL): Use TOP.

The specific syntax and behavior of LIMIT-like clauses can differ, so be sure to consult the documentation for your database system to use the appropriate syntax.

Related

SQL SELECT

SQL WHERE

SQL ORDER BY

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes