File size: 16,371 Bytes
606ac8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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