An analyst should be able to use SQL to answer six kinds of everyday business question: how many or how much, broken down by what, for which records, top or bottom, across which tables, and who didn’t. The syntax for all six can be learned fairly quickly. The harder skill — and the one that decides whether the answer is useful — is turning a vague request into a question precise enough to query, and then checking the result before anyone acts on it.
This guide walks through that translation with one running example: an online grocery business, the same one we used in How to Design KPIs That Actually Support Business Decisions. Last time we looked at which numbers it should track. This time we look at how to get those numbers out of its database.
Why is writing the SQL rarely the hard part?
Data requests usually arrive as a sentence: “Can you pull the numbers for Kolkata?” or “How are we doing in Kolkata?” The person asking knows what they mean. The analyst often doesn’t — not yet.
“Doing well” could mean revenue or number of orders. It could include cancelled orders or exclude them. It could mean last month, this quarter, or since a campaign started. And “well” only means something next to a comparison: last month, a target, other cities.
A query cannot be vague. Every condition in a WHERE clause and every column in a GROUP BY is a decision. If nobody makes those decisions explicitly, the analyst makes them silently while writing the query — and the result answers a question nobody actually asked.
The example: three tables from an online grocery store
Every example in this guide uses three illustrative tables. They are simplified, but the structure is typical of real order data.
| Table | Columns | One row means… |
| customers | customer_id, name, city | one customer |
| orders | order_id, customer_id, order_date, order_value, status, promo_code | one order |
| order_items | order_id, product, qty | one product line within an order |
The tables and queries are illustrative and do not describe a real business. Queries are written in MySQL-style SQL; differences in other databases are noted where they matter.
Step 1: How do you pin a business question down?
Before opening the database, settle five things. Together they turn a request into a question a query can answer.
| Decide | Question to ask | For “How are we doing in Kolkata?” |
| Measure | What exactly is being counted or added up? | Order revenue (order_value) |
| Records | Which rows count, and which don’t? | Exclude cancelled orders |
| Split | Broken down by what? | By city, so Kolkata can be compared |
| Window | Over which exact period? | 1–31 October 2026 |
| Comparison | Compared with what? | Other cities in the same month |
The precise version becomes: “Which cities brought in the most revenue in October 2026, not counting cancelled orders?”
This is the same discipline that makes a KPI usable. A KPI with a written definition — formula, time window, exclusions, source — can be turned into a query directly. One without a definition turns into a debate about whose number is right.
A useful test: if two people would write different queries for the same request, the question isn’t finished yet. Send the precise question back to the person who asked before you send back a number. It takes a minute and saves a round of rework.
Step 2: How does each part of the question become part of the query?
Read the precise question slowly. Every phrase that narrows, splits, ranks or limits the answer has a place in the query.
Each phrase of the precise question maps to one part of the query.
Put together, the query reads almost like the question:
In plain language: take the orders, attach each one to its customer’s city, keep only October orders that weren’t cancelled, add up order value for each city, sort from highest to lowest, and show the top five.
Two details are worth noticing. The date range uses >= the first day and < the first day of the next month. That “half-open” window includes every order on 31 October, even if order_date carries a time. And LIMIT is MySQL and PostgreSQL syntax; SQL Server uses TOP, and Oracle and several others use FETCH FIRST 5 ROWS ONLY. The logic is the same.
Six business question patterns every analyst should recognise
Most everyday business questions fit one of six patterns. Spotting the pattern in the question is half of writing the query.
| # | The business asks… | SQL pattern |
| 1 | How many? How much? | COUNT, SUM, AVG |
| 2 | Broken down by…? | GROUP BY |
| 3 | Only these records? | WHERE (rows) · HAVING (groups) |
| 4 | Top or bottom? | ORDER BY + LIMIT |
| 5 | Across tables? | JOIN |
| 6 | Who didn’t? | LEFT JOIN … IS NULL or NOT EXISTS |
1. How many? How much?
Aggregate functions answer the most common questions in business: how many orders, how much revenue, what the average order value was. The detail that matters is what you are counting. COUNT(*) counts rows — here, orders. COUNT(DISTINCT customer_id) counts customers. “How many customers ordered in October?” and “How many orders did we get in October?” are different questions with different answers.
2. Broken down by what?
Whenever a question contains “for each”, “by” or “per”, it needs GROUP BY. A simple rule keeps grouped queries honest: every column in SELECT is either grouped or aggregated. If a column is neither, the question is still unclear — the database (or the analyst) is being asked to pick one value from many without being told how.
3. Only these records?
There are two kinds of filter, and they happen at different moments. WHERE filters individual rows before grouping: only delivered orders, only October. HAVING filters groups after aggregation: only cities with at least 50 orders. Choosing the wrong one either fails or quietly answers a different question.
4. Top or bottom?
“Top five products”, “slowest-moving items”, “highest-spending customers” — sort with ORDER BY (add DESC for highest first) and cut the list with LIMIT. Decide how to handle ties before you share a “top five”: if the fifth and sixth products are level, a plain LIMIT 5 drops one of them without saying so.
5. Across tables?
Business questions often span tables: which city a customer is in, which products were in an order. JOIN connects them through a shared key, such as customer_id or order_id.
Joins are also where the most convincing wrong numbers come from. Before every join, ask: what will one row mean after this join? Join orders to order_items and each order now appears once for every product in it. That is exactly right if you want item-level detail — and exactly wrong if you then add up order_value. More on this in Step 3.
6. Who didn’t?
This is the pattern most often missed, because absence doesn’t show up in an ordinary join. Customers who haven’t ordered, products listed but never sold, branches that stopped reporting — none of them appear in a list built from the rows that exist. These questions are often among the most useful a business can ask.
The approach is to start from the full list — every customer — and keep only those with no matching row:
Notice where the date condition sits: in the ON clause, not in WHERE. A LEFT JOIN keeps every customer and fills in blanks where there is no matching order. If the date condition is moved to WHERE, it removes those blank rows — and the query returns no one. Many analysts find NOT EXISTS easier to read and it gives the same result:
Step 3: What should you check before sharing the answer?
SQL has one dangerous habit: a query that runs returns its answer with complete confidence, even when it answers a slightly different question. These checks take minutes.
- What does one row mean after each join? If a join multiplies rows, sums and counts inflate. The fix is to add up values at the right level — for example, sum order_value from the orders table alone, or aggregate order_items to one row per order before joining.
- Orders or customers? Check that COUNT(*) vs COUNT(DISTINCT …) matches the question.
- Where did the NULLs go? A filter such as status <> ‘cancelled’ also leaves out rows where status is empty, because an unknown value is neither equal nor unequal to anything. Decide whether those rows belong, and handle them explicitly if they do (OR status IS NULL).
- Is the time window exact? Use half-open ranges and check whether “last month” should include today’s incomplete data.
- Does it reconcile? Compare one total with a figure the business already trusts — a finance report, a known monthly count — before anyone acts on the result.
Many of these problems start in the data rather than the query: duplicate records, inconsistent city names, missing values. Our guide Data Cleaning: What to Fix Before You Trust Your Analysis covers what to check there.
When is SQL not the right tool?
SQL is the right tool when the data sits in a database and the question will be asked again. For a one-off look at a small file someone has emailed, a spreadsheet is often quicker. And if the data doesn’t record the thing being asked about — why customers left, say — no query can answer it; that needs a survey, an experiment or a conversation. Part of the skill is recognising which kind of question you have.
A six-step checklist: from question to query
- Restate the question precisely — measure, records, split, window, comparison. Confirm it with the person who asked.
- Find the tables and the grain — which tables hold the data, and what one row means in each.
- Filter — which records count (WHERE), and which groups count (HAVING).
- Group — what the answer is broken down by (GROUP BY).
- Aggregate and rank — count, sum or average; sort and limit if the question asks for top or bottom.
- Check before you share — row multiplication, distinct counts, NULLs, date windows, and one reconciled total.
Frequently asked questions
1. What SQL should a beginner analyst learn first?
The six patterns above cover a large share of everyday business questions: SELECT, WHERE, GROUP BY with COUNT/SUM/AVG, HAVING, ORDER BY with LIMIT, and JOIN including LEFT JOIN. Practise them on realistic business tables, starting each time from a written question rather than from the syntax.
2. What is the difference between WHERE and HAVING?
WHERE filters individual rows before they are grouped. HAVING filters groups after aggregation. “Only delivered orders” is a WHERE condition; “only cities with at least 50 orders” is a HAVING condition.
3. Does it matter which database I learn SQL on?
Less than people expect. The core of SQL — selecting, filtering, grouping, joining — works the same way in MySQL, PostgreSQL, SQL Server and others. Differences such as LIMIT versus TOP are small and quick to look up.
Where to go next
If you are building the skills for an analyst role, From B.Com to Data Analyst: A Realistic Roadmap for Commerce Graduates shows where SQL sits alongside Excel and Power BI, and How to Become a Data Analyst in the AI Era looks at the wider skill set. For the employer’s view, see What Indian Companies Actually Look For When Hiring Junior Data Scientists.
And if you haven’t read it yet, How to Design KPIs That Actually Support Business Decisions explains how to decide which numbers are worth querying in the first place.







