The FROM clause specifies the source table(s), subquery, or table function to query from.
Copy
-- Query from a tableSELECT * FROM table_name;-- Query from a specific databaseSELECT * FROM database.table_name;-- Query from a subquerySELECT *FROM ( SELECT column1, column2 FROM table_name WHERE condition) AS subquery;-- Query using a table functionSELECT *FROM table_function(arg1, arg2);
The GROUP BY clause groups rows by specified columns and is typically used with aggregate functions. Supports additional modifiers:
WITH ROLLUP: Adds extra rows with subtotals
WITH CUBE: Adds subtotals for all possible combinations
WITH TOTALS: Adds a final row with totals
Copy
-- Basic groupingSELECT category, COUNT(*) as count, AVG(price) as avg_priceFROM productsGROUP BY category;-- Grouping with ROLLUPSELECT category, subcategory, SUM(sales)FROM salesGROUP BY category, subcategoryWITH ROLLUP;
The ORDER BY clause sorts results by specified columns. Supports additional features:
WITH FILL: Fills missing values in sorted results
INTERPOLATE: Specifies how to interpolate missing values
Copy
-- Basic sortingSELECT *FROM table_nameORDER BY column1 DESC, column2 ASC;-- Sorting with fillSELECT time, valueFROM metricsORDER BY time WITH FILL FROM '2024-01-01' TO '2024-01-02' STEP INTERVAL 1 HOUR;
Propel supports various types of joins to combine rows from two or more tables based on a related column between them.
LEFT JOIN
The LEFT JOIN returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side if there is no match.
The RIGHT JOIN returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side if there is no match.
The FULL JOIN returns all records when there is a match in either left (table1) or right (table2) table records. If there is no match, the result contains NULL values for columns from the table without a match.
The WINDOW clause defines named window specifications for window functions.
Copy
SELECT date, value, AVG(value) OVER w as moving_avg, RANK() OVER w as rankFROM metricsWINDOW w AS ( PARTITION BY category ORDER BY date ROWS BETWEEN 3 PRECEDING AND CURRENT ROW);
The QUALIFY clause filters results based on window functions.
Copy
SELECT date, category, value, RANK() OVER (PARTITION BY category ORDER BY value DESC) as rankFROM metricsQUALIFY rank <= 3; -- Keep only top 3 values per category