Use this file to discover all available pages before exploring further.
Embucket implements the Snowflake SQL dialect, providing compatibility with existing Snowflake queries and workflows. This guide covers supported syntax, functions, and query optimization.
-- Simple selectSELECT 1 as result;-- Select with WHERE clauseSELECT customer_id, customer_name, emailFROM customersWHERE status = 'active';-- Select with ORDER BY and LIMITSELECT order_id, order_date, total_amountFROM ordersORDER BY order_date DESCLIMIT 100;
-- INNER JOINSELECT o.order_id, c.customer_name, o.total_amountFROM orders oINNER JOIN customers c ON o.customer_id = c.customer_id;-- LEFT JOINSELECT c.customer_id, c.customer_name, COUNT(o.order_id) as order_countFROM customers cLEFT JOIN orders o ON c.customer_id = o.customer_idGROUP BY c.customer_id, c.customer_name;-- Multiple joinsSELECT o.order_id, c.customer_name, p.product_name, oi.quantityFROM orders oJOIN customers c ON o.customer_id = c.customer_idJOIN order_items oi ON o.order_id = oi.order_idJOIN products p ON oi.product_id = p.product_id;
WITH monthly_sales AS ( SELECT DATE_TRUNC('month', order_date) as month, SUM(total_amount) as total_sales FROM orders GROUP BY DATE_TRUNC('month', order_date)),average_monthly AS ( SELECT AVG(total_sales) as avg_sales FROM monthly_sales)SELECT ms.month, ms.total_sales, am.avg_sales, ms.total_sales - am.avg_sales as varianceFROM monthly_sales msCROSS JOIN average_monthly amORDER BY ms.month;
-- Days between datesSELECT datediff(day, '2024-01-01', '2024-01-31') as days_diff;-- Months between datesSELECT datediff(month, hire_date, CURRENT_DATE()) as months_employedFROM employees;-- Hours between timestampsSELECT order_id, datediff(hour, created_at, shipped_at) as hours_to_shipFROM ordersWHERE shipped_at IS NOT NULL;
-- Truncate to day (removes time)SELECT date_trunc(day, CURRENT_TIMESTAMP()) as today;-- Truncate to month (first day of month)SELECT date_trunc(month, order_date) as order_month, COUNT(*) as order_countFROM ordersGROUP BY date_trunc(month, order_date);-- Truncate to hourSELECT date_trunc(hour, event_time) as event_hour, COUNT(*) as event_countFROM eventsGROUP BY date_trunc(hour, event_time);
-- Current date and timeSELECT CURRENT_DATE() as today, CURRENT_TIME() as now_time, CURRENT_TIMESTAMP() as now_timestamp;-- Extract parts of datesSELECT EXTRACT(year FROM order_date) as order_year, EXTRACT(month FROM order_date) as order_month, EXTRACT(day FROM order_date) as order_dayFROM orders;-- DATE_PART (alias for EXTRACT)SELECT date_part(year, order_date) as order_year, date_part(week, order_date) as order_weekFROM orders;-- Convert to dateSELECT TO_DATE('2024-01-15', 'YYYY-MM-DD') as parsed_date, CAST('2024-01-15' AS DATE) as cast_date;
-- Basic aggregatesSELECT COUNT(*) as total_orders, SUM(total_amount) as total_revenue, AVG(total_amount) as avg_order_value, MIN(order_date) as first_order, MAX(order_date) as last_orderFROM orders;-- GROUPING_ID for cube/rollup operationsSELECT region, product_category, SUM(sales) as total_sales, GROUPING_ID(region, product_category) as grouping_levelFROM salesGROUP BY CUBE(region, product_category);-- COUNT with conditionsSELECT customer_id, COUNT(*) as total_orders, COUNT(CASE WHEN status = 'completed' THEN 1 END) as completed_orders, COUNT(CASE WHEN status = 'cancelled' THEN 1 END) as cancelled_ordersFROM ordersGROUP BY customer_id;-- Statistical aggregatesSELECT product_id, AVG(price) as avg_price, STDDEV(price) as price_stddev, VARIANCE(price) as price_varianceFROM product_pricesGROUP BY product_id;
-- Row numberingSELECT customer_id, order_date, total_amount, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) as order_numberFROM orders;-- RankingSELECT product_name, sales_amount, RANK() OVER (ORDER BY sales_amount DESC) as sales_rank, DENSE_RANK() OVER (ORDER BY sales_amount DESC) as dense_sales_rankFROM product_sales;-- Moving averagesSELECT order_date, total_amount, AVG(total_amount) OVER ( ORDER BY order_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW ) as moving_avg_7dayFROM orders;-- LAG and LEADSELECT order_date, total_sales, LAG(total_sales, 1) OVER (ORDER BY order_date) as previous_day_sales, LEAD(total_sales, 1) OVER (ORDER BY order_date) as next_day_salesFROM daily_sales;
-- PIVOT: Convert rows to columnsSELECT *FROM ( SELECT product_category, month, sales FROM monthly_sales)PIVOT ( SUM(sales) FOR month IN ('Jan', 'Feb', 'Mar', 'Apr')) as pivoted_sales;-- UNPIVOT: Convert columns to rowsSELECT *FROM yearly_salesUNPIVOT ( sales FOR quarter IN (q1, q2, q3, q4)) as unpivoted_sales;
-- Show logical planEXPLAIN SELECT * FROM orders WHERE order_date > '2024-01-01';-- Show physical plan with execution statsEXPLAIN ANALYZE SELECT customer_id, COUNT(*) as order_countFROM ordersGROUP BY customer_id;-- Verbose outputEXPLAIN VERBOSE SELECT * FROM large_table WHERE id = 12345;
-- Keep latest record per customerWITH ranked AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) as rn FROM customer_updates)SELECT * FROM ranked WHERE rn = 1;
SELECT order_id, order_date, LEAD(order_date) OVER (ORDER BY order_date) as next_order_date, datediff(day, order_date, LEAD(order_date) OVER (ORDER BY order_date)) as days_gapFROM ordersWHERE datediff(day, order_date, LEAD(order_date) OVER (ORDER BY order_date)) > 7;