Analytic SQL Examples¶
Real-world examples of Analytic SQL queries. Each example shows the Analytic SQL syntax alongside the generated SQL for different database dialects.
Try these queries yourself: play.analyticsql.com
Table of Contents¶
Basic Queries¶
Simple FROM¶
FROM with WHERE¶
FROM with SELECT¶
FROM WHERE SELECT¶
Filtering¶
Comparison Operators¶
ASQL:
SQL (PostgreSQL):
ASQL:
SQL (PostgreSQL):
ASQL:
SQL (PostgreSQL):
ASQL:
SQL (PostgreSQL):
NULL Checks¶
ASQL:
SQL (PostgreSQL):
ASQL:
SQL (PostgreSQL):
Logical Operators¶
ASQL:
SQL (PostgreSQL):
ASQL:
SQL (PostgreSQL):
ASQL:
SQL (PostgreSQL):
Multiple Conditions¶
ASQL:
SQL (PostgreSQL):
IN Operator¶
ASQL:
SQL (PostgreSQL):
ASQL:
SQL (PostgreSQL):
NOT IN Operator¶
ASQL:
SQL (PostgreSQL):
Aggregations¶
GROUP BY with COUNT (#)¶
ASQL:
SQL (PostgreSQL):
GROUP BY with SUM¶
ASQL:
SQL (PostgreSQL):
GROUP BY with AVG¶
ASQL:
SQL (PostgreSQL):
Multiple Aggregations¶
ASQL:
SQL (PostgreSQL):
SELECT region, SUM(amount) AS revenue, COUNT(*) AS orders, AVG(amount) AS avg_order
FROM sales
GROUP BY region
Multiple Grouping Columns¶
ASQL:
SQL (PostgreSQL):
GROUP BY with WHERE¶
ASQL:
SQL (PostgreSQL):
All Aggregation Functions¶
ASQL:
from sales
group by region (
sum(amount) as total_revenue,
avg(amount) as avg_order,
count(*) as order_count,
min(amount) as min_order,
max(amount) as max_order
)
SQL (PostgreSQL):
SELECT
region,
SUM(amount) AS total_revenue,
AVG(amount) AS avg_order,
COUNT(*) AS order_count,
MIN(amount) AS min_order,
MAX(amount) AS max_order
FROM sales
GROUP BY region
Sorting and Limiting¶
SORT Ascending¶
ASQL:
SQL (PostgreSQL):
SORT Descending¶
ASQL:
SQL (PostgreSQL):
Multiple Sort Columns¶
ASQL:
SQL (PostgreSQL):
TAKE/LIMIT¶
ASQL:
SQL (PostgreSQL):
GROUP BY + SORT¶
ASQL:
SQL (PostgreSQL):
Complete Pipeline¶
ASQL:
SQL (PostgreSQL):
SELECT country, COUNT(*) AS total_users
FROM users
WHERE status = 'active'
GROUP BY country
ORDER BY total_users DESC
LIMIT 10
Sort by Column (Descending)¶
ASQL:
SQL (PostgreSQL):
ASQL:
SQL (PostgreSQL):
The - prefix makes it easy to sort by columns in descending order. It also works with function calls like sort -month(created_at).
Complex Queries¶
Complex Analytics Query¶
ASQL:
from sales
where status == "completed" and amount > 100
group by region, month (
sum(amount) as revenue,
# as order_count,
avg(amount) as avg_order
)
sort -revenue
take 20
SQL (PostgreSQL):
SELECT
region,
month,
SUM(amount) AS revenue,
COUNT(*) AS order_count,
AVG(amount) AS avg_order
FROM sales
WHERE status = 'completed' AND amount > 100
GROUP BY region, month
ORDER BY revenue DESC
LIMIT 20
User Analytics¶
ASQL:
from users
where status == "active" and age >= 18 and email is not null
group by country (
# as total_users,
avg(age) as avg_age
)
sort -total_users
SQL (PostgreSQL):
SELECT
country,
COUNT(*) AS total_users,
AVG(age) AS avg_age
FROM users
WHERE status = 'active' AND age >= 18 AND email IS NOT NULL
GROUP BY country
ORDER BY total_users DESC
Sales Report¶
ASQL:
from sales
where (status == "completed" or status == "pending")
and amount >= 50
and created_at is not null
group by product_category (
sum(amount) as total_revenue,
# as total_orders,
avg(amount) as avg_order_value,
max(amount) as max_order_value
)
sort -total_revenue
take 10
SQL (PostgreSQL):
SELECT
product_category,
SUM(amount) AS total_revenue,
COUNT(*) AS total_orders,
AVG(amount) AS avg_order_value,
MAX(amount) AS max_order_value
FROM sales
WHERE (status = 'completed' OR status = 'pending')
AND amount >= 50
AND created_at IS NOT NULL
GROUP BY product_category
ORDER BY total_revenue DESC
LIMIT 10
Try It Yourself¶
The easiest way to try these examples is in the Interactive Playground. Just copy any ASQL query from the examples above and paste it into the playground to see the generated SQL in real-time.
You can also use the ASQL compiler in your Python code: