CCareerClutch
← All posts
·5 min read·CareerClutch Team

SQL Interview Questions for Freshers in India (With Sample Answers)

interview-preptechnicalsql

SQL is one of the most tested skills in Indian campus placements and entry-level job interviews — whether you're applying for a software engineer, data analyst, or business analyst role. Service companies like TCS, Infosys, Wipro, and Cognizant ask SQL in their written tests; product companies ask it in dedicated technical rounds. The good news: the set of questions is fairly predictable.

This guide covers the questions that actually appear, what interviewers want to hear, and a few traps to avoid.


Why SQL comes up even for non-data roles

Most applications read from and write to a database. Even if your day-to-day work is in Java or Python, interviewers want to know you can reason about data — joins, aggregations, filtering. For analyst roles it's the primary technical test. For developer roles it's often the warm-up before the harder coding questions.


The core concepts tested

Before diving into specific questions, know what categories interviewers work from:

  • Basic queries — SELECT, WHERE, ORDER BY, LIMIT
  • Aggregations — GROUP BY, HAVING, COUNT, SUM, AVG, MAX, MIN
  • Joins — INNER, LEFT, RIGHT, FULL OUTER, SELF, CROSS
  • Subqueries and CTEs
  • Window functions — ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD
  • NULL handling — IS NULL, COALESCE, NULLIF
  • Constraints and keys — PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL

Freshers are rarely tested on window functions in service company rounds, but they come up at product companies and data roles.


Common SQL interview questions with sample answers

1. What is the difference between WHERE and HAVING?

WHERE filters rows before aggregation. HAVING filters groups after aggregation.

-- Wrong: this errors because SUM doesn't exist at WHERE time
SELECT department, SUM(salary) FROM employees WHERE SUM(salary) > 50000;

-- Correct
SELECT department, SUM(salary) FROM employees GROUP BY department HAVING SUM(salary) > 50000;

Interviewers love this because many candidates confuse the two.


2. Explain the different types of JOINs with an example.

Most common trap: candidates describe INNER and LEFT correctly but fumble RIGHT JOIN and forget FULL OUTER entirely.

| Join | Returns | |------|---------| | INNER | Rows with matching keys in both tables | | LEFT | All rows from left + matched rows from right (NULL for no match) | | RIGHT | All rows from right + matched rows from left | | FULL OUTER | All rows from both, NULLs where no match |

In interviews, always have a concrete example ready. Use two simple tables: employees and departments.

-- All employees with their department name (unassigned employees included)
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.dept_id;

3. Find the second highest salary in a table.

This is the single most asked SQL question in Indian freshers' interviews.

Method 1 — subquery (works everywhere):

SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

Method 2 — LIMIT/OFFSET (MySQL, PostgreSQL):

SELECT DISTINCT salary FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;

Method 3 — window function (for nth salary at product companies):

SELECT salary FROM (
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
  FROM employees
) ranked
WHERE rnk = 2;

Know all three. Service companies expect method 1 or 2; product companies may ask you to generalise to "Nth highest."


4. What is the difference between DELETE, TRUNCATE, and DROP?

| Command | What it does | Rollback? | Removes structure? | |---------|-------------|-----------|-------------------| | DELETE | Removes specific rows (with WHERE) or all rows | Yes | No | | TRUNCATE | Removes all rows, faster | No (usually) | No | | DROP | Removes the entire table | No | Yes |

A common follow-up: "Why is TRUNCATE faster than DELETE?" Answer: TRUNCATE doesn't log individual row deletions; it deallocates data pages.


5. What are indexes and when should you use them?

An index is a data structure (usually a B-tree) that makes lookups faster at the cost of additional storage and slower writes.

Use an index when:

  • A column appears frequently in WHERE, JOIN, or ORDER BY clauses
  • The table has a large number of rows and the query is selective

Don't index every column — each index slows down INSERT, UPDATE, and DELETE because the index must also be updated.

Interviewers at product companies often ask: "You notice a query is slow. What do you check first?" The expected answer includes: check if the relevant columns are indexed, run EXPLAIN/EXPLAIN ANALYZE, and look at the query plan.


6. What is a self join? Give a real example.

A self join joins a table to itself. Classic use case: finding an employee's manager when both are in the same table.

SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;

Freshers often haven't seen this pattern. If you have, you'll stand out.


What interviewers at Indian companies actually want

Service companies (TCS, Infosys, Wipro): Focus on writing correct basic queries. They care that your syntax is right and you understand joins and aggregations. Speed matters in written tests — practice on paper.

IT consultancies (Accenture, Capgemini, Cognizant): Similar to above, with occasional questions on normalization (1NF, 2NF, 3NF) and constraints.

Product and startup roles: Expect window functions, query optimisation, and the ability to explain your approach step-by-step. They may give you a schema and ask you to write a complex query from scratch on a whiteboard or shared editor.


Quick prep checklist

  • [ ] Write the second-highest salary query from memory
  • [ ] Explain all four join types with a concrete example
  • [ ] Know the difference between WHERE and HAVING
  • [ ] Know DELETE vs TRUNCATE vs DROP
  • [ ] Practise GROUP BY + HAVING on a real dataset (use any free SQL sandbox)
  • [ ] For data/analyst roles: learn ROW_NUMBER and RANK window functions

The fastest way to get confident is to actually write queries, not just read about them. Even 30 minutes of hands-on practice on /features style mock rounds — or a free SQL sandbox like DB Fiddle — will do more than re-reading notes.

SQL rewards practice over theory. Most freshers know the concepts; the ones who stand out are the ones who can write correct syntax under pressure without pausing to think about commas.