98 questions in 8 topics
SQL Basics Interview Questions
Keys, constraints, NULL, filtering and the basic commands, with several predict-the-output questions.
- What is SQL, and how is it different from MySQL?
- What are DDL, DML, DCL and TCL commands in SQL?
- What is the difference between a primary key and a unique key?
- What is a foreign key, and what does ON DELETE CASCADE do?
SQL Joins Interview Questions
Join types, the row counts they produce, and the traps behind wrong totals.
- What are the different types of joins in SQL?
- How many rows does a
LEFT JOINof customers to orders return? - What happens to a
LEFT JOINwhen you filter the right table inWHERE? - What does
SUM(o.amount)give after joining orders to their line items?
SQL GROUP BY and Aggregate Function Interview Questions
Aggregates, GROUP BY and HAVING, conditional counts, string aggregation and subtotals.
- What are aggregate functions in SQL?
- How does
AVGtreat NULL values? - Why do you get "column must appear in the GROUP BY clause or be used in an aggregate function"?
- Which filter runs first,
WHEREorHAVING?
SQL Subqueries and CTEs Interview Questions
Correlated subqueries, IN vs EXISTS, the NOT IN null trap, CTEs, recursion and UNION.
- What is a subquery, and what is a correlated subquery?
- What is the difference between IN and EXISTS?
- Does
NOT INwork when the subquery returns a NULL? - What is a CTE, and how is it different from a subquery or a temporary table?
SQL Window Functions Interview Questions
ROW_NUMBER, RANK, DENSE_RANK, running totals, LAG and LEAD, and the frame rules that catch experienced developers.
- What is a window function, and how is it different from GROUP BY?
- How do
ROW_NUMBER,RANKandDENSE_RANKnumber tied rows? - How do you calculate a running total in SQL?
- What do LAG and LEAD do? Show the change from the previous day.
SQL Query Interview Questions
The classic queries asked in a shared editor; each one runs, so change the data and try your own.
- Write a query to find the second highest salary.
- How do you find the Nth highest salary?
- How do you find duplicate rows in a table?
- How do you delete duplicate rows but keep one copy of each?
Advanced SQL Interview Questions
Indexes, query plans, transactions, isolation, locking and schema design.
- What is an index, and how does it make queries faster?
- What is the difference between a clustered and a non-clustered index?
- How does a composite index work, and why does the column order matter?
- You added an index but the query still does a full table scan. Why?
SQL Interview Questions for Data Analysts
Reporting queries: dates, percentages, pivots, medians, retention and streaks.
- How do you group sales by month?
- How do you calculate each row's percentage of the total?
- How do you pivot rows into columns in SQL?
- How do you calculate the median in SQL?
Frequently Asked Questions
How do I prepare for a SQL interview?
Write queries, do not only read answers. Practice joins, GROUP BY with HAVING, subqueries and window functions until you need no reference, then the classic questions (second highest salary, duplicates, top N per group) on a small table of your own. In the interview, state your assumptions about ties, NULLs and empty groups.
What SQL questions are asked to freshers and to experienced candidates?
Freshers get definitions and short queries: keys, constraints, DELETE vs TRUNCATE, WHERE vs HAVING, joins, second highest salary. At 3 to 5 years expect window functions, CTEs, harder queries and indexes. At 5 to 10 years: query plans, isolation levels, deadlocks, schema design and fixing a slow production query.
Which database should I practice on for SQL interviews?
Any of them; most interviews accept standard SQL. If the job names a database, practice on it (MySQL and PostgreSQL are most common, SQL Server in many enterprise roles), and know the differences that come up: LIMIT vs TOP, date functions, string aggregation and upserts.
Which SQL topics matter most for a data analyst interview?
Joins, GROUP BY with conditional aggregation (SUM(CASE WHEN ...)), window functions (ranking, running totals, LAG), dates, and turning a business question into a query: retention, conversion, month-over-month growth. Indexes and transactions matter far less than for a developer role.
How long does it take to prepare SQL for interviews?
If you already know basic SELECT, two to three weeks of daily practice covers this page; from zero, plan four to six weeks. Window functions and the classic queries take longest, so start them once joins feel easy.
Do I need DSA for a SQL or data analyst role?
Less than for a software engineering role, but many companies add an easy coding round: arrays, strings, hash maps. Duplicates, the Kth largest value and running sums are the same problems in SQL and in code, so the practice problems below are a good warm-up.