Aggregates of the query builder (from(), db()->from(), class yf_db_query_builder_driver). Each returns the value, or with $as_sql = true the SQL it would run. The examples render the SQL on an offline copy of db() ($db() below): nothing runs. Every method of the builder: API reference / yf_db_query_builder_driver; build a query and see its SQL for MySQL and PostgreSQL: demo db / sql_preview.

count

count($id = '*', $as_sql = false): the number of rows of the query.

Result
SELECT COUNT(*) FROM `y_table`
$db()->from('table')->count('*', true)

max

max($pk = null, $as_sql = false): MAX() of a column, the primary key by default.

Result
SELECT MAX(`id`) FROM `y_table`
$db()->from('table')->max('id', true)

min

min($pk = null, $as_sql = false): MIN() of a column.

Result
SELECT MIN(`id`) FROM `y_table`
$db()->from('table')->min('id', true)

avg

avg($pk = null, $as_sql = false): AVG() of a column.

Result
SELECT AVG(`price`) FROM `y_table`
$db()->from('table')->avg('price', true)

sum

sum($pk = null, $as_sql = false): SUM() of a column.

Result
SELECT SUM(`price`) FROM `y_table`
$db()->from('table')->sum('price', true)

first, last

first($select = null, $use_cache = false) / last(...): the first / last row of a model query (ordered by its primary key), from the cache with $use_cache. On a plain from() first() adds LIMIT 1 to the query as it is; last() runs an order_by() of plain columns reversed with LIMIT 1, and a query without an order as a COUNT(*) and then LIMIT 1 OFFSET count-1, so it reads one row (before 2026-11-15 it added LIMIT 1 and returned the first row). A query with a limit(), a union or an order by expression is read whole and its last row returned. They run the query, so they have no example here:

$newest = from('news')->where('active', 1)->order_by('id desc')->first();