Skip to main content

What Cramming for a SQL Exam Taught Me About Running a Business on Data

Updated 8 min readMarkdown
What Cramming for a SQL Exam Taught Me About Running a Business on Data

Two days, one written exam, and seven lessons that any business owner can use, even if you never write a line of SQL.


The setup

I got invited to a written exam for a SQL Engineer role. I had two days to get ready.

I've spent years building with Next.js, Node and PostgreSQL. You'd think SQL would be easy for me. It wasn't. ORMs and query builders had quietly done the hard thinking for me. When I sat down to write queries by hand on paper, I found gaps I didn't know I had.

So I went back to basics: keys, normalisation, joins, GROUP BY, set operations and ACID. I used one running example the whole way through, a small online shop with customers, products and orders.

Halfway through, something clicked. These weren't just exam topics. Every concept answered a question a business owner asks every week.

Here's what I learned, and how a business can put each lesson to work.

Lesson 1: Start with the question, not the syntax

My biggest problem wasn't remembering syntax. It was starting at SELECT and getting lost halfway through.

The fix was a four-step recipe I now use every time:

  1. Which table has my data? That's your FROM. If the data is in two tables, join them on the ID they share.
  2. Which rows do I want? That's your WHERE.
  3. Does it say "each" alongside a total or a count? Then GROUP BY. A filter on that total goes in HAVING.
  4. What should I show? That's your SELECT. Write it last.

Before: "Hmm... SELECT customer... something... orders?"

After: "How many orders has each customer placed, for customers with more than 3?"

SELECT c.name, COUNT(o.order_id) AS order_count
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.name
HAVING COUNT(o.order_id) > 3;

For business: Every useful report starts with a clear business question, not a dashboard tool. "Who are my repeat buyers?" is a question you can answer. "Show me the data" isn't. Write the question in one sentence before anyone opens a spreadsheet.

Lesson 2: "Including" vs "never" is worth money

This was the most useful distinction I learned.

  • "Including customers with no orders" means a LEFT JOIN, which keeps everyone.
  • "Customers who never ordered" means a LEFT JOIN plus IS NULL, which keeps only the missing ones.
-- Customers who signed up but never bought
SELECT c.name, c.email
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;

For business: This one query is a ready-made win-back list. These people already trusted you with their email address. Send them a first-order discount and you're marketing to warm leads, not strangers.

The set-operation version works for mailing lists too:

-- Newsletter subscribers who never became customers
SELECT email FROM subscribers
EXCEPT
SELECT email FROM customers;

Lesson 3: Rows vs groups (WHERE vs HAVING)

I kept writing WHERE SUM(amount) > 100000, and it kept failing. That's because SQL runs in this order:

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY

WHERE runs before any totals exist. It filters rows. HAVING filters groups, after the totals have been calculated.

-- Cities where total sales are above Rs 1 lakh
SELECT c.city, SUM(o.amount) AS total_sales
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.city
HAVING SUM(o.amount) > 100000;

For business: This is the difference between "big orders" (rows) and "big markets" (groups). One query tells you which cities deserve your ad budget, where to open a pickup point, or which region needs a dedicated sales person.

Lesson 4: Duplicates can lie about your revenue

I had UNION and UNION ALL backwards at first. Here's the difference:

  • UNION combines the results and removes duplicates.
  • UNION ALL combines the results and keeps everything. It's also faster.

The trap: say you merge January and February sales logs with UNION. Two customers each buy the same Rs 500 item on the same day, so the two rows look identical. UNION deletes one of them, and your revenue report quietly comes out too low.

The rule: use UNION when duplicates mean the same thing, like an email list. Use UNION ALL when every row is a real event, like a sale, a log entry or a payment.

For business: If your numbers come from merged spreadsheets or exports, ask how they were merged. A "cleaned" report that removed duplicates may have removed real sales too.

Lesson 5: One source of truth (normalisation)

Imagine storing a customer's address on every order they place. When they move, you have to update 100 rows. Someone misses three, and now deliveries go to the wrong address.

Normalisation fixes this by giving every fact exactly one home. There's a line examiners love:

Every column must depend on the key, the whole key, and nothing but the key.

  • 1NF: one value per cell, so no "98xxx, 97xxx" crammed into a single phone field.
  • 2NF: product details live in a products table, not repeated on every order line.
  • 3NF: a city's pincode lives in a cities table, not on every customer.

For business: This isn't just about databases. If your customer list lives in a Google Sheet, a CRM and a WhatsApp group, you don't have three backups. You have three versions of the truth that disagree. Pick one system as the master record.

Lesson 6: Trust is built on ACID

When a customer pays, two things must happen: money leaves their wallet, and an order is created. ACID is what guarantees that goes right.

  • Atomicity: both steps happen or neither does. Nobody pays without getting an order.
  • Consistency: rules are never broken. Stock never goes negative.
  • Isolation: two people buying the last phone at the same moment can't both get it.
  • Durability: once an order is confirmed, it survives a server crash.

For business: Customers never see ACID, but they feel it when it's missing. A charge with no order, or an oversold item, is what turns into a one-star review. If you're choosing a payment or e-commerce platform, ask how it handles failed payments and overselling.

Lesson 7: Missing isn't zero

WHERE phone = NULL doesn't throw an error. It just returns zero rows, without saying anything. That's because NULL means unknown, and comparing anything to unknown gives unknown. The fix is IS NULL.

-- Total spend per customer, showing 0 instead of blank
SELECT c.name, COALESCE(SUM(o.amount), 0) AS total_spent
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.name;

For business: A blank field and a zero are different facts. "Didn't buy" isn't the same as "we don't know". Gaps in your data, like missing phone numbers or untagged sources, are a to-do list, not noise. Measure them.

5 questions every small business should ask its data

You don't need a data team to start. Here are five questions, with the SQL idea behind each:

Business question SQL idea What you do with it
Who signed up but never bought? LEFT JOIN + IS NULL Win-back offer
Who are my top 10% of customers? GROUP BY + SUM + ORDER BY VIP perks, referrals
Which cities or areas drive the most sales? GROUP BY + HAVING Ad budget, delivery zones
Which products sell above the category average? Correlated subquery Stock and promote winners
Which subscribers aren't customers yet? EXCEPT Targeted email campaign

Each of these is one query, or one pivot table if you're in a spreadsheet. The thinking is what matters, not the tool.

The real takeaway

I started out cramming syntax for an exam. I ended up learning a way of thinking:

  1. Ask a precise question.
  2. Find where the data lives.
  3. Filter the right thing: rows or groups.
  4. Respect what's missing.
  5. Keep one source of truth.

That applies whether you're writing SQL, building a Next.js dashboard, or running a business from a spreadsheet. The businesses that grow aren't always the ones with the most data. They're the ones that ask it the sharpest questions.


If you run a small business and want help turning your sales data into decisions like these, get in touch.

3 views

No comments yet. Be the first to comment.