FOUND_ROWS() Function SQL

The FOUND_ROWS() function is paired up with the SQL_CALC_FOUND_ROWS phrase as a way to find out the total number of rows that fulfill the WHERE requirement even when there is a LIMIT on the returned rows.

The function and the phrase are used in two different statements. SQL will keep track of how many rows were gone through every time a SELECT statement is performed until the next SELECT query is made. Normally SQL will only remember the number of rows returned. However, by specifying SQL_CALC_FOUND_ROWS the database will instead remember the number of rows that fulfill the WHERE requirement. For example:

prop_idnamepricegroup
1Baltic Ave60purple
2Mediterranean Ave60purple
3Reading Railroad200rr
4Oriental Ave100light blue
5Vermont Ave100light blue
6Connecticut Ave120light blue

If we want to know how many of the monopoly properties in our table have a price less than 190, but we only want to return the first two, we can accomplish this with two statements. First:

SELECT SQL_CALC_FOUND_ROWS *
FROM monopoly_prop
WHERE price < 190 LIMIT 2

We will get back "Baltic and Mediterranean Avenues". The SQL_CALC_FOUND_ROWS remembers that SQL went through five properties. To get this information, we need to make an additional separate query:

SELECT FOUND_ROWS()

We will find out that five rows were available to fulfill the WHERE clause. Without the SQL_CALC_FOUND_ROWS the FOUND_ROWS() function would have given us "2" as a result. COUNT() is a similar function.

< EXPORT SET Function SQL | HEX Function SQL >


Send notes in disappearing ink!

Interesting Pages