SQL interview guide: why it matters, core concepts, practice plan
Why interviewers ask about it
SQL appears in 17 of today’s 90 new software roles. Companies ask because data is the backbone of most products. A candidate who can retrieve, aggregate, and transform data reliably demonstrates three things:
- Problem‑solving with real data – they can turn a business question into a query.
- Understanding of relational design – they know how tables relate, which affects performance and correctness.
- Ability to work with existing codebases – most legacy systems expose data through SQL, so a developer must read and modify queries daily.
Interviewers use SQL to gauge both theoretical knowledge (normal forms, joins) and practical skill (writing clear, efficient queries). Even for roles that focus on front‑end or systems programming, data‑driven decisions are common, so a baseline SQL competence is expected.
The core ideas
1. Relational fundamentals
- Tables, rows, columns – the basic storage unit. Each column has a type; each row is a record.
- Primary keys uniquely identify rows; foreign keys enforce relationships.
- Normalization reduces redundancy. First normal form (1NF) eliminates repeating groups; higher forms address functional dependencies.
2. Query building blocks
- SELECT – choose columns.
- FROM – specify source tables.
- WHERE – filter rows.
- GROUP BY – aggregate rows sharing a key.
- HAVING – filter aggregated results.
- ORDER BY – sort output.
3. Joins
- INNER JOIN returns rows with matching keys in both tables.
- LEFT (OUTER) JOIN keeps all rows from the left table, filling NULLs when no match.
- RIGHT JOIN and FULL OUTER JOIN are less common but useful for completeness.
Understanding join order and join conditions is crucial for both correctness and performance.
4. Set operations
- UNION / UNION ALL combine result sets.
- INTERSECT and EXCEPT (or MINUS) find common or distinct rows.
5. Window functions
- OVER (PARTITION BY … ORDER BY …) lets you compute running totals, ranks, or moving averages without collapsing rows.
6. Indexes and execution plans
- Indexes speed lookups on columns used in WHERE, JOIN, ORDER BY.
- Over‑indexing hurts write performance; under‑indexing leads to full table scans.
- Use
EXPLAIN(or equivalent) to see how the engine will execute a query.
7. Common pitfalls
- Implicit type conversion can cause unexpected NULLs.
- Forgetting to alias columns when two tables have the same name.
- Using
SELECT *in production code – it hides column changes and can waste I/O. - Not handling NULL correctly in predicates (
=vsIS NULL).
A worked example
Task: A company stores user activity in two tables.
-- users table
CREATE TABLE users (
user_id INT PRIMARY KEY,
email VARCHAR(255) NOT NULL,
signup_dt DATE NOT NULL
);
-- events table
CREATE TABLE events (
event_id INT PRIMARY KEY,
user_id INT REFERENCES users(user_id),
event_type VARCHAR(50),
event_ts TIMESTAMP NOT NULL
);
Requirement: For each user who signed up in the last 30 days, list the total number of login events and the date of their most recent purchase event. Show users with zero logins as 0 and users with no purchases as NULL. Solution:
WITH recent_users AS (
SELECT user_id, email
FROM users
WHERE signup_dt >= CURRENT_DATE - INTERVAL '30 day'
),
login_counts AS (
SELECT user_id, COUNT(*) AS login_cnt
FROM events
WHERE event_type = 'login'
GROUP BY user_id
),
last_purchase AS (
SELECT user_id, MAX(event_ts) AS last_purchase_dt
FROM events
WHERE event_type = 'purchase'
GROUP BY user_id
)
SELECT ru.user_id,
ru.email,
COALESCE(lc.login_cnt, 0) AS login_cnt,
lp.last_purchase_dt
FROM recent_users ru
LEFT JOIN login_counts lc ON ru.user_id = lc.user_id
LEFT JOIN last_purchase lp ON ru.user_id = lp.user_id
ORDER BY ru.user_id;
Explanation:
recent_usersisolates the target cohort.login_countsaggregates login events per user.last_purchasefinds the most recent purchase timestamp.- Two
LEFT JOINs keep every recent user, inserting0for missing login rows viaCOALESCEand leavingNULLfor missing purchases. - The final
ORDER BYgives deterministic output.
5 questions you will be asked
- Explain the difference between
INNER JOINandLEFT JOIN.
- Inner returns only rows with matches in both tables.
- Left keeps all rows from the left side, filling NULLs when no match.
- Use left when you need to preserve rows that may lack related data.
- How would you find duplicate rows based on a column
emailin auserstable?
- Group by
emailand filter groups havingCOUNT(*) > 1. - Example:
SELECT email, COUNT() FROM users GROUP BY email HAVING COUNT() > 1;
- What is a window function and when is it preferable to
GROUP BY?
- Computes values across a set of rows related to the current row without collapsing rows.
- Preferable for running totals, rankings, or moving averages where you still need the original row context.
- Describe how an index on
events(user_id, event_ts)would affect the query in the worked example.
- The composite index supports the
WHERE user_id = …lookup and ordering byevent_tsfor theMAX(event_ts)aggregation, reducing scan time. - It also helps the join condition on
user_id.
- **Why is
SELECTdiscouraged in production code?*
- It hides column changes, can return unnecessary data, and may cause performance penalties.
- Explicit column lists make intent clear and protect against schema drift.
Common mistakes
- Missing join condition – leads to Cartesian product and huge result sets.
- Using
WHEREto filter after aLEFT JOIN– inadvertently turns it into an inner join; move the predicate to theONclause instead. - Aggregating before filtering – e.g., applying
HAVINGwithout properGROUP BYcan give wrong totals. - Neglecting NULL handling –
COUNT(column)ignores NULLs;COUNT(*)counts rows regardless. - Over‑using subqueries – sometimes a simple join or window function is more efficient.
- Assuming indexes are always used – the optimizer may skip an index if the predicate is not selective or if functions are applied to indexed columns.
A 3‑day practice plan
Day 1 – Foundations
- Review relational theory: primary/foreign keys, normal forms.
- Write basic SELECT statements with
WHERE,ORDER BY, and simple aggregates. - Solve 5 beginner‑level problems (e.g., filter users by signup date, count rows per category).
- End with the worked example: implement it, then modify it to add a
COUNT(DISTINCT event_type)column.
Day 2 – Joins and Window Functions
- Practice inner, left, and full outer joins using two‑table schemas.
- Convert a problem that uses a subquery into a join and vice‑versa.
- Learn one window function (
ROW_NUMBER) and apply it to rank users by activity. - Complete 5 intermediate questions that combine joins with aggregates.
Day 3 – Performance and Edge Cases
- Study index basics; create an index on a column and compare
EXPLAINplans before and after. - Write queries that deliberately produce NULLs and practice
COALESCEandIS NULLchecks. - Attempt 5 advanced questions: duplicate detection, hierarchical data with recursive CTEs, and optimizing a slow query.
- Review all mistakes listed above; for each, write a short “bad query → corrected query” pair.
By the end of the three days you will have reinforced the core ideas, seen how they appear in realistic tasks, and built a checklist of pitfalls to avoid during an interview.
Roles hiring for SQL now
Each has its own mock interview, built from the real job description.
- Software Engineer at Vertex Inc., Hyderabad: practise and apply
- Java Lead II - Software Engineering at UST, Chennai: practise and apply
- Specialist Solutions Architect - Data Engineering (Digital Native Business) at Databricks, United States · Remote: practise and apply
- Senior Staff Software Engineer at Fivetran, Oakland: practise and apply
- Software Engineer - Customer Experience at Aurora, Dallas: practise and apply
- Full-Stack Software Engineer, Child Safety Tools & Systems at OpenAI, San Francisco: practise and apply
- Full-Stack Software Engineer, Child Safety Tools & Systems (London) at OpenAI, London: practise and apply
- Security Engineer, Detection and Response at Notion, Tokyo: practise and apply
- 2027 - Internship, Software Engineering and Quantitative Development at Qube Research & Technologies, London Area: practise and apply
- Data Engineer - Corporate Data, Analytics & AI at Lyft, Toronto: practise and apply
- Software Engineer, Observability - Logging (Japan, Singapore) at Supabase, Remote: practise and apply
- Senior Software Engineer - Corporate Systems at Macy's, Johns Creek: practise and apply
- Software Engineer - Intern at Q2, Bengaluru: practise and apply
- Java Lead I - Software Engineering at UST, Chennai: practise and apply
- Full Stack Engineer at ThinkNovaa, India: practise and apply
- Principal Software Engineer at Fivetran, Remote: practise and apply
- Manager, Data Science - Marketplace Intelligence & Forecasting (Inference) at Airbnb, Remote - USA: practise and apply
More tech roles today
- Staff Software Engineer, Backend at Twilio, Remote - US: practise and apply
- Full-Stack & AI Agents Intern at Greedy Adventures, Pune City: practise and apply
- Software Development Engineer II, Amazon Teams at Amazon, Herndon: practise and apply
- Full Stack Development Internship in Gurgaon at GrowthGear, Gurgaon: practise and apply
- Software Engineer II, Warehouse Management Systems at Flexport, Bellevue: practise and apply
- Node.js Development Internship in Jaipur at AND WE SUPPORT INDIA, Jaipur: practise and apply
- Software Development Engineer Intern at LH2 AI Labs, Bengaluru: practise and apply
- Offensive Hardware Security Engineer, Platform Security at Anthropic, New York City: practise and apply
- Communications Manager, Starlink Mobile at SpaceX, Hawthorne: practise and apply
- Software Engineer (HPC High Speed Fabric Options Qualification) at Hewlett Packard Enterprise, Spring: practise and apply
- Communications Manager, Starlink Mobile at SpaceX, Redmond: practise and apply
- AI Inference Engineer at Baseten, London: practise and apply
- Data Scientist, Experimentation Partnerships at OpenAI, San Francisco: practise and apply
- Research Intern - Self-Improving Agentic Systems and AI Research Agents at Meta, London: practise and apply
- Senior Software Engineer at impact.com, Columbus: practise and apply
- Software Engineer, Safety Data Flywheel at OpenAI, San Francisco: practise and apply
- Engineering Manager, AI for Law at OpenAI, San Francisco: practise and apply
- Software Engineer - Java Developer at Fidelity Investments, Bengaluru East: practise and apply
- C++ Sr. Software Engineer at Comcast, New York: practise and apply
- Staff Software Engineer, Full Stack (App Experience) at Affirm, Cleveland: practise and apply
- Staff Software Engineer, Full Stack (App Experience) at Affirm, Salt Lake City: practise and apply
- Site Reliability Engineer, Infrastructure Platforms (Intermediate to Staff) at GitLab, Remote: practise and apply
- Information Security Engineer at Zscaler, Remote - Costa Rica: practise and apply
- Staff Product Manager, Conversational AI at Duolingo, New York: practise and apply
- Senior Software Engineer - Notifications Experience Team at PagerDuty, Toronto · Hybrid: practise and apply
- and 48 more on giveinterview.com/jobs
Practise it out loud
Knowing the answer is not the same as explaining it under follow-up questions. Try a free mock interview on giveinterview.com.