-
Notifications
You must be signed in to change notification settings - Fork 689
With
Mathias Wulff edited this page Dec 15, 2025
·
6 revisions
In AlaSQL you can use WITH caluse with SELECT operator to create temporary tables for current select (a.k.a. Commom Table Expressions).
A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. CTE can be referenced multiple times in the same query.
AlaSQL supports recursive CTEs (Common Table Expressions). This allows you to reference the CTE within itself, which is useful for hierarchical data.
WITH RECURSIVE cte_name (column_names) AS (
-- Anchor member
SELECT ...
UNION ALL
-- Recursive member
SELECT ... FROM cte_name WHERE ...
)
SELECT * FROM cte_name;Example of generating a sequence of numbers:
WITH RECURSIVE seq (n) AS (
SELECT 1
UNION ALL
SELECT n + 1 FROM seq WHERE n < 10
)
SELECT * FROM seq; With Totals as
(
select *,
price * quantity as [Total price]
from grocery
)
select *
, case
when [Total price]>100 and [Total price]<= 200 then '2%'
when [Total price]>200 and [Total price]<= 300 then '3%'
when [Total price]>300 and [Total price]<= 400 then '4%'
else '0%'
end as tax
from
TotalsYou can try this example in jsFiddle.
var res = alasql('WITH one AS (SELECT * FROM ?),\
two AS (SELECT * FROM ?)\
SELECT * FROM one,two', [[{a:1},{a:2}],[{b:10},{b:20}]]);You can try this example in jsFiddle.
© 2014-2026, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo