Last updated
Formatting Options
- Keyword case: UPPERCASE (default), lowercase, or Title Case
- Indentation: 2 spaces, 4 spaces, or tabs
- Dialect: MySQL, PostgreSQL, SQL Server, Oracle, SQLite, ANSI SQL
- Comma position: end of line (default) or start of line
Examples
Example 1: Basic SELECT Query
Before formatting:
select u.id,u.first_name,u.last_name,u.email,o.order_date,o.total from users u inner join orders o on u.id=o.user_id where u.active=1 and o.total>100 order by o.order_date desc limit 20
After formatting:
SELECT
u.id,
u.first_name,
u.last_name,
u.email,
o.order_date,
o.total
FROM
users u
INNER JOIN orders o ON u.id = o.user_id
WHERE
u.active = 1
AND o.total > 100
ORDER BY
o.order_date DESC
LIMIT 20
Example 2: Complex Query with Subquery
Before formatting:
select p.name,p.price,(select avg(price) from products where category_id=p.category_id) as avg_category_price from products p where p.price>(select avg(price) from products) order by p.price desc
After formatting:
SELECT
p.name,
p.price,
(
SELECT
AVG(price)
FROM
products
WHERE
category_id = p.category_id
) AS avg_category_price
FROM
products p
WHERE
p.price > (
SELECT
AVG(price)
FROM
products
)
ORDER BY
p.price DESC
Example 3: Common Table Expression (CTE)
Before formatting:
with monthly_sales as (select date_trunc('month',order_date) as month,sum(total) as revenue from orders where order_date>=current_date-interval '12 months' group by 1),ranked as (select month,revenue,rank() over(order by revenue desc) as rank from monthly_sales) select month,revenue,rank from ranked where rank<=3 order by rank
After formatting:
WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', order_date) AS month,
SUM(total) AS revenue
FROM
orders
WHERE
order_date >= CURRENT_DATE - INTERVAL '12 months'
GROUP BY
1
),
ranked AS (
SELECT
month,
revenue,
RANK() OVER (ORDER BY revenue DESC) AS rank
FROM
monthly_sales
)
SELECT
month,
revenue,
rank
FROM
ranked
WHERE
rank <= 3
ORDER BY
rank