mariadb-window-functions

Write MariaDB window function queries for ranking, navigation, and percentile calculations.

28|115|Updated Jan 28, 2025
One-click install
npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-window-functions-mariadb-corporation
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mariadb-window-functions
Source: https://github.com/mariadb-corporation/mariadb-docs/tree/main/agent-skills/granular/functions/mariadb-window-functions
Command: npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-window-functions-mariadb-corporation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing correct window function SQL in MariaDB is error-prone: window functions are rejected in WHERE/GROUP BY/HAVING, default frames silently change SUM and LAST_VALUE semantics, and ordered-set functions like PERCENTILE_CONT reject ORDER BY inside OVER. This Skill provides a catalog of MariaDB's window functions plus the common mistakes LLMs make, so generated SQL is correct on the first try. ## Core Features & Use Cases - Function Catalog: Signatures and one-line semantics for ROW_NUMBER, RANK, DENSE_RANK, PERCENT_RANK, CUME_DIST, NTILE, LAG, LEAD, FIRST_VALUE, LAST_VALUE, NTH_VALUE, and MEDIAN, plus OVER, named windows, and ROWS/RANGE frames. - Pitfall Table: Concrete rewrites for frequent errors, such as filtering window results via subqueries, choosing between RANK and DENSE_RANK for top-N-per-group, and fixing LAST_VALUE under the default frame. - Use Case: A user asks for the top 2 orders per customer by amount. The Skill guides producing a CTE with ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY amount DESC) filtered in the outer query, avoiding the invalid WHERE-clause placement. ## Quick Start Write a MariaDB query that ranks each customer's orders by amount and returns only the two most recent orders per customer using a window function.

Frequently Asked Questions about mariadb-window-functions

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I get the top N rows per group in MariaDB?▼

Compute ROW_NUMBER() OVER (PARTITION BY group_col ORDER BY sort_col DESC) in a subquery or CTE, then filter rn <= N in the outer query. Window functions cannot appear in WHERE, GROUP BY, or HAVING in MariaDB.

What is the difference between ROW_NUMBER, RANK, and DENSE_RANK in MariaDB?▼

ROW_NUMBER gives distinct 1..N values, arbitrarily breaking ties. RANK gives ties the same value and skips subsequent ranks (1,2,2,4). DENSE_RANK gives ties the same value with no gaps (1,2,2,3), which matters for tie-heavy top-N queries.

Why does MariaDB reject my window function in the WHERE clause?▼

MariaDB evaluates window functions after WHERE, GROUP BY, and HAVING, and only allows them in the SELECT list and ORDER BY. Wrap the window computation in a subquery or CTE and filter on its alias in the outer query.

Why does LAST_VALUE return the current row's value in MariaDB?▼

With ORDER BY and no explicit frame, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so the last row in frame is the current row. Use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to get the partition's true last value.

Does MariaDB support PERCENTILE_CONT and MEDIAN as window functions?▼

Yes, but ordering goes in WITHIN GROUP (ORDER BY expr), not in OVER. The OVER clause for MEDIAN, PERCENTILE_CONT, and PERCENTILE_DISC accepts PARTITION BY only; putting ORDER BY inside OVER is a parse error.

What window frame features are not supported in MariaDB?▼

MariaDB does not support GROUPS frames, NULLS FIRST/NULLS LAST ordering, or EXCLUDE frame exclusion (rejected at runtime). RANGE frames with numeric or interval offsets require a single-column ORDER BY and do not support DATE/DATETIME arithmetic.