MariaDB FOUND_ROWS() Function

In MariaDB, FOUND_ROWS() is a built-in function that returns the total number of rows for the last query that needed to count rows.

The MariaDB FOUND_ROWS() function needs to be used in conjunction with the SQL_CALC_FOUND_ROWS keyword, otherwise it will return the number of rows of the previous query.

If you use a LIMIT clause limit the number of rows returned, and you want to know the total number of rows if the statement did not include the LIMIT clause, you can use the SQL_CALC_FOUND_ROWS keyword and the FOUND_ROWS() function. This avoids running queries twice.

MariaDB FOUND_ROWS() Syntax

Here is the syntax of the MariaDB FOUND_ROWS() function:

FOUND_ROWS()

Parameters

The MariaDB FOUND_ROWS() function do not require any parameters.

Return value

The MariaDB FOUND_ROWS() function returns the total number of rows for the last query that needed to count the number of rows.

If you need to get the total number of rows for a query, you need to include the SQL_CALC_FOUND_ROWS keyword in the query.

MariaDB FOUND_ROWS() Examples

The following example uses the film table from the Sakila sample database.

This statement uses the LIMIT clause to display 5 rows from the film table.

SELECT SQL_CALC_FOUND_ROWS film_id, title
FROM film
LIMIT 5;

Note that the above statement contains the SQL_CALC_FOUND_ROWS keyword.

Output:

+---------+------------------+
| film_id | title            |
+---------+------------------+
|       1 | ACADEMY DINOSAUR |
|       2 | ACE GOLDFINGER   |
|       3 | ADAPTATION HOLES |
|       4 | AFFAIR PREJUDICE |
|       5 | AFRICAN EGG      |
+---------+------------------+

If you want to know how many rows the query above returns without the LIMIT clause, use the FOUND_ROWS() function:

SELECT FOUND_ROWS();

Output:

+--------------+
| FOUND_ROWS() |
+--------------+
|         1000 |
+--------------+

You can use the COUNT() function to verify it:

SELECT COUNT(*)
FROM film;

Output:

+----------+
| COUNT(*) |
+----------+
|     1000 |
+----------+

If without the SQL_CALC_FOUND_ROWS keyword, FOUND_ROWS() returns the row count of the previous query.

SELECT film_id, title
FROM film
LIMIT 5;

Note that there is no SQL_CALC_FOUND_ROWS keyword.

Output:

+---------+------------------+
| film_id | title            |
+---------+------------------+
|       1 | ACADEMY DINOSAUR |
|       2 | ACE GOLDFINGER   |
|       3 | ADAPTATION HOLES |
|       4 | AFFAIR PREJUDICE |
|       5 | AFRICAN EGG      |
+---------+------------------+

Then look at the return value of the FOUND_ROWS() function:

SELECT FOUND_ROWS();

Output:

+--------------+
| FOUND_ROWS() |
+--------------+
|            5 |
+--------------+

Now, the FOUND_ROWS() function returns 5 instead 1000.

Conclusion

In MariaDB, FOUND_ROWS() is a built-in function that returns the total number of rows for the last query that needed to count rows.