SQL interview preparation should cover filtering, joins, grouping, keys, NULL values, and basic window functions. A strong answer explains the concept, demonstrates it with a query, and checks the result. Learn how to reason about data rather than memorising answers without understanding what the query returns.

The examples below use PostgreSQL-compatible syntax. Other databases may differ. Ask which database and version the interviewer expects before using dialect-specific features.

What is SQL?

SQL stands for Structured Query Language. It is used to work with relational databases, including retrieving and changing data and defining database structures.

A concise interview answer can connect the definition to a task: “I use SQL to filter records, combine related tables, and calculate summaries.” Then provide an example from a project you actually completed.

What is the difference between WHERE and HAVING?

WHERE filters input rows before grouping. HAVING filters the resulting groups. Suppose an orders table contains customer_id, amount, and status:

SELECT customer_id, SUM(amount) AS total
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
HAVING SUM(amount) > 10000;

This query first selects paid orders, then calculates each customer's total and keeps totals above 10,000. PostgreSQL's aggregate tutorial explains the distinction.

In an interview, say what one output row represents: one qualifying customer. That small explanation helps demonstrate that you understand the result.

How do INNER JOIN and LEFT JOIN differ?

INNER JOIN returns matching row combinations. LEFT JOIN also preserves rows from the left table when no match exists on the right, filling the right-side columns with NULL.

For example, join customers to orders with LEFT JOIN when you need to retain customers who have not ordered. Check the relationship carefully: one customer with three orders can create three result rows.

The PostgreSQL join tutorial demonstrates these behaviors. Explain how you would avoid counting joined order rows as unique customers.

To build broader data-analysis skills around SQL, explore Itvedant's Data Science and Analytics course.

What is the difference between a primary key and a foreign key?

A primary key uniquely identifies a row and cannot contain NULL values. A foreign key enforces a relationship to a referenced key, subject to its definition.

For example, customers. customer_id could be a primary key for orders. customer_id references it. This helps prevent orders from pointing to a customer identifier that does not exist when the constraint applies.

Read the official constraint documentation for details, including composite keys and NULL behavior.

How should you handle NULL values?

NULL represents missing or unknown information. Use IS NULL or IS NOT NULL to test for it instead of comparing it with an equals sign.

Also distinguish COUNT(*) from COUNT(column). COUNT(*) counts rows, while COUNT(column) counts non-NULL values in that expression. PostgreSQL documents these aggregate behaviors.

If three rows contain email values for only two people, the two counts will differ. State which count the business question requires.

How can you find the second-highest distinct salary?

First clarify how ties should work. If two employees share the highest salary, “second employee” and “second-highest distinct salary” mean different things.

WITH ranked AS (
SELECT salary,
DENSE_RANK() OVER (
ORDER BY salary DESC
) AS salary_rank
FROM employees
WHERE salary IS NOT NULL
)
SELECT DISTINCT salary
FROM ranked
WHERE salary_rank = 2;

With salaries of 50,000, 50,000, and 40,000, this returns 40,000. With fewer than two distinct non-NULL salaries, it returns no row. PostgreSQL's window-function tutorial provides the underlying concepts.

How should you answer a query-writing problem?

Clarify the required output before writing code. Ask whether duplicates matter, which dates are included, and how missing values should be handled. Then describe the table relationships and your planned steps.

After writing the query, test it with a tiny dataset. Include an unmatched row, a duplicate, and a NULL where relevant. Explain any assumptions rather than hiding them in the SQL.

Avoid claiming that a query is efficient just because it is short. For performance questions, discuss the data size, indexes, and execution plan, then measure the actual behavior.

Interested in using SQL within applications? Review Itvedant's Full Stack Development curriculum and see how database learning connects with development.

Frequently asked questions

Is SQL enough to get a data analyst job?

SQL is useful, but vacancies may also require spreadsheets, visualization, statistics, and communication. Read actual job descriptions and build projects that demonstrate the required combination.

Should I memorize interview queries?

Practise patterns, but understand each step. An interviewer can change the table structure or business rule. Reasoning helps you adapt when a memorized answer no longer fits.

Which SQL database should I learn first?

Choose one supported database and practice consistently. Learn its syntax well, then compare differences when applying for roles that use another system.

What should I do if my first query is wrong?

Explain the issue, inspect a small example, and revise the query. Clear debugging and honest reasoning can show useful problem-solving skills.

Let's talk about your career growth!

+91

Please provide valid mobile number

Please provide valid name

Please provide valid email ID

Please select training mode

Thank you for contacting us !

Our Team will get in touch with you soon or call +919205004404 now to get answer for all your queries !

Scroll to Top