SQL interview questions · Business analyst
15 SQL interview questions for business analysts
Business analyst SQL tests focus on the reports a stakeholder would actually ask for: totals by segment, share of revenue, period-over-period growth and clean pivots. The SQL is rarely exotic; getting the numbers exactly right is what's tested.
What business analyst SQL interviews test
- Filtering precisely with AND/OR, IN, BETWEEN and LIKE
- Totals and averages by segment with GROUP BY and HAVING
- Combining tables without losing or duplicating rows
- Pivoting categories into columns
- Percent of total and growth rates
Filtering and sorting
- Active Canadian customersReturn the name and email of every active customer in Canada, sorted alphabetically by name.Pattern: WHERE · ANDFree
- North American customers who stayedReturn name and country of every customer in Canada or the USA who has NOT churned (active_status is not 'churned'), sorted by name.Pattern: OR · NOT · parenthesesFree
- First-week shipmentsReturn id, customer_id, amount, status and order_date of every completed or pending order dated 2024-07-01 to 2024-07-07 inclusive, sorted by order_date.Pattern: IN · BETWEENFree
- Three biggest ordersReturn id, customer_id and amount of the 3 largest completed orders, largest first.Pattern: ORDER BY · LIMITFree
- Salary bandsReturn name, salary and pay_band for every employee, where pay_band is 'high' for a salary of at least 150000, 'mid' for at least 100000, and 'entry' otherwise. Sort by salary (highest first), then name.Pattern: CASE WHENFree
Totals by segment
- Grocery spending summaryUsing only transactions in the 'groceries' category, return one row with txn_count (how many there are), total_spent (sum of amount), avg_spent (average amount, rounded to 2 decimals), smallest (lowest amount) and largest (highest amount).Pattern: COUNT · SUM · AVG · MIN · MAXFree
- Orders by customer and statusReturn customer_id, status, order_count (number of orders) and total_amount (sum of amount) for every combination of customer and status that has orders. Sort by customer_id, then status A to Z.Pattern: GROUP BY two columnsFree
- Revenue by countryReturn country and total_revenue (sum of completed order amounts), highest total_revenue first.Pattern: JOIN + GROUP BYFree
- High-value repeat buyersUsing completed orders, return customer_id, order_count and total_spent for customers with at least 2 completed orders AND total_spent over 300. Sort by customer_id.Pattern: GROUP BY · HAVINGFree
Report-style queries
- Spend by category, one row per accountReturn account_id, groceries, travel and dining (total spend in each category) with one row per account, sorted by account_id.Pattern: Pivot with FILTER / CASEPro
- Share of total spendReturn category, total (sum of amount) and pct_of_total (that total as a percentage of all spend, rounded to 1 decimal), largest share first.Pattern: SUM() OVER () · percent of totalPro
- Customer share of country revenueUsing completed orders, return country, name, revenue (that customer's completed total) and pct_of_country (revenue as a percentage of their country's completed revenue, rounded to 1 decimal). Sort by country, then pct_of_country highest first.Pattern: SUM() OVER (PARTITION BY) · percent of groupPro
- Monthly spendReturn month (formatted YYYY-MM) and total_spend (sum of amount) for each month, oldest first.Pattern: to_char · GROUP BY monthPro
- Month-over-month growthReturn month (YYYY-MM), total_spend and pct_change (percent change from the previous month, rounded to 1 decimal; NULL for the first month), oldest first.Pattern: CTE + LAG · growth ratePro
- Year-over-year growth by quarterFor each region's 2024 quarters, return region, quarter, revenue, prev_year_revenue (same region and quarter in 2023, NULL if none) and yoy_pct (percent change from prev_year_revenue, rounded to 1 decimal, NULL if no prior year). Sort by region, quarter.Pattern: Self-join on period · YoY %Pro
Interview tip: Round only at the end, and say which rows you're excluding (refunds, test accounts) before you start. Most wrong answers in BA screens come from an unstated filter.