0
0
SQLquery~5 mins

LIMIT clause behavior in SQL

Choose your learning style9 modes available
Introduction
The LIMIT clause helps you get only a certain number of rows from a big list. It makes it easier to see or work with just a small part of your data.
When you want to see the first 5 customers from a large customer list.
When you only need the top 10 highest scores from a game leaderboard.
When testing queries and you want to check a few rows instead of the whole table.
When showing search results page by page, like showing 20 results at a time.
Syntax
SQL
SELECT column1, column2 FROM table_name LIMIT number_of_rows;
The number_of_rows tells how many rows you want to get.
LIMIT is usually placed at the end of the SELECT statement.
Examples
Gets the first 3 rows from the employees table.
SQL
SELECT * FROM employees LIMIT 3;
Gets the first 5 users with their name and age.
SQL
SELECT name, age FROM users LIMIT 5;
Gets only the first product name from the products table.
SQL
SELECT product_name FROM products LIMIT 1;
Sample Program
This creates a small fruits table, adds 4 fruits, then selects only the first 2 rows using LIMIT.
SQL
CREATE TABLE fruits (id INT, name VARCHAR(20));
INSERT INTO fruits VALUES (1, 'Apple'), (2, 'Banana'), (3, 'Cherry'), (4, 'Date');
SELECT * FROM fruits LIMIT 2;
OutputSuccess
Important Notes
LIMIT does not guarantee order unless you use ORDER BY before it.
If you want to skip some rows and then get rows, use OFFSET with LIMIT.
Some SQL databases may use different syntax for LIMIT (like TOP in SQL Server).
Summary
LIMIT helps get a small number of rows from a query result.
It is useful for previewing data or paginating results.
Always use ORDER BY if you want consistent row order with LIMIT.