Average Salary by Department
Calculate the average salary for each department. Return columns: department_id, avg_salary. Order results by: department_id.
Practice aggregate functions, grouped reporting, and grain-safe metrics. These 9 interactive challenges use real schemas and instant query validation.
Calculate the average salary for each department. Return columns: department_id, avg_salary. Order results by: department_id.
Count how many orders have the status 'completed'. Return columns: completed_count.
Calculate the combined budget of all departments. Return columns: total_budget.
Calculate inventory value for products with fewer than 200 units. Return columns: name, stock, price, inventory_value. Order results by: inventory_value DESC.
For completed positive-quantity sales, calculate each region's weighted average selling price as SUM(quantity*unit_price)/SUM(quantity). Cancelled sales and return rows with negative quantity are excluded. Return region, units_sold, revenue and weighted_average_price rounded to 2 decimals. Return columns: region, units_sold, revenue, weighted_average_price.
For each month, count distinct customers with completed web activity, completed store activity, both channels, and exactly one channel. Repeated transactions must count a customer once; cancelled activity is excluded. Return columns: month, web_customers, store_customers, both_channels, exactly_one_channel.
Calculate each product's share of completed invoice revenue. Invoice lines define revenue; line_tags is one-to-many descriptive data and must not multiply line revenue. Return product_id, revenue and revenue_share. Products with multiple tags must remain at invoice-line grain. Return columns: product_id, revenue, revenue_share.
As of 2025-05-15, compare regional completed revenue with calendar-day-paced quarterly targets. Q1 is complete: 90/90 eligible days. Q2 uses April 1 through May 15 inclusive: 45/91 days. Return region, quarter, actual_revenue, quarterly_target, eligible_days, quarter_days, paced_target and attainment_rate. A zero target yields NULL attainment. Return columns: region, quarter, actual_revenue, quarterly_target, eligible_days, quarter_days, paced_target, attainment_rate.
Inventory level events set the product's level until the next event. As of 2025-01-10 00:00:00, a stockout interval begins when level becomes zero and ends at the next event changing it above zero; open stockouts end at the cutoff. Sequence same-time events by event_id. Return product_id, stockout_start, stockout_end and stockout_hours. Return columns: product_id, stockout_start, stockout_end, stockout_hours.
Concept guide
SQL aggregation summarizes many rows with functions such as COUNT, SUM, AVG, MIN and MAX. Correct aggregation starts by defining the intended result grain.
COUNT(*) counts rows. COUNT(column) counts only rows where that column is not NULL.
The JOIN may multiply source rows before aggregation. Inspect the joined row count and aggregate each side at the correct grain when necessary.