MariaDB: WITH SELECT


-- SIMPLE SAMPLE

WITH sales_product_year AS (
    SELECT product,
        YEAR(ship_date) AS year,
        SUM(price) AS total_amt
    FROM item_sales

)
SELECT * FROM sales_product_year S1
WHERE total_amt >
    (SELECT 0.1 * SUM(total_amt)
        FROM sales_product_year S2
            WHERE S2.year = S1.year)

-- RECURSIVE SAMPLE

WITH RECURSIVE paths (cur_path, cur_dest) AS (
    SELECT origin, origin FROM bus_routes WHERE origin='New York'
  UNION
    SELECT CONCAT(paths.cur_path, ',', bus_routes.dst), bus_routes.dst
      FROM paths, bus_routes
      WHERE paths.cur_dest = bus_routes.origin AND
      LOCATE(bus_routes.dst, paths.cur_path)=0
)
SELECT * FROM paths;

+-----------------------------+------------+
| cur_path                    | cur_dest   |
+-----------------------------+------------+
| New York                    | New York   |
| New York,Boston             | Boston     |
| New York,Washington         | Washington |
| New York,Washington,Boston  | Boston     |
| New York,Washington,Raleigh | Raleigh    |
+-----------------------------+------------+