Describe your data model in natural language and Cerewro creates the complete SQL schema with relationships, indexes and constraints. It also writes the most complex queries (JOINs, CTEs, windows) and optimizes slow queries with EXPLAIN ANALYZE.
Design the SQL schema for an e-commerce system: users, products with categories and variants, carts, orders with line items, payments and reviews. MySQL 8, with indexes and foreign keys.
WITH stats AS (
SELECT u.id, u.name, COUNT(o.id) AS orders, SUM(o.total) AS spent
FROM users u JOIN orders o ON o.user_id = u.id
WHERE o.status = 'delivered'
AND o.created_at >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
GROUP BY u.id, u.name
)
SELECT RANK() OVER (ORDER BY spent DESC) AS rank, name, orders,
ROUND(spent, 2) AS total_spent
FROM stats ORDER BY spent DESC LIMIT 10;