LIMIT Clause
In the previous section, we discussed how to retrieve records from a
database using the SELECT statement. For all those queries, the
resulting output was all the records available in the table. This can
become problematic if you have a table with millions or even billions of
rows especially when you just need a subset of that information for
validation. Here is where the LIMIT clause becomes useful.
The LIMIT clause returns a sample of $n$ rows
of the records that you are querying for. If you want to return 10
values, you would specify 10 when applying the LIMIT clause.
Let's demonstrate this in an example. Below is the result of a query
that returns all the records available in the customers_new table.
select *
from customers_new
In this example, we have all the data retrieved as output. However,
suppose we wanted to only see a sample of 5 rows of the output. We can
write a query with the limit clause as seen below to return a sample of
5 rows.
select *
from customers_new
limit 5
What happers if we set LIMIT at value 7? Below is the query and the
resulting output.
select *
from customers_new
limit 7
Limits are very useful in returning a sample of data of the query that
you are running. Data analysis using complex queries are often written
iteratively to achieve a specific output and limit clause is very useful
invalidating the results without returning all the records which can be
computationally expansive.