DATA with BARAA

SQL LIMIT

In this tutorial, you will learn how to retrieve a fixed number of rows using the SQL LIMIT  and TOP keywords.

Syntax

The basic syntax of the LIMIT clause to sort the data returned by a query, can be given with:

				
					SELECT column_names
FROM table_name
LIMIT number
				
			
				
					SELECT TOP number column_names
FROM table_name
				
			

Examples

To understand the LIMIT statement in a better way, let’s look at the following customers table in our tutorial database:

Customers
customer_id first_name last_name country score
1 Maria Cramer Germany 350
2 John Steel USA 900
3 Georg Pipps UK 750
4 Martin MĂ¼ller Germany 500
5 Peter Franken USA NULL

Now, let’s check out some examples that demonstrate how it actually works.

We have the following task be to solve using SQL statements

Retrieve only two customers

The following SQL statement will returns only 2 customers from the database

				
					SELECT *
FROM customers
LIMIT 2
				
			

After executing the above query, you’ll get the result set something like this:

customer_id first_name last_name country score
1 Maria Cramer Germany 350
2 John Steel USA 900

As you can see the output contains only two customers from the table customers.

.
Share it !