Pakistan Careers · 250 companies
Back to journal

Interview Preparation

SQL Interviews: Explain the Grain Before Writing the Query

Practise joins, nulls, aggregation, and verification with a coherent interview dataset.

QIA editorial team · 27 October 2026 · 3 min read · 573 words

Practical learning workspace and tools for this guide

Clarify the requested result

Before typing SQL, restate the question in terms of rows and measures. If asked for monthly active customers, clarify what active means, which timezone determines the month, and whether cancelled activity counts. An interviewer may intentionally leave these details open to observe your reasoning. Ask focused questions rather than silently choosing a convenient interpretation.

Write the intended output grain: one row per month, perhaps with customer count and qualifying activity count. Then identify the source grain. Distinguishing those two levels prevents many errors before syntax becomes relevant. It also gives you a way to explain the query as a sequence of transformations.

Use a small schema with deliberate traps

Practise with customers, orders, order lines, and payments. Give one customer no orders, one order multiple lines, and one order two payment attempts. Add a cancelled order and a missing optional field. This tiny dataset exposes mistakes that a large random dataset can hide.

Calculate the expected answers manually for a few questions. Joining lines and payments directly can multiply rows, so compare counts before and after each join. Explain whether a relationship is one-to-one, one-to-many, or potentially many-to-many. A correct-looking total is not evidence that the join is safe.

Handle nulls and filters explicitly

SQL null represents missing or unknown information, not an ordinary value. Practise the difference between counting rows and counting a nullable expression. Test null comparisons and explain why a missing value may need separate treatment rather than automatic replacement with zero.

Compare a filter placed in a left join condition with the same filter placed in the final WHERE clause. Use a customer with no matching order to show the difference. PostgreSQL's official tutorial provides join examples, but interview platforms may use another dialect. State dialect-specific syntax and avoid presenting one implementation's behaviour as universal.

Aggregate in controlled steps

Use common table expressions or subqueries to make intermediate grains explicit. For revenue per customer, aggregate the correct financial events before joining to descriptive dimensions when that avoids multiplication. Explain whether refunds are negative transactions, separate events, or excluded under a stated business definition.

Practise conditional aggregation, date grouping, and ratios with zero-denominator handling. Do not rely on integer division accidentally producing the desired result. For a rate, explain both numerator and denominator in plain language and demonstrate one hand-calculated case. This is often more important than compressing the entire solution into fewer lines.

Learners reviewing a dashboard and project evidence together
Practice, review and explain your work. Editorial illustration; not a student testimonial.

Verify before discussing optimization

Run or mentally evaluate the query against your deliberate edge cases. Check duplicate keys, customers with no activity, tied dates, and periods without records. If the result is wrong, describe the debugging step rather than repeatedly rewriting the query without a hypothesis.

Only then discuss performance. PostgreSQL EXPLAIN documents how to inspect a plan; plan details and optimizer decisions depend on the database and data. Avoid claiming that a common table expression or an index is always faster. Explain the access pattern, selectivity, and evidence you would inspect before recommending a change.

Rehearse a complete answer under time pressure

Use a short practice routine: clarify the question, sketch the output, write the query, test edge cases, and summarize trade-offs. Speak enough to expose your reasoning without narrating every keystroke. If you cannot recall a dialect-specific function, describe the required operation and ask which engine is available.

After practice, keep an error log organized by concept rather than by question title. Record whether the failure involved grain, join multiplicity, nulls, dates, or syntax. Revisit the underlying concept with a new dataset. Memorizing a solution can help recognition, but reliable interview performance comes from rebuilding the reasoning when the question changes.

Sources & further reading

Last editorial review: 27 October 2026. Examples are educational, not live employer data or guaranteed outcomes.

Mentor support

Need Help Choosing the Right Career?

Live mentors are available all the time — talk to one right now.

  • Career Guidance
  • Skill Assessment
  • Roadmap Planning
  • Learning Advice
  • Enrollment Support
  • Salary Guidance
  • Gulf · PK · CA Job Market Advice
Chat with a Mentor on WhatsApp
+92 308 1311101 · qiatraining.support@gmail.com

1:1 counseling

Before you enroll

Job market data

Gulf · PK · CA

Mentor is calling…Talk now to a mentor