SQL for analysts
SQL для аналитика
Analysts use SQL to answer questions about data stored in tables. A query can select relevant records, combine information from several tables, and calculate metrics such as the number of customers who bought something this month.
What analysts do with SQL
SQL for analysts isn't a separate language or dialect. It's the practice of turning a question into a query that can be run and checked. A product analyst might use it to investigate a drop in sign-ups, while a finance analyst might use it to reconcile two sales totals. SQL is also used to build the datasets that feed recurring reports and BI dashboards.
Start with what one row represents
Before writing a query, identify the grain of each input table: does one row represent an order, a line item, or a payment attempt? Then decide what a row in the result should represent. For a monthly report by country, for instance, each output row might represent one country in one month. Specify which records qualify and which date defines the month before choosing how to count them.
WHERE filters rows before aggregation. GROUP BY produces a summary per group, and HAVING filters those groups after aggregation. JOIN brings in fields from another table, but a key with multiple matches can produce multiple output rows. Window functions such as LAG() and ROW_NUMBER() let you compare or rank rows without collapsing them. A common table expression (WITH) gives an intermediate result a name so you can read a multi-step calculation in stages.
Example: Which customers bought more than once?
Assume orders contains one row per order with a non-null customer_id, and paid_at uses a consistent reporting time zone. This query returns one row per customer who paid for at least two orders in May:
SELECT customer_id, COUNT(*) AS paid_orders
FROM orders
WHERE paid_at >= '2025-05-01'
AND paid_at < '2025-06-01'
GROUP BY customer_id
HAVING COUNT(*) >= 2;
WHERE sets the reporting period; HAVING applies the minimum order count. Using the start of June as an exclusive upper bound includes payments throughout May 31 without guessing the last representable instant of that day. This measures repeat purchasing within May, not how many May buyers had ever bought before.
Common mistakes and interview questions
A frequent reporting bug is joining orders to line items and then summing an order-level amount. An order with three items would contribute its total three times. Check whether the join key is unique on either side. If you need item-level measures alongside order totals, summarize the items to one row per order before joining. Also check how missing values affect your metric: COUNT(*) counts rows, while COUNT(column) skips NULL values in that column.
Interview tasks often ask for the first event per user, the top products in each category, or the share of users who convert. Explain what a result row represents and what should happen to ties before coding. For conversion, identify the denominator explicitly: if you join eligible users to purchases with an inner join, you'll lose the users who never purchased and may inflate the rate.