Write and fix SQL
Get a working query from a description, or fix a broken one.
Data · When you ask · Updated
How do you generate or fix a SQL query?
To generate or fix a SQL query, describe the result you want or attach the broken query in Slack. Maito reads your BigQuery or Supabase schema, writes or repairs the SQL, checks the result against your data, and explains the cause of any error, such as a join that counts rows twice.
How it's done today
Analysts help others with queries all day.
What Maito does
- Read the request and schema
- Write or fix the query
- Explain it
Example
@Maito Fix this query; it double counts orders. Attached: orders_revenue.sql.
Found it. The join to refunds is one row per refund, so an order with two partial refunds appears twice. Revenue came out at $412,880 instead of $398,240, which is 3.7% too high. 61 orders have more than one refund. Fix: total the refunds per order first, then join. The catch: the filter status != 'cancelled' also drops rows where status is empty, because empty never equals anything. 17 orders vanish that way. I kept your behavior, but you probably do not want it. I ran it on BigQuery for last month and got $398,240. Attached: orders_revenue_fixed.sql.
@Maito Include the 17.
Done. Those 17 orders add $4,120, so the total is $402,360. Attached: orders_revenue_fixed_v2.sql.
Nina (1:20 PM):
Fix this query; it double counts orders. Attached: orders_revenue.sql.
Maito (1:25 PM):
Found it. The join to refunds is one row per refund, so an order with two partial refunds appears twice. Revenue came out at $412,880 instead of $398,240, which is 3.7% too high. 61 orders have more than one refund.
Fix: total the refunds per order first, then join.
The catch: the filter status != 'cancelled' also drops rows where status is empty, because empty never equals anything. 17 orders vanish that way. I kept your behavior, but you probably do not want it.
I ran it on BigQuery for last month and got $398,240. Attached: orders_revenue_fixed.sql.
Nina (1:31 PM):
Include the 17.
Maito (1:33 PM):
Done. Those 17 orders add $4,120, so the total is $402,360. Attached: orders_revenue_fixed_v2.sql.
How to set it up
- Set up
Nothing to set up. Ask in Slack or by email whenever you need it.
- Connect database
- BigQuery
- Supabase
Not connected yet? Your agent sends you a link to sign in.
What it won't do
Maito cannot know which table or column your team treats as the source of truth unless the schema or a doc says so. It will ask instead of picking one.
Questions
It can run read queries through BigQuery or Supabase to check that the output makes sense. It does not change data unless your team has set that up and approved it.
Yes. It looks for common causes such as duplicated joins and filters that drop rows, shows the difference in the result, and attaches the fixed query.
It reads them from the connected database, so it uses your real schema rather than guessing.