9951 explained code solutions for 126 technologies


mariadbHow to use the WITH clause in Mariadb?


The WITH clause in Mariadb is used to define a temporary named result set, known as a Common Table Expression (CTE). It can be used to simplify complex queries by breaking them down into smaller, more manageable parts.

Example

WITH cte_name AS (
  SELECT * FROM table_name
)
SELECT * FROM cte_name;

Output example

+--------+--------+
| column | value  |
+--------+--------+
|   ...  |   ...  |
+--------+--------+

Code explanation

  • WITH cte_name AS: This defines the CTE and assigns it a name.
  • SELECT * FROM table_name: This is the query that will be used to populate the CTE.
  • SELECT * FROM cte_name: This is the query that will use the CTE.

Helpful links

Edit this code on GitHub