SQL Interview Questions and Answers
The SQL questions Pakistani employers actually ask, with worked answers for JOINs, GROUP BY, window functions, and the live tasks that catch candidates out.
SQL interviews test whether you can get the right rows, not whether you can recite syntax. JOIN, GROUP BY with HAVING, and window functions carry most tasks, so practise them on a real database with messy data. Always check for duplicates and NULLs before trusting a result, and know an index speeds up reads but slows down writes.
- SQL interviews test whether you can get the right rows, not whether you can recite syntax.
- JOIN, GROUP BY with HAVING, and window functions carry most live tasks.
- Always check for duplicates and NULLs before trusting a result.
- An index speeds up reads and slows down writes, so know the trade-off.
- Practise on a real database with messy data, not on clean textbook tables.
1. What SQL Interviews Actually Test
A hiring manager at a Lahore software house opens a spreadsheet of 2,000 orders and asks the candidate a simple question: which customer spent the most last month? The candidate writes a query, runs it, and the answer is wrong. Not because the syntax failed, but because an order appeared twice after a join, and the total doubled. That mistake is the whole interview in miniature. SQL interviews in Pakistan do not test whether you know the word SELECT. They test whether the number you hand over is right. Employers hiring backend developers, data analysts, and support engineers all run the same kind of test: a small database, a question in plain English, and a result they can check. The candidate who wins is not the one who knows the most functions. It is the one who checks the data first. How many rows are in the table? Are there duplicates? Does the customer table have customers with no orders at all? Those three questions, asked before you write anything, prevent most wrong answers. Learn SQL by running queries against a real database with messy data, not by memorising a cheat sheet. Install PostgreSQL or use an online playground, load a sample orders database, and break your own queries on purpose.
What is the difference between INNER JOIN and LEFT JOIN?
Question: what is the difference between INNER JOIN and LEFT JOIN? Worked answer: an INNER JOIN returns only rows that match in both tables, so a customer with no orders disappears from the result. A LEFT JOIN keeps every row from the left table and fills the missing side with NULL, so that same customer appears with a NULL order. If the question asks for all customers and their spending, use a LEFT JOIN, then handle the NULL with COALESCE. Question: what does this query return? SELECT customer_id, COUNT(order_id) FROM orders GROUP BY customer_id. Worked answer: one row per customer, with the number of orders each one placed. Question: how do you find customers who placed more than three orders? Worked answer: add HAVING COUNT(order_id) greater than 3 after the GROUP BY, because WHERE filters rows before grouping and HAVING filters groups after grouping. Writing WHERE COUNT(order_id) greater than 3 is the classic error, and interviewers wait for it. Question: what is the difference between WHERE and HAVING? Worked answer: WHERE removes rows before they are grouped, HAVING removes groups after they are formed, so a condition on an aggregate belongs in HAVING and a condition on a plain column belongs in WHERE.
3. Window Functions and Ranking, With Worked Answers
Question: write a query that ranks each order within its customer, newest first. Worked answer: SELECT order_id, customer_id, order_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS order_rank FROM orders. ROW_NUMBER gives each order a number starting at 1 for each customer, and PARTITION BY restarts the count for every customer. If two orders share a date, ROW_NUMBER still gives them different numbers, while RANK would give them the same number and skip the next one. Question: how do you find the second highest salary in a table? Worked answer: use a window function, SELECT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS r FROM employees) ranked WHERE r = 2, or use a subquery with MAX and a condition that the salary is below the overall maximum. Say which one you prefer and why: the window version reads clearly and handles ties. Question: what does DISTINCT do? Worked answer: it removes duplicate rows from the result, so SELECT DISTINCT city FROM customers returns each city once. It does not fix a bad join, though. If your join is doubling rows, DISTINCT hides the problem instead of fixing it. Find the cause, usually a one-to-many join, and fix the join or aggregate first.
What is an index, and when does it hurt?
Question: what is an index, and when does it hurt? Worked answer: an index is a separate structure the database keeps so it can find rows fast, like the index at the back of a book. An index on customer_id makes lookups by customer fast. It hurts on writes, because every insert and update must also update the index, so indexing every column on a table that changes constantly slows the whole system down. Question: what is a primary key? Worked answer: a column or a set of columns that identifies each row uniquely, and it cannot be NULL. An order_id of 1042 identifies one order, and no second row can share it. Question: what is a foreign key? Worked answer: a column in one table that refers to the primary key of another table, like orders.customer_id referring to customers.customer_id. It stops you inserting an order for a customer who does not exist, and it stops you deleting a customer who still has orders, unless the rule says otherwise. Question: how do you find duplicate emails in a table? Worked answer: SELECT email, COUNT(email) AS total FROM users GROUP BY email HAVING COUNT(email) greater than 1. Then say what you do next: decide which row to keep, usually the oldest account, before deleting anything, and never delete from a live table without a backup.
5. A Live SQL Task, Step by Step
A bank in Karachi runs a live SQL test with a database of accounts, transactions, and branches. The task is: find the branch with the highest total deposits in March, and list its top five accounts. The candidates who fail usually fail in the same three places. First, they filter the month after grouping instead of before, so January and February leak into the total. Filter with WHERE on the date first, then group. Second, they join transactions to accounts and forget that one account can have hundreds of transactions, so a COUNT of accounts returns the transaction count instead. Count the distinct account, or aggregate at the account level first and then at the branch level. Third, they never check the result. A branch total of nine digits on a test database of a few thousand rows should make you stop and look. The candidates who pass do a small sanity check: how many rows went in, how many came out, and does the total look sane? They also write the query in steps, as a common table expression, so the interviewer can follow it: first the March transactions, then the totals per account, then the totals per branch. A readable query with a small check beats a clever one-liner nobody can follow.
6. A Two-Week Plan That Ends With Queries You Can Show
Fourteen days, one hour a day, and a database you can break without consequence. Days one to three, install PostgreSQL and load a sample database, then run fifty small queries a day: select with a filter, sort the result, and group it. Predict the row count before you run each one. Days four to six, practise joins. Take two tables, customers and orders, and answer ten questions with each join type: inner, left, right, and full. Write down which customers vanish with each one. Days seven to nine, practise aggregation. Answer questions like the top three customers by spending, the average order value per city, and the products nobody bought last month. Every question needs GROUP BY, and half of them need HAVING. Days ten to twelve, learn one window function a day: ROW_NUMBER, RANK, and a running total with SUM OVER. Use them to answer ranking questions, such as the newest order per customer and the second highest salary. Days thirteen and fourteen, do two timed tasks with messy data. Load a CSV with a duplicate row, a blank amount, and a date written in the wrong format, clean it in SQL, and then answer a business question from it. Save your five best queries in a folder with a short note on each one. That folder is what you show when an interviewer asks what you have done with SQL.
Frequently Asked Questions
Do developers really need SQL, or is it only for data analysts?
Yes. Employers in Lahore and Karachi hiring backend developers, analysts, and data engineers test SQL in the first technical round, often before any framework question. A developer who cannot join two tables will not get far.
What should I practise first if I have one week?
JOINs and GROUP BY with HAVING. Almost every live task combines the two: join the tables, group the rows, filter the groups. Practise that pattern until it is automatic.
What do I do when my query returns the wrong rows in a live test?
Say what you tried, name the error or the wrong result, and ask for a hint about the data, not the answer. Interviewers score how you debug, and a candidate who asks a good question scores higher than one who guesses in silence.
Do I need a SQL certification to get hired in Pakistan?
You do not need a certification. A GitHub folder with five queries, each with a short note on what it answers and a screenshot of the result, proves more than a badge.
Ready to Put These Strategies into Practice?
Explore thousands of software, remote, and IT vacancies actively hiring today.
Recommended Career Playbooks
How to Pass Modern Software Engineering Interviews: A Practical, Low-Stress Guide
What happens in live coding sessions, system design interviews and behavioral questions, and how to prepare for each one.
Read ArticleTypeScript Interview Questions and Answers for 2026
The exact TypeScript questions Pakistani developers face in 2026 interviews, with worked answers you can say out loud and a two-week plan to get ready.
Read ArticlePython Interview Questions and Answers
The Python questions that decide interviews in Pakistan, with worked answers for lists, decorators, generators, and the live coding tasks employers actually set.
Read ArticleSpoken and Written English for Job Interviews: A Practical Practice Plan
Improve your workplace English with a simple daily routine: speaking practice, email writing, interview answers, common errors and free ways to practise.
Read Article