by mygptlist
5-8 production-ready, well-commented SQL queries tailored to your schema and dialect — each with a description and any assumptions.
A creator-provided example of what this workflow can produce.
```markdown
### 1. Top 10 Customers by Total Spend
This query returns the top 10 customers based on their total spending. We assume that the total spend is calculated from the `total_cents` in the `orders` table.
```sql
SELECT
c.id,
c.name,
SUM(o.total_cents) AS total_spent
FROM
customers c
JOIN
orders o ON c.id = o.customer_id
GROUP BY
c.id, c.name
ORDER BY
total_spent DESC
LIMIT 10; -- Limit to top 10 customers
```
---
### 2. Monthly Revenue for the Last 6 Months
This query returns monthly revenue for the last 6 months. It assumes that the relevant data is available and created_at is correctly populated.
```sql
SELECT
DATE_TRUNC('month', o.created_at) AS month,
SUM(o.total_cents) AS total_revenue
FROM
orders o
WHERE
o.created_at >= NOW() - INTERVAL '6 months'
GROUP BY
month
ORDER BY
month; -- Order by month for better readability
```
---
### 3. Count of Orders by Status
This query provides a count of orders grouped by their status, assuming that the `status` column contains meaningful categorizations.
```sql
SELECT
o.status,
COUNT(*) AS order_count
FROM
orders o
GROUP BY
o.status
ORDER BY
order_count DESC; -- Order by count for better visibility
```
---
### 4. New Customers Per Month
This query identifies the number of new customers each month. It assumes that `created_at` accurately reflects the customer registration date.
```sql
SELECT
DATE_TRUNC('month', c.created_at) AS month,
COUNT(*) AS new_customers
FROM
customers c
GROUP BY
month
ORDER BY
month; -- Order by month to show trends over time
```
---
### 5. Average Order Value by Country
This query calculates the average order value for customers segmented by their country. The average is derived from the total cents in the orders.
```sql
SELECT
c.country,
AVG(o.total_cents) AS average_order_value
FROM
customers c
JOIN
orders o ON c.id = o.customer_id
GROUP BY
c.country
ORDER BY
average_order_value DESC; -- Order by average order value for insights
```
```3 guided details before the run starts.
Price
$2