mariadb-control-flow-functions

Documents MariaDB control-flow functions and operators for writing conditional SQL expressions.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing conditional logic in MariaDB SQL is error-prone because agents and developers confuse expression-level functions (IF(), CASE ... END) with stored-program statements (IF ... END IF, CASE ... END CASE), misuse DECODE outside Oracle mode, and hit type-aggregation gotchas in COALESCE. This Skill provides a verified catalog of every MariaDB control-flow function with signatures, semantics, and the mistakes LLMs most often make. ## Core Features & Use Cases - Complete function catalog: Covers IF(), IFNULL()/NVL(), NULLIF(), COALESCE(), NVL2(), the CASE operator (simple and searched forms), and DECODE/DECODE_ORACLE with one-line semantics per entry. - Common-mistake correction table: Maps wrong patterns (e.g., CASE ... END CASE in a SELECT list) to the correct MariaDB syntax with explanations. - Version awareness: Defaults to MariaDB 11.8 LTS context and annotates functions only available from specific versions. - Use Case: When writing a query like SELECT with a NULL-safe division, the Skill directs you to x / NULLIF(y, 0) instead of relying on error-prone division, and clarifies that DECODE_ORACLE works in any SQL mode while plain DECODE is the two-argument decryption function. ## Quick Start Ask the AI to write a MariaDB query that branches on a condition or substitutes for NULL values, such as categorizing orders with a CASE expression.

Frequently Asked Questions about mariadb-control-flow-functions

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

FAQPage Schema
How do I write conditional logic in a MariaDB SELECT query?▼

Use the three-argument IF(expr1, expr2, expr3) function or the CASE ... END operator anywhere an expression is valid. The IF ... THEN ... END IF and CASE ... END CASE forms are compound statements legal only inside stored programs like procedures and triggers.

What is the difference between IFNULL and COALESCE in MariaDB?▼

IFNULL(expr1, expr2) takes exactly two arguments and returns the first non-NULL value. COALESCE accepts any number of arguments and returns the first non-NULL one, so prefer COALESCE when you have more than two fallback candidates.

Does MariaDB DECODE work like Oracle's DECODE function?▼

Only when sql_mode=ORACLE. In default mode, DECODE(crypt_str, pass_str) is a two-argument decryption function. Use DECODE_ORACLE for the Oracle-style value-matching form in any SQL mode, noting it treats NULL as equal to NULL.

How do I avoid division by zero errors in MariaDB?▼

Guard the divisor with NULLIF: write x / NULLIF(y, 0). NULLIF(a, b) returns NULL when a equals b, so a zero divisor becomes NULL and the division yields NULL instead of failing the statement.

Why does COALESCE return unexpected types in MariaDB?▼

COALESCE, IF, and CASE aggregate a single result type across their value arguments. A hex literal passed to COALESCE is treated as a string, not a number, even though the same literal inserted into an INT column is numeric, so cast branches to a common type explicitly.

Which MariaDB versions support these control-flow functions?▼

All listed functions are available in every current LTS branch (10.6, 10.11, 11.4, 11.8) unless annotated with a since-version marker. The Skill assumes MariaDB 11.8 LTS as the default context unless another version is specified.