Common Table Expressions (CTEs) allow for temporary query results to be stored and reused within the same SQL statement. There are two types of CTEs - non-recursive and recursive. Non-recursive CTEs can refer to other CTEs and are optimized by MariaDB through techniques like CTE merging and condition pushdown. Recursive CTEs enable querying recursive relationships and computing transitive closures through a recursive part that is executed repeatedly until it produces no new results.