sql-natural-language-pairs / validation.jsonl
SalutefaceAAC's picture
Add 284 new NL→SQL pairs from finance and social schemas + expanded coverage of ecommerce, HR, analytics, healthcare, SaaS, and inventory. Total grows from 286 to 570. New splits: 458/56/56 train/val/test. JSONL format added alongside existing CSV. Richer query type distribution: window, CTE, self_join, union.
05fc851 verified
Raw History Blame Contribute Delete
53.7 kB
{"id": "9c6ebb1eaf67246c", "question": "Find all employees who changed departments in the last year.", "sql": "SELECT e.name, e.department_id, e.hire_date\nFROM employees e\nWHERE e.hire_date >= CURRENT_DATE - INTERVAL '1 year';", "difficulty": "easy", "schema": "hr", "query_type": "select", "tables_used": ["employees"], "schema_context": " employees(employee_id, name, department_id, manager_id, hire_date, salary, city)\n departments(department_id, name, location, budget, head_count)\n projects(project_id, name, department_id, start_date, end_date, budget)\n assignments(assignment_id, employee_id, project_id, role, hours_per_week)\n performance_reviews(review_id, employee_id, review_year, rating, bonus)"}
{"id": "2d6b99362082b1e0", "question": "List all loans currently in 'active' status with their principal amounts.", "sql": "SELECT id, account_id, principal, interest_rate, term_months FROM loans WHERE status = 'active';", "difficulty": "easy", "schema": "finance", "query_type": "select", "tables_used": ["loans"], "schema_context": "accounts(id, holder_name, type, balance, opened_at, status)\n transactions(id, account_id, type, amount, description, ts, category)\n loans(id, account_id, principal, interest_rate, term_months, issued_at, status)\n loan_payments(id, loan_id, amount, paid_at, remaining_balance)\n fraud_flags(id, transaction_id, reason, flagged_at, resolved)"}
{"id": "fc8a406beff52231", "question": "How many customers are from Australia?", "sql": "SELECT COUNT(*) AS customer_count FROM customers WHERE country = 'Australia';", "difficulty": "easy", "schema": "ecommerce", "query_type": "aggregate", "tables_used": ["customers"], "schema_context": " customers(customer_id, name, email, city, country, created_at, total_spent)\n orders(order_id, customer_id, order_date, status, total_amount, shipping_city)\n order_items(item_id, order_id, product_id, quantity, unit_price)\n products(product_id, name, category, price, stock_quantity, supplier_id)\n suppliers(supplier_id, name, country, rating)"}
{"id": "bde23f4e2197ea97", "question": "List all users from the United States who signed up after 2023-06-01, ordered by creation date.", "sql": "SELECT id, created_at, plan FROM users WHERE country = 'United States' AND created_at > '2023-06-01' ORDER BY created_at DESC;", "difficulty": "easy", "schema": "analytics", "query_type": "select", "tables_used": ["users"], "schema_context": "events(id, session_id, user_id, event_type, page, ts, properties)\n users(id, created_at, plan, country, referral_source)\n sessions(id, user_id, started_at, ended_at, device)\n conversions(id, user_id, goal, value, converted_at)\n ab_experiments(id, name, variant, user_id, enrolled_at)"}
{"id": "05cd0bc0f8924d9f", "question": "What is the average bonus paid in 2022?", "sql": "SELECT AVG(bonus) FROM performance_reviews WHERE review_year = 2022;", "difficulty": "easy", "schema": "hr", "query_type": "aggregate", "tables_used": ["performance_reviews"], "schema_context": " employees(employee_id, name, department_id, manager_id, hire_date, salary, city)\n departments(department_id, name, location, budget, head_count)\n projects(project_id, name, department_id, start_date, end_date, budget)\n assignments(assignment_id, employee_id, project_id, role, hours_per_week)\n performance_reviews(review_id, employee_id, review_year, rating, bonus)"}
{"id": "c39a5eab6874d1e3", "question": "Which items have a reorder point higher than 50 units?", "sql": "SELECT name, sku, reorder_point FROM items WHERE reorder_point > 50 ORDER BY reorder_point DESC;", "difficulty": "easy", "schema": "inventory", "query_type": "select", "tables_used": ["items"], "schema_context": "warehouses(id, name, location, capacity, manager_id)\n items(id, sku, name, category, unit_cost, reorder_point)\n stock_levels(warehouse_id, item_id, quantity, last_updated)\n purchase_orders(id, supplier_id, warehouse_id, created_at, status, total)\n po_lines(id, po_id, item_id, quantity, unit_price)"}
{"id": "2de33c6088fbaffe", "question": "What is the average conversion value for purchases completed in December 2023?", "sql": "SELECT AVG(value) as avg_conversion_value FROM conversions WHERE goal = 'purchase' AND EXTRACT(YEAR FROM converted_at) = 2023 AND EXTRACT(MONTH FROM converted_at) = 12;", "difficulty": "easy", "schema": "analytics", "query_type": "aggregate", "tables_used": ["conversions"], "schema_context": "events(id, session_id, user_id, event_type, page, ts, properties)\n users(id, created_at, plan, country, referral_source)\n sessions(id, user_id, started_at, ended_at, device)\n conversions(id, user_id, goal, value, converted_at)\n ab_experiments(id, name, variant, user_id, enrolled_at)"}
{"id": "4449a5ac231d7ce9", "question": "What is the total revenue from 'delivered' orders?", "sql": "SELECT SUM(total_amount) AS revenue FROM orders WHERE status = 'delivered';", "difficulty": "easy", "schema": "ecommerce", "query_type": "aggregate", "tables_used": ["orders"], "schema_context": " customers(customer_id, name, email, city, country, created_at, total_spent)\n orders(order_id, customer_id, order_date, status, total_amount, shipping_city)\n order_items(item_id, order_id, product_id, quantity, unit_price)\n products(product_id, name, category, price, stock_quantity, supplier_id)\n suppliers(supplier_id, name, country, rating)"}
{"id": "732c52bc2d3c2e41", "question": "List all customers from Germany.", "sql": "SELECT * FROM customers WHERE country = 'Germany';", "difficulty": "easy", "schema": "ecommerce", "query_type": "select", "tables_used": ["customers"], "schema_context": " customers(customer_id, name, email, city, country, created_at, total_spent)\n orders(order_id, customer_id, order_date, status, total_amount, shipping_city)\n order_items(item_id, order_id, product_id, quantity, unit_price)\n products(product_id, name, category, price, stock_quantity, supplier_id)\n suppliers(supplier_id, name, country, rating)"}
{"id": "a1d0eb6a62d0d091", "question": "How many sessions were from desktop?", "sql": "SELECT COUNT(*) FROM sessions WHERE device = 'desktop';", "difficulty": "easy", "schema": "analytics", "query_type": "aggregate", "tables_used": ["sessions"], "schema_context": " events(event_id, user_id, event_type, event_date, page, session_id, duration_seconds)\n users(user_id, username, signup_date, plan, country, is_active)\n sessions(session_id, user_id, start_time, end_time, device, browser)\n pages(page_id, url, title, category, created_at)\n conversions(conversion_id, user_id, event_date, revenue, campaign_id)"}
{"id": "038a08e1c95ffe92", "question": "Find all resolved support tickets created in the last 30 days.", "sql": "SELECT id, org_id, subject, priority, resolved_at FROM support_tickets WHERE status = 'resolved' AND created_at >= NOW() - INTERVAL '30 days' ORDER BY created_at DESC;", "difficulty": "easy", "schema": "saas", "query_type": "select", "tables_used": ["support_tickets"], "schema_context": "organizations(id, name, plan, created_at, mrr, employee_count)\n users(id, org_id, email, role, created_at, last_active)\n subscriptions(id, org_id, plan, started_at, ended_at, seats, mrr)\n feature_usage(id, org_id, feature, used_at, user_id, count)\n support_tickets(id, org_id, user_id, subject, priority, status, created_at, resolved_at)"}
{"id": "1dfcadbcb3cd5c21", "question": "Find all products whose name starts with 'Pro'.", "sql": "SELECT * FROM products WHERE name LIKE 'Pro%';", "difficulty": "easy", "schema": "ecommerce", "query_type": "select", "tables_used": ["products"], "schema_context": " customers(customer_id, name, email, city, country, created_at, total_spent)\n orders(order_id, customer_id, order_date, status, total_amount, shipping_city)\n order_items(item_id, order_id, product_id, quantity, unit_price)\n products(product_id, name, category, price, stock_quantity, supplier_id)\n suppliers(supplier_id, name, country, rating)"}
{"id": "dcff1748cdb1c0f9", "question": "What is the average years of experience for Orthopedics doctors?", "sql": "SELECT AVG(years_experience) FROM doctors WHERE specialty = 'Orthopedics';", "difficulty": "easy", "schema": "healthcare", "query_type": "aggregate", "tables_used": ["doctors"], "schema_context": " patients(patient_id, name, date_of_birth, gender, city, insurance_type)\n doctors(doctor_id, name, specialty, hospital_id, years_experience)\n appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis)\n hospitals(hospital_id, name, city, beds, rating)\n prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)"}
{"id": "73471d477b89b287", "question": "How many 'adjustment' movements happened this month?", "sql": "SELECT COUNT(*) FROM movements\nWHERE movement_type = 'adjustment' AND DATE_TRUNC('month', movement_date) = DATE_TRUNC('month', CURRENT_DATE);", "difficulty": "easy", "schema": "inventory", "query_type": "aggregate", "tables_used": ["movements"], "schema_context": " warehouses(warehouse_id, name, city, country, capacity)\n items(item_id, name, sku, category, unit_cost, reorder_point)\n stock(stock_id, item_id, warehouse_id, quantity, last_updated)\n movements(movement_id, item_id, warehouse_id, movement_type, quantity, movement_date)\n vendors(vendor_id, name, country, lead_time_days, reliability_score)"}
{"id": "253dfe7eb038eba0", "question": "How many posts were created by user with ID 42 in the last 30 days that have not been deleted?", "sql": "SELECT COUNT(*) as post_count FROM posts WHERE user_id = 42 AND is_deleted = false AND created_at >= CURRENT_TIMESTAMP - INTERVAL '30 days';", "difficulty": "easy", "schema": "social", "query_type": "aggregate", "tables_used": ["posts"], "schema_context": "users(id, username, email, created_at, bio, follower_count)\n posts(id, user_id, content, created_at, likes, shares, is_deleted)\n comments(id, post_id, user_id, content, created_at, likes)\n follows(follower_id, following_id, followed_at)\n hashtags(id, tag)\n post_hashtags(post_id, hashtag_id)"}
{"id": "98c0f631068acf1b", "question": "What is the total balance across all active savings accounts?", "sql": "SELECT SUM(balance) FROM accounts WHERE type = 'savings' AND status = 'active';", "difficulty": "easy", "schema": "finance", "query_type": "aggregate", "tables_used": ["accounts"], "schema_context": "accounts(id, holder_name, type, balance, opened_at, status)\n transactions(id, account_id, type, amount, description, ts, category)\n loans(id, account_id, principal, interest_rate, term_months, issued_at, status)\n loan_payments(id, loan_id, amount, paid_at, remaining_balance)\n fraud_flags(id, transaction_id, reason, flagged_at, resolved)"}
{"id": "203197173dd079bd", "question": "What is the average number of hours allocated per project assignment?", "sql": "SELECT AVG(hours_allocated) FROM project_assignments;", "difficulty": "easy", "schema": "hr", "query_type": "aggregate", "tables_used": ["project_assignments"], "schema_context": "employees(id, name, email, dept_id, manager_id, hire_date, salary)\n departments(id, name, location, budget)\n projects(id, name, start_date, end_date, dept_id, status)\n project_assignments(employee_id, project_id, role, hours_allocated)\n performance_reviews(id, employee_id, reviewer_id, year, score, notes)"}
{"id": "2f61d35a515a250a", "question": "What is the name and SKU of all items in the Electronics category?", "sql": "SELECT name, sku FROM items WHERE category = 'Electronics';", "difficulty": "easy", "schema": "inventory", "query_type": "select", "tables_used": ["items"], "schema_context": "warehouses(id, name, location, capacity, manager_id)\n items(id, sku, name, category, unit_cost, reorder_point)\n stock_levels(warehouse_id, item_id, quantity, last_updated)\n purchase_orders(id, supplier_id, warehouse_id, created_at, status, total)\n po_lines(id, po_id, item_id, quantity, unit_price)"}
{"id": "5110d949642d1b71", "question": "How many doctors specialize in Neurology?", "sql": "SELECT COUNT(*) FROM doctors WHERE specialty = 'Neurology';", "difficulty": "easy", "schema": "healthcare", "query_type": "aggregate", "tables_used": ["doctors"], "schema_context": " patients(patient_id, name, date_of_birth, gender, city, insurance_type)\n doctors(doctor_id, name, specialty, hospital_id, years_experience)\n appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis)\n hospitals(hospital_id, name, city, beds, rating)\n prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)"}
{"id": "fecc87a8f33ab02b", "question": "What is the average unit cost of items in the Furniture category?", "sql": "SELECT AVG(unit_cost) as average_cost FROM items WHERE category = 'Furniture';", "difficulty": "easy", "schema": "inventory", "query_type": "aggregate", "tables_used": ["items"], "schema_context": "warehouses(id, name, location, capacity, manager_id)\n items(id, sku, name, category, unit_cost, reorder_point)\n stock_levels(warehouse_id, item_id, quantity, last_updated)\n purchase_orders(id, supplier_id, warehouse_id, created_at, status, total)\n po_lines(id, po_id, item_id, quantity, unit_price)"}
{"id": "3aca2980616c09aa", "question": "How many patients are male and how many are female?", "sql": "SELECT gender, COUNT(*) as patient_count FROM patients GROUP BY gender;", "difficulty": "easy", "schema": "healthcare", "query_type": "aggregate", "tables_used": ["patients"], "schema_context": "patients(id, name, dob, gender, insurance_id)\n doctors(id, name, specialty, dept_id, npi)\n appointments(id, patient_id, doctor_id, scheduled_at, status, notes)\n diagnoses(id, patient_id, doctor_id, icd_code, description, diagnosed_at)\n prescriptions(id, patient_id, doctor_id, drug_name, dosage, start_date, end_date)"}
{"id": "e8cfa30cbb8306ca", "question": "List all users created in 2024 who have never been active, ordered by email.", "sql": "SELECT id, org_id, email, role, created_at FROM users WHERE EXTRACT(YEAR FROM created_at) = 2024 AND last_active IS NULL ORDER BY email ASC;", "difficulty": "easy", "schema": "saas", "query_type": "select", "tables_used": ["users"], "schema_context": "organizations(id, name, plan, created_at, mrr, employee_count)\n users(id, org_id, email, role, created_at, last_active)\n subscriptions(id, org_id, plan, started_at, ended_at, seats, mrr)\n feature_usage(id, org_id, feature, used_at, user_id, count)\n support_tickets(id, org_id, user_id, subject, priority, status, created_at, resolved_at)"}
{"id": "d7405190b1887311", "question": "For each referral source, calculate the average number of events per user session and the conversion rate for users acquired in the last 90 days.", "sql": "WITH user_stats AS (SELECT u.id, u.referral_source, COUNT(DISTINCT s.id) as session_count, COUNT(e.id) as event_count FROM users u LEFT JOIN sessions s ON u.id = s.user_id LEFT JOIN events e ON s.id = e.session_id WHERE u.created_at >= NOW() - INTERVAL '90 days' GROUP BY u.id, u.referral_source) SELECT referral_source, ROUND(AVG(CASE WHEN session_count > 0 THEN event_count::numeric / session_count ELSE 0 END), 2) as avg_events_per_session, COUNT(DISTINCT id) as total_users FROM user_stats GROUP BY referral_source ORDER BY avg_events_per_session DESC;", "difficulty": "medium", "schema": "analytics", "query_type": "cte", "tables_used": ["users", "sessions", "events"], "schema_context": "events(id, session_id, user_id, event_type, page, ts, properties)\n users(id, created_at, plan, country, referral_source)\n sessions(id, user_id, started_at, ended_at, device)\n conversions(id, user_id, goal, value, converted_at)\n ab_experiments(id, name, variant, user_id, enrolled_at)"}
{"id": "b43141b5e4ce7048", "question": "Find all orders placed by customers from France.", "sql": "SELECT o.*\nFROM orders o\nJOIN customers c ON o.customer_id = c.customer_id\nWHERE c.country = 'France';", "difficulty": "medium", "schema": "ecommerce", "query_type": "join", "tables_used": ["orders", "customers"], "schema_context": " customers(customer_id, name, email, city, country, created_at, total_spent)\n orders(order_id, customer_id, order_date, status, total_amount, shipping_city)\n order_items(item_id, order_id, product_id, quantity, unit_price)\n products(product_id, name, category, price, stock_quantity, supplier_id)\n suppliers(supplier_id, name, country, rating)"}
{"id": "cee8938dc0a99522", "question": "Find organizations where total feature usage count exceeds the sum of all support ticket creation dates by month", "sql": "SELECT o.id, o.name,\n SUM(f.count) as total_feature_usage,\n COUNT(DISTINCT t.id) as ticket_count,\n EXTRACT(YEAR_MONTH FROM MAX(f.used_at)) as latest_usage_month\nFROM organizations o\nINNER JOIN feature_usage f ON o.id = f.org_id\nLEFT JOIN support_tickets t ON o.id = t.org_id\nGROUP BY o.id, o.name\nHAVING SUM(f.count) > COUNT(DISTINCT t.id) * 50\nORDER BY total_feature_usage DESC;", "difficulty": "medium", "schema": "saas", "query_type": "aggregate", "tables_used": ["organizations", "feature_usage", "support_tickets"], "schema_context": "organizations(id, name, plan, created_at, mrr, employee_count)\n users(id, org_id, email, role, created_at, last_active)\n subscriptions(id, org_id, plan, started_at, ended_at, seats, mrr)\n feature_usage(id, org_id, feature, used_at, user_id, count)\n support_tickets(id, org_id, user_id, subject, priority, status, created_at, resolved_at)"}
{"id": "8e5381829f2e2ad8", "question": "List all patients who have had more than 2 appointments in the past 6 months, along with their appointment counts and most recent appointment date.", "sql": "SELECT p.id, p.name, COUNT(a.id) as appointment_count, MAX(a.scheduled_at) as latest_appointment FROM patients p INNER JOIN appointments a ON p.id = a.patient_id WHERE a.scheduled_at >= CURRENT_DATE - INTERVAL '6 months' GROUP BY p.id, p.name HAVING COUNT(a.id) > 2 ORDER BY appointment_count DESC;", "difficulty": "medium", "schema": "healthcare", "query_type": "aggregate", "tables_used": ["patients", "appointments"], "schema_context": "patients(id, name, dob, gender, insurance_id)\n doctors(id, name, specialty, dept_id, npi)\n appointments(id, patient_id, doctor_id, scheduled_at, status, notes)\n diagnoses(id, patient_id, doctor_id, icd_code, description, diagnosed_at)\n prescriptions(id, patient_id, doctor_id, drug_name, dosage, start_date, end_date)"}
{"id": "45f7aeeb552c731e", "question": "Find users who have commented on posts by their followers but not on posts by users they follow.", "sql": "SELECT DISTINCT u.id, u.username FROM users u INNER JOIN comments c ON u.id = c.user_id INNER JOIN posts p ON c.post_id = p.id INNER JOIN follows f ON f.follower_id = p.user_id AND f.following_id = u.id WHERE NOT EXISTS (SELECT 1 FROM comments c2 INNER JOIN posts p2 ON c2.post_id = p2.id INNER JOIN follows f2 ON f2.follower_id = u.id AND f2.following_id = p2.user_id WHERE c2.user_id = u.id);", "difficulty": "medium", "schema": "social", "query_type": "subquery", "tables_used": ["users", "comments", "posts", "follows"], "schema_context": "users(id, username, email, created_at, bio, follower_count)\n posts(id, user_id, content, created_at, likes, shares, is_deleted)\n comments(id, post_id, user_id, content, created_at, likes)\n follows(follower_id, following_id, followed_at)\n hashtags(id, tag)\n post_hashtags(post_id, hashtag_id)"}
{"id": "bab96911d3d1069f", "question": "Which product categories have average price above 100?", "sql": "SELECT category, AVG(price) AS avg_price\nFROM products\nGROUP BY category\nHAVING AVG(price) > 100\nORDER BY avg_price DESC;", "difficulty": "medium", "schema": "ecommerce", "query_type": "aggregate", "tables_used": ["products"], "schema_context": " customers(customer_id, name, email, city, country, created_at, total_spent)\n orders(order_id, customer_id, order_date, status, total_amount, shipping_city)\n order_items(item_id, order_id, product_id, quantity, unit_price)\n products(product_id, name, category, price, stock_quantity, supplier_id)\n suppliers(supplier_id, name, country, rating)"}
{"id": "562e4509fe6e36a2", "question": "Find employees assigned to projects in the Engineering department with total hours allocated greater than 100", "sql": "SELECT e.id, e.name, d.name as assigned_dept, SUM(pa.hours_allocated) as total_hours FROM employees e INNER JOIN project_assignments pa ON e.id = pa.employee_id INNER JOIN projects p ON pa.project_id = p.id INNER JOIN departments d ON p.dept_id = d.id WHERE d.name = 'Engineering' GROUP BY e.id, e.name, d.name HAVING SUM(pa.hours_allocated) > 100 ORDER BY total_hours DESC;", "difficulty": "medium", "schema": "hr", "query_type": "aggregate", "tables_used": ["employees", "project_assignments", "projects", "departments"], "schema_context": "employees(id, name, email, dept_id, manager_id, hire_date, salary)\n departments(id, name, location, budget)\n projects(id, name, start_date, end_date, dept_id, status)\n project_assignments(employee_id, project_id, role, hours_allocated)\n performance_reviews(id, employee_id, reviewer_id, year, score, notes)"}
{"id": "617fa5b65896cfa4", "question": "Find the difference between each transaction amount and the account's average transaction amount.", "sql": "WITH account_averages AS (SELECT account_id, AVG(amount) as avg_amount FROM transactions GROUP BY account_id) SELECT t.id, t.account_id, t.amount, aa.avg_amount, ROUND(t.amount - aa.avg_amount, 2) as difference_from_avg, t.ts FROM transactions t INNER JOIN account_averages aa ON t.account_id = aa.account_id WHERE t.ts >= CURRENT_DATE - INTERVAL '30 days' ORDER BY ABS(t.amount - aa.avg_amount) DESC;", "difficulty": "medium", "schema": "finance", "query_type": "cte", "tables_used": ["transactions"], "schema_context": "accounts(id, holder_name, type, balance, opened_at, status)\n transactions(id, account_id, type, amount, description, ts, category)\n loans(id, account_id, principal, interest_rate, term_months, issued_at, status)\n loan_payments(id, loan_id, amount, paid_at, remaining_balance)\n fraud_flags(id, transaction_id, reason, flagged_at, resolved)"}
{"id": "10a1278f49faf211", "question": "Find all customers from 'United States' who placed more than 3 orders in 2023, showing customer name and order count", "sql": "SELECT c.name, COUNT(o.id) as order_count FROM customers c INNER JOIN orders o ON c.id = o.customer_id WHERE c.country = 'United States' AND EXTRACT(YEAR FROM o.order_date) = 2023 GROUP BY c.id, c.name HAVING COUNT(o.id) > 3 ORDER BY order_count DESC", "difficulty": "medium", "schema": "ecommerce", "query_type": "aggregate", "tables_used": ["customers", "orders"], "schema_context": "customers(id, name, email, country, created_at)\n products(id, name, category, price, stock, supplier_id)\n orders(id, customer_id, order_date, status, total)\n order_items(id, order_id, product_id, quantity, unit_price)\n suppliers(id, name, country, rating)"}
{"id": "6179920d8a6ba8eb", "question": "Find patients who have had appointments with the same doctor more than 3 times in the past year, showing appointment frequency and last visit date.", "sql": "SELECT p.id, p.name, d.id, d.name as doctor_name, COUNT(a.id) as appointment_count, MAX(a.scheduled_at) as last_visit FROM patients p INNER JOIN appointments a ON p.id = a.patient_id INNER JOIN doctors d ON a.doctor_id = d.id WHERE a.scheduled_at >= CURRENT_DATE - INTERVAL '1 year' GROUP BY p.id, p.name, d.id, d.name HAVING COUNT(a.id) > 3 ORDER BY appointment_count DESC;", "difficulty": "medium", "schema": "healthcare", "query_type": "aggregate", "tables_used": ["patients", "appointments", "doctors"], "schema_context": "patients(id, name, dob, gender, insurance_id)\n doctors(id, name, specialty, dept_id, npi)\n appointments(id, patient_id, doctor_id, scheduled_at, status, notes)\n diagnoses(id, patient_id, doctor_id, icd_code, description, diagnosed_at)\n prescriptions(id, patient_id, doctor_id, drug_name, dosage, start_date, end_date)"}
{"id": "91a1af26265b064c", "question": "List all active accounts with their total transaction amount in the last 90 days, ordered by transaction volume descending.", "sql": "SELECT a.id, a.holder_name, a.type, COUNT(t.id) as transaction_count, COALESCE(SUM(t.amount), 0) as total_amount FROM accounts a LEFT JOIN transactions t ON a.id = t.account_id AND t.ts >= CURRENT_DATE - INTERVAL '90 days' WHERE a.status = 'active' GROUP BY a.id, a.holder_name, a.type ORDER BY total_amount DESC;", "difficulty": "medium", "schema": "finance", "query_type": "aggregate", "tables_used": ["accounts", "transactions"], "schema_context": "accounts(id, holder_name, type, balance, opened_at, status)\n transactions(id, account_id, type, amount, description, ts, category)\n loans(id, account_id, principal, interest_rate, term_months, issued_at, status)\n loan_payments(id, loan_id, amount, paid_at, remaining_balance)\n fraud_flags(id, transaction_id, reason, flagged_at, resolved)"}
{"id": "e31129af6a81bc3c", "question": "List purchase orders with status 'pending' that were created more than 15 days ago, showing the warehouse location and total items in each order.", "sql": "SELECT po.id, po.created_at, w.location, po.status, po.total, COUNT(pol.id) as item_count FROM purchase_orders po LEFT JOIN warehouses w ON po.warehouse_id = w.id LEFT JOIN po_lines pol ON po.id = pol.po_id WHERE po.status = 'pending' AND po.created_at < CURRENT_DATE - INTERVAL '15 days' GROUP BY po.id, w.location, po.status, po.created_at, po.total ORDER BY po.created_at;", "difficulty": "medium", "schema": "inventory", "query_type": "aggregate", "tables_used": ["purchase_orders", "warehouses", "po_lines"], "schema_context": "warehouses(id, name, location, capacity, manager_id)\n items(id, sku, name, category, unit_cost, reorder_point)\n stock_levels(warehouse_id, item_id, quantity, last_updated)\n purchase_orders(id, supplier_id, warehouse_id, created_at, status, total)\n po_lines(id, po_id, item_id, quantity, unit_price)"}
{"id": "d5a8123f51749106", "question": "Show the most recent stock update for each item, along with the warehouse name and the days since last update.", "sql": "SELECT i.id, i.sku, i.name, w.name as warehouse_name, sl.quantity, sl.last_updated, EXTRACT(DAY FROM CURRENT_TIMESTAMP - sl.last_updated) as days_since_update FROM items i INNER JOIN stock_levels sl ON i.id = sl.item_id INNER JOIN warehouses w ON sl.warehouse_id = w.id WHERE (i.id, sl.last_updated) IN (SELECT item_id, MAX(last_updated) FROM stock_levels GROUP BY item_id) ORDER BY sl.last_updated DESC;", "difficulty": "medium", "schema": "inventory", "query_type": "subquery", "tables_used": ["items", "stock_levels", "warehouses"], "schema_context": "warehouses(id, name, location, capacity, manager_id)\n items(id, sku, name, category, unit_cost, reorder_point)\n stock_levels(warehouse_id, item_id, quantity, last_updated)\n purchase_orders(id, supplier_id, warehouse_id, created_at, status, total)\n po_lines(id, po_id, item_id, quantity, unit_price)"}
{"id": "c23ca0af94d4e33d", "question": "List all 'cancelled' orders with customer name.", "sql": "SELECT o.order_id, c.name, o.order_date, o.total_amount\nFROM orders o JOIN customers c ON o.customer_id = c.customer_id\nWHERE o.status = 'cancelled';", "difficulty": "medium", "schema": "ecommerce", "query_type": "join", "tables_used": ["orders", "customers"], "schema_context": " customers(customer_id, name, email, city, country, created_at, total_spent)\n orders(order_id, customer_id, order_date, status, total_amount, shipping_city)\n order_items(item_id, order_id, product_id, quantity, unit_price)\n products(product_id, name, category, price, stock_quantity, supplier_id)\n suppliers(supplier_id, name, country, rating)"}
{"id": "135ce9256d070151", "question": "List all projects that are over budget relative to the department budget.", "sql": "SELECT p.name AS project_name, p.budget AS project_budget, d.budget AS dept_budget\nFROM projects p JOIN departments d ON p.department_id = d.department_id\nWHERE p.budget > d.budget;", "difficulty": "medium", "schema": "hr", "query_type": "join", "tables_used": ["projects", "departments"], "schema_context": " employees(employee_id, name, department_id, manager_id, hire_date, salary, city)\n departments(department_id, name, location, budget, head_count)\n projects(project_id, name, department_id, start_date, end_date, budget)\n assignments(assignment_id, employee_id, project_id, role, hours_per_week)\n performance_reviews(review_id, employee_id, review_year, rating, bonus)"}
{"id": "069fcbfead445bbb", "question": "Show patients who have been prescribed the same drug multiple times by different doctors, with counts of prescriptions per drug.", "sql": "SELECT p.id, p.name, pr.drug_name, COUNT(DISTINCT pr.doctor_id) as doctor_count, COUNT(pr.id) as total_prescriptions FROM patients p INNER JOIN prescriptions pr ON p.id = pr.patient_id GROUP BY p.id, p.name, pr.drug_name HAVING COUNT(DISTINCT pr.doctor_id) > 1 ORDER BY total_prescriptions DESC;", "difficulty": "medium", "schema": "healthcare", "query_type": "aggregate", "tables_used": ["patients", "prescriptions"], "schema_context": "patients(id, name, dob, gender, insurance_id)\n doctors(id, name, specialty, dept_id, npi)\n appointments(id, patient_id, doctor_id, scheduled_at, status, notes)\n diagnoses(id, patient_id, doctor_id, icd_code, description, diagnosed_at)\n prescriptions(id, patient_id, doctor_id, drug_name, dosage, start_date, end_date)"}
{"id": "90b45c8199a485e4", "question": "Find pairs of users who follow each other (mutual follows) and compare their follower counts.", "sql": "SELECT f1.follower_id AS user_a, u1.username AS user_a_name, u1.follower_count AS user_a_followers, f1.following_id AS user_b, u2.username AS user_b_name, u2.follower_count AS user_b_followers FROM follows f1 INNER JOIN follows f2 ON f1.follower_id = f2.following_id AND f1.following_id = f2.follower_id INNER JOIN users u1 ON f1.follower_id = u1.id INNER JOIN users u2 ON f1.following_id = u2.id WHERE f1.follower_id < f1.following_id ORDER BY u1.follower_count DESC;", "difficulty": "medium", "schema": "social", "query_type": "self_join", "tables_used": ["follows", "users"], "schema_context": "users(id, username, email, created_at, bio, follower_count)\n posts(id, user_id, content, created_at, likes, shares, is_deleted)\n comments(id, post_id, user_id, content, created_at, likes)\n follows(follower_id, following_id, followed_at)\n hashtags(id, tag)\n post_hashtags(post_id, hashtag_id)"}
{"id": "03828ae54705152c", "question": "What is the total stock value for 'Raw Materials' items?", "sql": "SELECT SUM(s.quantity * i.unit_cost) AS total_value\nFROM stock s JOIN items i ON s.item_id = i.item_id\nWHERE i.category = 'Raw Materials';", "difficulty": "medium", "schema": "inventory", "query_type": "join", "tables_used": ["stock", "items"], "schema_context": " warehouses(warehouse_id, name, city, country, capacity)\n items(item_id, name, sku, category, unit_cost, reorder_point)\n stock(stock_id, item_id, warehouse_id, quantity, last_updated)\n movements(movement_id, item_id, warehouse_id, movement_type, quantity, movement_date)\n vendors(vendor_id, name, country, lead_time_days, reliability_score)"}
{"id": "3aa9fdf5de1bc0e7", "question": "Compare event type distribution between treatment and control variants, showing percentage change in event frequency.", "sql": "WITH variant_events AS (SELECT ab.variant, e.event_type, COUNT(*) as event_count FROM events e INNER JOIN ab_experiments ab ON e.user_id = ab.user_id GROUP BY ab.variant, e.event_type) SELECT event_type, MAX(CASE WHEN variant = 'treatment' THEN event_count ELSE 0 END) as treatment_count, MAX(CASE WHEN variant = 'control' THEN event_count ELSE 0 END) as control_count, ROUND((MAX(CASE WHEN variant = 'treatment' THEN event_count ELSE 0 END)::numeric - MAX(CASE WHEN variant = 'control' THEN event_count ELSE 0 END)) / NULLIF(MAX(CASE WHEN variant = 'control' THEN event_count ELSE 0 END), 0) * 100, 2) as pct_change FROM variant_events GROUP BY event_type HAVING MAX(CASE WHEN variant = 'control' THEN event_count ELSE 0 END) > 0 ORDER BY ABS(ROUND((MAX(CASE WHEN variant = 'treatment' THEN event_count ELSE 0 END)::numeric - MAX(CASE WHEN variant = 'control' THEN event_count ELSE 0 END)) / NULLIF(MAX(CASE WHEN variant = 'control' THEN event_count ELSE 0 END), 0) * 100, 2)) DESC LIMIT 25;", "difficulty": "medium", "schema": "analytics", "query_type": "cte", "tables_used": ["events", "ab_experiments"], "schema_context": "events(id, session_id, user_id, event_type, page, ts, properties)\n users(id, created_at, plan, country, referral_source)\n sessions(id, user_id, started_at, ended_at, device)\n conversions(id, user_id, goal, value, converted_at)\n ab_experiments(id, name, variant, user_id, enrolled_at)"}
{"id": "98b2e1339dfa9354", "question": "Find premium users who had zero conversions but had more than 10 sessions in the last 60 days, along with their session metrics.", "sql": "SELECT u.id, u.created_at, COUNT(DISTINCT s.id) as total_sessions, COUNT(DISTINCT e.id) as total_events, ROUND(AVG(EXTRACT(EPOCH FROM (s.ended_at - s.started_at))/60), 2) as avg_session_duration FROM users u INNER JOIN sessions s ON u.id = s.user_id LEFT JOIN events e ON s.id = e.session_id WHERE u.plan = 'premium' AND s.started_at >= NOW() - INTERVAL '60 days' AND u.id NOT IN (SELECT user_id FROM conversions) GROUP BY u.id, u.created_at HAVING COUNT(DISTINCT s.id) > 10 ORDER BY total_events DESC;", "difficulty": "medium", "schema": "analytics", "query_type": "subquery", "tables_used": ["users", "sessions", "events", "conversions"], "schema_context": "events(id, session_id, user_id, event_type, page, ts, properties)\n users(id, created_at, plan, country, referral_source)\n sessions(id, user_id, started_at, ended_at, device)\n conversions(id, user_id, goal, value, converted_at)\n ab_experiments(id, name, variant, user_id, enrolled_at)"}
{"id": "add7177825cf2909", "question": "Show all loans with their next expected payment due date and days until due, considering the last payment made.", "sql": "SELECT l.id, a.holder_name, l.principal, l.term_months, l.issued_at, l.status, COALESCE(lp_last.paid_at, l.issued_at) as last_payment_date, l.issued_at + (INTERVAL '1 month' * ROW_NUMBER() OVER (PARTITION BY l.id ORDER BY lp_last.paid_at)) as next_due_date, EXTRACT(DAY FROM (l.issued_at + (INTERVAL '1 month' * ROW_NUMBER() OVER (PARTITION BY l.id ORDER BY lp_last.paid_at)) - CURRENT_DATE)) as days_until_due FROM loans l INNER JOIN accounts a ON l.account_id = a.id LEFT JOIN (SELECT DISTINCT ON (loan_id) loan_id, paid_at FROM loan_payments ORDER BY loan_id, paid_at DESC) lp_last ON l.id = lp_last.loan_id WHERE l.status = 'active' LIMIT 100;", "difficulty": "medium", "schema": "finance", "query_type": "window", "tables_used": ["loans", "accounts", "loan_payments"], "schema_context": "accounts(id, holder_name, type, balance, opened_at, status)\n transactions(id, account_id, type, amount, description, ts, category)\n loans(id, account_id, principal, interest_rate, term_months, issued_at, status)\n loan_payments(id, loan_id, amount, paid_at, remaining_balance)\n fraud_flags(id, transaction_id, reason, flagged_at, resolved)"}
{"id": "328c556bf8eacaed", "question": "For each department, calculate the rank of employees by salary and show the cumulative sum of salaries, then identify employees earning above the department's median salary", "sql": "WITH dept_salary_ranks AS (\n SELECT e.id, e.name, e.dept_id, e.salary, d.name as dept_name,\n RANK() OVER (PARTITION BY e.dept_id ORDER BY e.salary DESC) as salary_rank,\n SUM(e.salary) OVER (PARTITION BY e.dept_id ORDER BY e.salary DESC) as cumulative_salary,\n PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY e.salary) OVER (PARTITION BY e.dept_id) as median_salary\n FROM employees e\n JOIN departments d ON e.dept_id = d.id\n)\nSELECT id, name, dept_name, salary, salary_rank, cumulative_salary, median_salary,\n CASE WHEN salary > median_salary THEN 'Above Median' ELSE 'Below Median' END as salary_position\nFROM dept_salary_ranks\nWHERE salary > median_salary\nORDER BY dept_id, salary_rank;", "difficulty": "hard", "schema": "hr", "query_type": "window", "tables_used": ["employees", "departments"], "schema_context": "employees(id, name, email, dept_id, manager_id, hire_date, salary)\n departments(id, name, location, budget)\n projects(id, name, start_date, end_date, dept_id, status)\n project_assignments(employee_id, project_id, role, hours_allocated)\n performance_reviews(id, employee_id, reviewer_id, year, score, notes)"}
{"id": "f5d8b9196615901e", "question": "Find the cohort of organizations created in Q1 2024 that have never used a particular feature, cross-reference with those who cancelled subscriptions, and show the intersection of both groups", "sql": "WITH q1_2024_orgs AS (\n SELECT id, name, created_at, mrr\n FROM organizations\n WHERE created_at >= '2024-01-01' AND created_at < '2024-04-01'\n),\nfeature_users AS (\n SELECT DISTINCT org_id\n FROM feature_usage\n WHERE feature = 'advanced_analytics'\n),\ncancelled_orgs AS (\n SELECT DISTINCT org_id\n FROM subscriptions\n WHERE ended_at IS NOT NULL AND ended_at > NOW() - INTERVAL '12 months'\n),\nq1_non_feature_users AS (\n SELECT q1.id, q1.name, q1.created_at, q1.mrr\n FROM q1_2024_orgs q1\n WHERE q1.id NOT IN (SELECT org_id FROM feature_users)\n),\ncommon_orgs AS (\n SELECT qnf.id, qnf.name, qnf.created_at, qnf.mrr\n FROM q1_non_feature_users qnf\n INTERSECT\n SELECT q1.id, q1.name, q1.created_at, q1.mrr\n FROM q1_2024_orgs q1\n WHERE q1.id IN (SELECT org_id FROM cancelled_orgs)\n)\nSELECT\n co.id,\n co.name,\n co.created_at,\n co.mrr,\n DATEDIFF(day, co.created_at::date, NOW()::date) as days_since_creation,\n 'Never used advanced_analytics and cancelled' as reason\nFROM common_orgs co\nUNION\nSELECT\n q1.id,\n q1.name,\n q1.created_at,\n q1.mrr,\n DATEDIFF(day, q1.created_at::date, NOW()::date) as days_since_creation,\n 'Never used advanced_analytics (still active)' as reason\nFROM q1_2024_orgs q1\nWHERE q1.id NOT IN (SELECT org_id FROM feature_users)\nAND q1.id NOT IN (SELECT org_id FROM cancelled_orgs)\nORDER BY created_at DESC;", "difficulty": "hard", "schema": "saas", "query_type": "union", "tables_used": ["organizations", "subscriptions", "feature_usage"], "schema_context": "organizations(id, name, plan, created_at, mrr, employee_count)\n users(id, org_id, email, role, created_at, last_active)\n subscriptions(id, org_id, plan, started_at, ended_at, seats, mrr)\n feature_usage(id, org_id, feature, used_at, user_id, count)\n support_tickets(id, org_id, user_id, subject, priority, status, created_at, resolved_at)"}
{"id": "eaa391c53a110cb6", "question": "Rank products by price within each category using a window function.", "sql": "SELECT name, category, price,\nRANK() OVER (PARTITION BY category ORDER BY price DESC) AS price_rank\nFROM products;", "difficulty": "hard", "schema": "ecommerce", "query_type": "window", "tables_used": ["products"], "schema_context": " customers(customer_id, name, email, city, country, created_at, total_spent)\n orders(order_id, customer_id, order_date, status, total_amount, shipping_city)\n order_items(item_id, order_id, product_id, quantity, unit_price)\n products(product_id, name, category, price, stock_quantity, supplier_id)\n suppliers(supplier_id, name, country, rating)"}
{"id": "4554d389ef8603e5", "question": "Identify users whose device changed between sessions in the last 30 days, showing their session history and any conversion activity", "sql": "WITH session_devices AS (\n SELECT user_id, id as session_id, device,\n started_at,\n LAG(device) OVER (PARTITION BY user_id ORDER BY started_at) as prev_device\n FROM sessions\n WHERE started_at >= NOW() - INTERVAL '30 days'\n), device_changers AS (\n SELECT DISTINCT user_id\n FROM session_devices\n WHERE device != prev_device AND prev_device IS NOT NULL\n)\nSELECT sd.user_id, u.plan, u.country, u.created_at,\n sd.session_id, sd.device, sd.prev_device, sd.started_at,\n COUNT(e.id) as events_in_session,\n COUNT(DISTINCT c.id) as conversions_around_session\nFROM session_devices sd\nINNER JOIN device_changers dc ON sd.user_id = dc.user_id\nINNER JOIN users u ON sd.user_id = u.id\nLEFT JOIN events e ON sd.session_id = e.session_id\nLEFT JOIN conversions c ON sd.user_id = c.user_id AND c.converted_at BETWEEN sd.started_at - INTERVAL '1 hour' AND sd.started_at + INTERVAL '1 hour'\nWHERE sd.device != sd.prev_device\nGROUP BY sd.user_id, u.plan, u.country, u.created_at, sd.session_id, sd.device, sd.prev_device, sd.started_at\nORDER BY sd.user_id, sd.started_at DESC;", "difficulty": "hard", "schema": "analytics", "query_type": "window", "tables_used": ["sessions", "users", "events", "conversions"], "schema_context": "events(id, session_id, user_id, event_type, page, ts, properties)\n users(id, created_at, plan, country, referral_source)\n sessions(id, user_id, started_at, ended_at, device)\n conversions(id, user_id, goal, value, converted_at)\n ab_experiments(id, name, variant, user_id, enrolled_at)"}
{"id": "a499ed6942d2507a", "question": "Find pairs of employees in the same department who earn within 5000 of each other.", "sql": "SELECT a.name AS employee1, b.name AS employee2, a.salary AS sal1, b.salary AS sal2\nFROM employees a\nJOIN employees b ON a.department_id = b.department_id\nAND a.employee_id < b.employee_id\nAND ABS(a.salary - b.salary) <= 5000\nORDER BY a.name;", "difficulty": "hard", "schema": "hr", "query_type": "join", "tables_used": ["employees"], "schema_context": " employees(employee_id, name, department_id, manager_id, hire_date, salary, city)\n departments(department_id, name, location, budget, head_count)\n projects(project_id, name, department_id, start_date, end_date, budget)\n assignments(assignment_id, employee_id, project_id, role, hours_per_week)\n performance_reviews(review_id, employee_id, review_year, rating, bonus)"}
{"id": "e3293c812c886586", "question": "Identify which specialty combinations (pairs of doctors from different specialties) have treated overlapping patient populations, and count the shared patients.", "sql": "WITH doctor_patients AS (\n SELECT DISTINCT\n d.id as doctor_id,\n d.name as doctor_name,\n d.specialty,\n a.patient_id\n FROM doctors d\n JOIN appointments a ON d.id = a.doctor_id\n WHERE a.status = 'completed'\n),\nspecialty_pairs AS (\n SELECT \n dp1.doctor_id as doctor1_id,\n dp1.doctor_name as doctor1_name,\n dp1.specialty as specialty1,\n dp2.doctor_id as doctor2_id,\n dp2.doctor_name as doctor2_name,\n dp2.specialty as specialty2,\n COUNT(DISTINCT dp1.patient_id) as shared_patients\n FROM doctor_patients dp1\n JOIN doctor_patients dp2 ON dp1.patient_id = dp2.patient_id \n AND dp1.doctor_id < dp2.doctor_id\n AND dp1.specialty < dp2.specialty\n GROUP BY dp1.doctor_id, dp1.doctor_name, dp1.specialty, \n dp2.doctor_id, dp2.doctor_name, dp2.specialty\n HAVING COUNT(DISTINCT dp1.patient_id) >= 2\n)\nSELECT \n doctor1_id,\n doctor1_name,\n specialty1,\n doctor2_id,\n doctor2_name,\n specialty2,\n shared_patients,\n ROW_NUMBER() OVER (ORDER BY shared_patients DESC) as rank\nFROM specialty_pairs\nORDER BY shared_patients DESC, specialty1, specialty2;", "difficulty": "hard", "schema": "healthcare", "query_type": "self_join", "tables_used": ["doctors", "appointments"], "schema_context": "patients(id, name, dob, gender, insurance_id)\n doctors(id, name, specialty, dept_id, npi)\n appointments(id, patient_id, doctor_id, scheduled_at, status, notes)\n diagnoses(id, patient_id, doctor_id, icd_code, description, diagnosed_at)\n prescriptions(id, patient_id, doctor_id, drug_name, dosage, start_date, end_date)"}
{"id": "3dabbad25989d09e", "question": "Find the top 5 most engaged users (by total likes on their posts and comments combined) who have at least 10 followers and posted in the last 90 days, along with their engagement scores.", "sql": "WITH user_engagement AS (\n SELECT \n u.id,\n u.username,\n u.follower_count,\n COALESCE(SUM(p.likes), 0) + COALESCE(SUM(c.likes), 0) as total_engagement,\n COUNT(DISTINCT p.id) as post_count\n FROM users u\n LEFT JOIN posts p ON u.id = p.user_id AND p.created_at > NOW() - INTERVAL '90 days' AND p.is_deleted = false\n LEFT JOIN comments c ON u.id = c.user_id AND c.created_at > NOW() - INTERVAL '90 days'\n WHERE u.follower_count >= 10\n GROUP BY u.id, u.username, u.follower_count\n HAVING COUNT(DISTINCT p.id) > 0 OR COUNT(DISTINCT c.id) > 0\n)\nSELECT \n id,\n username,\n follower_count,\n total_engagement,\n post_count,\n RANK() OVER (ORDER BY total_engagement DESC) as engagement_rank\nFROM user_engagement\nORDER BY total_engagement DESC\nLIMIT 5;", "difficulty": "hard", "schema": "social", "query_type": "cte", "tables_used": ["users", "posts", "comments"], "schema_context": "users(id, username, email, created_at, bio, follower_count)\n posts(id, user_id, content, created_at, likes, shares, is_deleted)\n comments(id, post_id, user_id, content, created_at, likes)\n follows(follower_id, following_id, followed_at)\n hashtags(id, tag)\n post_hashtags(post_id, hashtag_id)"}
{"id": "def66a4468873ad1", "question": "Using a CTE, identify patients with more than 3 appointments and list their diagnoses.", "sql": "WITH frequent_patients AS (\nSELECT patient_id, COUNT(*) AS visit_count\nFROM appointments\nGROUP BY patient_id\nHAVING COUNT(*) > 3\n)\nSELECT p.name, a.appointment_date, a.diagnosis\nFROM appointments a\nJOIN patients p ON a.patient_id = p.patient_id\nWHERE a.patient_id IN (SELECT patient_id FROM frequent_patients)\nORDER BY p.name, a.appointment_date;", "difficulty": "hard", "schema": "healthcare", "query_type": "cte", "tables_used": ["appointments", "patients", "frequent_patients"], "schema_context": " patients(patient_id, name, date_of_birth, gender, city, insurance_type)\n doctors(doctor_id, name, specialty, hospital_id, years_experience)\n appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis)\n hospitals(hospital_id, name, city, beds, rating)\n prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)"}
{"id": "4ad4bf984d4c9a02", "question": "Identify suppliers who have had purchase orders rejected and compare their average unit prices to the company average for the same items, showing the price difference and deviation percentage.", "sql": "WITH rejected_orders AS (\n SELECT DISTINCT po.supplier_id\n FROM purchase_orders po\n WHERE po.status = 'rejected'\n),\nsupplier_prices AS (\n SELECT\n po.supplier_id,\n pl.item_id,\n AVG(pl.unit_price) as supplier_avg_price\n FROM purchase_orders po\n JOIN po_lines pl ON po.id = pl.po_id\n WHERE po.supplier_id IN (SELECT supplier_id FROM rejected_orders)\n GROUP BY po.supplier_id, pl.item_id\n),\ncompany_avg AS (\n SELECT\n item_id,\n AVG(unit_price) as company_avg_price\n FROM po_lines\n GROUP BY item_id\n)\nSELECT\n sp.supplier_id,\n sp.item_id,\n i.name as item_name,\n ROUND(sp.supplier_avg_price::NUMERIC, 2) as supplier_avg_price,\n ROUND(ca.company_avg_price::NUMERIC, 2) as company_avg_price,\n ROUND((sp.supplier_avg_price - ca.company_avg_price)::NUMERIC, 2) as price_difference,\n ROUND((((sp.supplier_avg_price - ca.company_avg_price) / ca.company_avg_price) * 100)::NUMERIC, 2) as deviation_percent\nFROM supplier_prices sp\nJOIN company_avg ca ON sp.item_id = ca.item_id\nJOIN items i ON sp.item_id = i.id\nORDER BY sp.supplier_id, deviation_percent DESC;", "difficulty": "hard", "schema": "inventory", "query_type": "cte", "tables_used": ["purchase_orders", "po_lines", "items"], "schema_context": "warehouses(id, name, location, capacity, manager_id)\n items(id, sku, name, category, unit_cost, reorder_point)\n stock_levels(warehouse_id, item_id, quantity, last_updated)\n purchase_orders(id, supplier_id, warehouse_id, created_at, status, total)\n po_lines(id, po_id, item_id, quantity, unit_price)"}
{"id": "078fd7a1955f5d7b", "question": "Find users who have not logged any event in the past 30 days.", "sql": "SELECT u.user_id, u.username\nFROM users u WHERE u.user_id NOT IN (\nSELECT DISTINCT user_id FROM events\nWHERE event_date >= CURRENT_DATE - INTERVAL '30 days'\n);", "difficulty": "hard", "schema": "analytics", "query_type": "subquery", "tables_used": ["users", "events"], "schema_context": " events(event_id, user_id, event_type, event_date, page, session_id, duration_seconds)\n users(user_id, username, signup_date, plan, country, is_active)\n sessions(session_id, user_id, start_time, end_time, device, browser)\n pages(page_id, url, title, category, created_at)\n conversions(conversion_id, user_id, event_date, revenue, campaign_id)"}
{"id": "910a682f1c466f5b", "question": "Find patients who have seen at least one doctor in every specialty at the hospital.", "sql": "SELECT p.name\nFROM patients p\nWHERE NOT EXISTS (\nSELECT 1 FROM doctors d\nWHERE NOT EXISTS (\nSELECT 1 FROM appointments a\nWHERE a.patient_id = p.patient_id AND a.doctor_id = d.doctor_id\n)\n);", "difficulty": "hard", "schema": "healthcare", "query_type": "subquery", "tables_used": ["patients", "doctors", "appointments"], "schema_context": " patients(patient_id, name, date_of_birth, gender, city, insurance_type)\n doctors(doctor_id, name, specialty, hospital_id, years_experience)\n appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis)\n hospitals(hospital_id, name, city, beds, rating)\n prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)"}
{"id": "03f7a373062a85ef", "question": "Find users who made a conversion but had zero events logged in their session where the conversion was recorded - identify incomplete session tracking", "sql": "SELECT DISTINCT c.user_id, c.goal, c.value, c.converted_at,\n s.id as session_id, s.started_at, s.ended_at, s.device,\n COUNT(e.id) as event_count\nFROM conversions c\nLEFT JOIN sessions s ON c.user_id = s.user_id \n AND c.converted_at BETWEEN s.started_at AND s.ended_at\nLEFT JOIN events e ON s.id = e.session_id\nWHERE s.id IS NOT NULL\nGROUP BY c.id, c.user_id, c.goal, c.value, c.converted_at, s.id, s.started_at, s.ended_at, s.device\nHAVING COUNT(e.id) = 0\nORDER BY c.converted_at DESC;", "difficulty": "hard", "schema": "analytics", "query_type": "join", "tables_used": ["conversions", "sessions", "events"], "schema_context": "events(id, session_id, user_id, event_type, page, ts, properties)\n users(id, created_at, plan, country, referral_source)\n sessions(id, user_id, started_at, ended_at, device)\n conversions(id, user_id, goal, value, converted_at)\n ab_experiments(id, name, variant, user_id, enrolled_at)"}
{"id": "7fcd5d17b06b866e", "question": "Create a retention cohort analysis for organizations grouped by creation month, tracking which months had subscriptions continue vs cancel, and calculate month-over-month retention rates", "sql": "WITH org_cohorts AS (\n SELECT\n o.id,\n o.name,\n DATE_TRUNC('month', o.created_at)::date as cohort_month,\n s.started_at,\n s.ended_at,\n EXTRACT(YEAR_MONTH FROM AGE(COALESCE(s.ended_at, NOW()), o.created_at)) as months_since_signup\n FROM organizations o\n LEFT JOIN subscriptions s ON o.id = s.org_id\n),\ncohort_status AS (\n SELECT\n cohort_month,\n months_since_signup,\n COUNT(DISTINCT CASE WHEN ended_at IS NULL THEN id ELSE NULL END) as active_count,\n COUNT(DISTINCT CASE WHEN ended_at IS NOT NULL THEN id ELSE NULL END) as churned_count,\n COUNT(DISTINCT id) as total_cohort\n FROM org_cohorts\n WHERE months_since_signup >= 0 AND months_since_signup <= 12\n GROUP BY cohort_month, months_since_signup\n),\ncohort_retention AS (\n SELECT\n cohort_month,\n months_since_signup,\n active_count,\n churned_count,\n total_cohort,\n ROUND((active_count * 100.0 / NULLIF(total_cohort, 0))::numeric, 2) as retention_rate,\n LAG(active_count) OVER (PARTITION BY cohort_month ORDER BY months_since_signup) as previous_month_active,\n ROUND(\n (active_count * 100.0 / NULLIF(LAG(active_count) OVER (PARTITION BY cohort_month ORDER BY months_since_signup), 0))::numeric, 2\n ) as mom_retention_rate\n FROM cohort_status\n)\nSELECT\n cohort_month,\n months_since_signup,\n total_cohort,\n active_count,\n churned_count,\n retention_rate,\n COALESCE(mom_retention_rate, 100) as month_over_month_retention,\n CASE\n WHEN retention_rate >= 80 THEN 'Excellent'\n WHEN retention_rate >= 60 THEN 'Good'\n WHEN retention_rate >= 40 THEN 'Fair'\n ELSE 'Poor'\n END as retention_quality\nFROM cohort_retention\nWHERE months_since_signup IS NOT NULL\nORDER BY cohort_month DESC, months_since_signup ASC;", "difficulty": "hard", "schema": "saas", "query_type": "window", "tables_used": ["organizations", "subscriptions"], "schema_context": "organizations(id, name, plan, created_at, mrr, employee_count)\n users(id, org_id, email, role, created_at, last_active)\n subscriptions(id, org_id, plan, started_at, ended_at, seats, mrr)\n feature_usage(id, org_id, feature, used_at, user_id, count)\n support_tickets(id, org_id, user_id, subject, priority, status, created_at, resolved_at)"}