question,sql,schema_context,domain,difficulty,query_type How many customers are from France?,SELECT COUNT(*) AS customer_count FROM customers WHERE country = 'France';," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,easy,aggregate How many products are in the 'Books' category?,SELECT COUNT(*) FROM products WHERE category = 'Books';," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,easy,aggregate How many orders were placed in 2022?,SELECT COUNT(*) AS order_count FROM orders WHERE EXTRACT(YEAR FROM order_date) = 2022;," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,easy,aggregate What is the total revenue from all orders?,SELECT SUM(total_amount) AS total_revenue FROM orders;," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,easy,aggregate What is the total revenue from 'cancelled' orders?,SELECT SUM(total_amount) AS revenue FROM orders WHERE status = 'cancelled';," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,easy,aggregate How many female patients are there?,SELECT COUNT(*) FROM patients WHERE gender = 'F';," patients(patient_id, name, date_of_birth, gender, city, insurance_type) doctors(doctor_id, name, specialty, hospital_id, years_experience) appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis) hospitals(hospital_id, name, city, beds, rating) prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)",healthcare,easy,aggregate Show all doctors with specialty 'Cardiology'.,SELECT * FROM doctors WHERE specialty = 'Cardiology';," patients(patient_id, name, date_of_birth, gender, city, insurance_type) doctors(doctor_id, name, specialty, hospital_id, years_experience) appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis) hospitals(hospital_id, name, city, beds, rating) prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)",healthcare,easy,select List prescriptions with duration longer than 30 days.,SELECT * FROM prescriptions WHERE duration_days > 30;," patients(patient_id, name, date_of_birth, gender, city, insurance_type) doctors(doctor_id, name, specialty, hospital_id, years_experience) appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis) hospitals(hospital_id, name, city, beds, rating) prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)",healthcare,easy,select How many 'logout' events occurred in the last 30 days?,"SELECT COUNT(*) FROM events WHERE event_type = 'logout' AND event_date >= CURRENT_DATE - INTERVAL '30 days';"," events(event_id, user_id, event_type, event_date, page, session_id, duration_seconds) users(user_id, username, signup_date, plan, country, is_active) sessions(session_id, user_id, start_time, end_time, device, browser) pages(page_id, url, title, category, created_at) conversions(conversion_id, user_id, event_date, revenue, campaign_id)",analytics,easy,aggregate How many distinct event types are there?,SELECT COUNT(DISTINCT event_type) AS event_type_count FROM events;," events(event_id, user_id, event_type, event_date, page, session_id, duration_seconds) users(user_id, username, signup_date, plan, country, is_active) sessions(session_id, user_id, start_time, end_time, device, browser) pages(page_id, url, title, category, created_at) conversions(conversion_id, user_id, event_date, revenue, campaign_id)",analytics,easy,aggregate What is the average bonus paid in 2023?,SELECT AVG(bonus) FROM performance_reviews WHERE review_year = 2023;," employees(employee_id, name, department_id, manager_id, hire_date, salary, city) departments(department_id, name, location, budget, head_count) projects(project_id, name, department_id, start_date, end_date, budget) assignments(assignment_id, employee_id, project_id, role, hours_per_week) performance_reviews(review_id, employee_id, review_year, rating, bonus)",hr,easy,aggregate Find the employee with the longest tenure.,"SELECT name, hire_date, CURRENT_DATE - hire_date AS tenure_days FROM employees ORDER BY hire_date ASC LIMIT 1;"," employees(employee_id, name, department_id, manager_id, hire_date, salary, city) departments(department_id, name, location, budget, head_count) projects(project_id, name, department_id, start_date, end_date, budget) assignments(assignment_id, employee_id, project_id, role, hours_per_week) performance_reviews(review_id, employee_id, review_year, rating, bonus)",hr,easy,select List all items in the 'Raw Materials' category.,SELECT * FROM items WHERE category = 'Raw Materials';," warehouses(warehouse_id, name, city, country, capacity) items(item_id, name, sku, category, unit_cost, reorder_point) stock(stock_id, item_id, warehouse_id, quantity, last_updated) movements(movement_id, item_id, warehouse_id, movement_type, quantity, movement_date) vendors(vendor_id, name, country, lead_time_days, reliability_score)",inventory,easy,select How many 'shipment' movements happened this month?,"SELECT COUNT(*) FROM movements WHERE movement_type = 'shipment' AND DATE_TRUNC('month', movement_date) = DATE_TRUNC('month', CURRENT_DATE);"," warehouses(warehouse_id, name, city, country, capacity) items(item_id, name, sku, category, unit_cost, reorder_point) stock(stock_id, item_id, warehouse_id, quantity, last_updated) movements(movement_id, item_id, warehouse_id, movement_type, quantity, movement_date) vendors(vendor_id, name, country, lead_time_days, reliability_score)",inventory,easy,aggregate Which month had the most orders in 2023?,"SELECT EXTRACT(MONTH FROM order_date) AS month, COUNT(*) AS orders FROM orders WHERE EXTRACT(YEAR FROM order_date) = 2023 GROUP BY month ORDER BY orders DESC LIMIT 1;"," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,medium,aggregate Find the customer who placed the most recent order.,"SELECT c.name, MAX(o.order_date) AS last_order FROM orders o JOIN customers c ON o.customer_id = c.customer_id GROUP BY c.customer_id, c.name ORDER BY last_order DESC LIMIT 1;"," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,medium,aggregate Find customers who have placed more than 5 orders.,"SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id HAVING COUNT(*) > 5 ORDER BY order_count DESC;"," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,medium,aggregate "How many patients does each hospital serve, by doctor specialty?","SELECT h.name AS hospital_name, d.specialty, COUNT(DISTINCT a.patient_id) AS patient_count FROM appointments a JOIN doctors d ON a.doctor_id = d.doctor_id JOIN hospitals h ON d.hospital_id = h.hospital_id GROUP BY h.name, d.specialty ORDER BY patient_count DESC;"," patients(patient_id, name, date_of_birth, gender, city, insurance_type) doctors(doctor_id, name, specialty, hospital_id, years_experience) appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis) hospitals(hospital_id, name, city, beds, rating) prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)",healthcare,medium,aggregate "Show all appointments with patient name, doctor name, and diagnosis.","SELECT a.appointment_id, p.name AS patient_name, d.name AS doctor_name, a.appointment_date, a.diagnosis FROM appointments a JOIN patients p ON a.patient_id = p.patient_id JOIN doctors d ON a.doctor_id = d.doctor_id;"," patients(patient_id, name, date_of_birth, gender, city, insurance_type) doctors(doctor_id, name, specialty, hospital_id, years_experience) appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis) hospitals(hospital_id, name, city, beds, rating) prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)",healthcare,medium,join Find all employees and their manager's name.,"SELECT e.name AS employee, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.employee_id ORDER BY m.name NULLS LAST, e.name;"," employees(employee_id, name, department_id, manager_id, hire_date, salary, city) departments(department_id, name, location, budget, head_count) projects(project_id, name, department_id, start_date, end_date, budget) assignments(assignment_id, employee_id, project_id, role, hours_per_week) performance_reviews(review_id, employee_id, review_year, rating, bonus)",hr,medium,join Which department had the highest average rating in 2024?,"SELECT d.name, AVG(pr.rating) AS avg_rating FROM performance_reviews pr JOIN employees e ON pr.employee_id = e.employee_id JOIN departments d ON e.department_id = d.department_id WHERE pr.review_year = 2024 GROUP BY d.name ORDER BY avg_rating DESC LIMIT 1;"," employees(employee_id, name, department_id, manager_id, hire_date, salary, city) departments(department_id, name, location, budget, head_count) projects(project_id, name, department_id, start_date, end_date, budget) assignments(assignment_id, employee_id, project_id, role, hours_per_week) performance_reviews(review_id, employee_id, review_year, rating, bonus)",hr,medium,aggregate What is the total revenue from 'free' plan users?,"SELECT SUM(c.revenue) FROM conversions c JOIN users u ON c.user_id = u.user_id WHERE u.plan = 'free';"," events(event_id, user_id, event_type, event_date, page, session_id, duration_seconds) users(user_id, username, signup_date, plan, country, is_active) sessions(session_id, user_id, start_time, end_time, device, browser) pages(page_id, url, title, category, created_at) conversions(conversion_id, user_id, event_date, revenue, campaign_id)",analytics,medium,join What is the total stock value for 'Consumables' items?,"SELECT SUM(s.quantity * i.unit_cost) AS total_value FROM stock s JOIN items i ON s.item_id = i.item_id WHERE i.category = 'Consumables';"," warehouses(warehouse_id, name, city, country, capacity) items(item_id, name, sku, category, unit_cost, reorder_point) stock(stock_id, item_id, warehouse_id, quantity, last_updated) movements(movement_id, item_id, warehouse_id, movement_type, quantity, movement_date) vendors(vendor_id, name, country, lead_time_days, reliability_score)",inventory,medium,join "Calculate a running total of order revenue per customer, ordered by date.","SELECT customer_id, order_date, total_amount, SUM(total_amount) OVER (PARTITION BY customer_id ORDER BY order_date) AS running_total FROM orders;"," customers(customer_id, name, email, city, country, created_at, total_spent) orders(order_id, customer_id, order_date, status, total_amount, shipping_city) order_items(item_id, order_id, product_id, quantity, unit_price) products(product_id, name, category, price, stock_quantity, supplier_id) suppliers(supplier_id, name, country, rating)",ecommerce,hard,window Find the specialty with the highest average patient age.,"SELECT d.specialty, AVG(EXTRACT(YEAR FROM AGE(p.date_of_birth))) AS avg_patient_age FROM appointments a JOIN doctors d ON a.doctor_id = d.doctor_id JOIN patients p ON a.patient_id = p.patient_id GROUP BY d.specialty ORDER BY avg_patient_age DESC LIMIT 1;"," patients(patient_id, name, date_of_birth, gender, city, insurance_type) doctors(doctor_id, name, specialty, hospital_id, years_experience) appointments(appointment_id, patient_id, doctor_id, appointment_date, status, diagnosis) hospitals(hospital_id, name, city, beds, rating) prescriptions(prescription_id, appointment_id, medication, dosage, duration_days)",healthcare,hard,aggregate "Using a CTE, find each department's headcount and its share of total company headcount.","WITH dept_counts AS ( SELECT department_id, COUNT(*) AS headcount FROM employees GROUP BY department_id ), total AS ( SELECT SUM(headcount) AS company_total FROM dept_counts ) SELECT d.name, dc.headcount, ROUND(100.0 * dc.headcount / t.company_total, 2) AS pct_of_company FROM dept_counts dc JOIN departments d ON dc.department_id = d.department_id CROSS JOIN total t ORDER BY dc.headcount DESC;"," employees(employee_id, name, department_id, manager_id, hire_date, salary, city) departments(department_id, name, location, budget, head_count) projects(project_id, name, department_id, start_date, end_date, budget) assignments(assignment_id, employee_id, project_id, role, hours_per_week) performance_reviews(review_id, employee_id, review_year, rating, bonus)",hr,hard,cte Find users who converted but never triggered a 'purchase' event.,"SELECT DISTINCT u.user_id, u.username FROM conversions c JOIN users u ON c.user_id = u.user_id WHERE u.user_id NOT IN ( SELECT user_id FROM events WHERE event_type = 'purchase' );"," events(event_id, user_id, event_type, event_date, page, session_id, duration_seconds) users(user_id, username, signup_date, plan, country, is_active) sessions(session_id, user_id, start_time, end_time, device, browser) pages(page_id, url, title, category, created_at) conversions(conversion_id, user_id, event_date, revenue, campaign_id)",analytics,hard,subquery Calculate inventory turnover per item (shipments / average stock).,"WITH shipped AS ( SELECT item_id, SUM(quantity) AS total_shipped FROM movements WHERE movement_type = 'shipment' GROUP BY item_id ), avg_stock AS ( SELECT item_id, AVG(quantity) AS avg_qty FROM stock GROUP BY item_id ) SELECT i.name, s.total_shipped, a.avg_qty, ROUND(s.total_shipped::numeric / NULLIF(a.avg_qty, 0), 2) AS turnover_ratio FROM items i JOIN shipped s ON i.item_id = s.item_id JOIN avg_stock a ON i.item_id = a.item_id ORDER BY turnover_ratio DESC;"," warehouses(warehouse_id, name, city, country, capacity) items(item_id, name, sku, category, unit_cost, reorder_point) stock(stock_id, item_id, warehouse_id, quantity, last_updated) movements(movement_id, item_id, warehouse_id, movement_type, quantity, movement_date) vendors(vendor_id, name, country, lead_time_days, reliability_score)",inventory,hard,cte