SQL Notes
Learn the most important intermediate-level SQL interview questions with detailed answers, examples, joins, subqueries, indexing, transactions, and optimization concepts.
Once you've crushed the basics, interviewers start asking about real problems:
Practical SQL
Query Logic
Joins
Subqueries
Indexes
Transactions
PerformanceThese are the questions you'll get for:
SQL Developer
Backend Developer
Database Developer
Software Engineer
Data Analystpositions.
HAVING
Filters groups after grouping is done.
Example:
SELECT DepartmentID,
COUNT(*)
FROM Employees
WHERE Salary > 50000
GROUP BY DepartmentID
HAVING COUNT(*) > 5;Q2. What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN
Only gives you matches from both tables.
LEFT JOIN
Everything from the left table, plus matches from the right. No match? You still get the left data.
Example:
SELECT *
FROM Employees E
LEFT JOIN Departments D
ON E.DepartmentID =
D.DepartmentID;Q3. What is a Self Join?
A table joining itself.
Example:
SELECT
E.EmployeeName,
M.EmployeeName AS Manager
FROM Employees E
LEFT JOIN Employees M
ON E.ManagerID =
M.EmployeeID;Q4. What is a Cross Join?
Gives you every possible combination — Cartesian product.
Example:
SELECT *
FROM Employees
CROSS JOIN Departments;Result:
Every Employee × Every Department
Q5. What is a Correlated Subquery?
A subquery that references the outer query. They talk to each other.
Example:
SELECT *
FROM Employees E
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
WHERE DepartmentID =
E.DepartmentID
);Q6. Difference Between IN and EXISTS?
IN
Checks if a value matches items in a list.
EXISTS
Checks if any rows exist from a query.
On big datasets:
EXISTSoften runs faster.
Q7. What is a CTE?
CTE stands for:
Common Table Expression
Think of it as a temporary named query you create for one query.
Example:
WITH HighSalary AS
(
SELECT *
FROM Employees
WHERE Salary > 50000
)
SELECT *
FROM HighSalary;Q8. What is a Recursive CTE?
A CTE that calls itself. Useful for:
Hierarchies Organization Charts Folder Structures
Q9. What is the difference between DELETE and TRUNCATE?
| Feature | DELETE | TRUNCATE |
|---|---|---|
| WHERE Clause | Yes | No |
| Row-by-Row | Yes | No |
| Speed | Slower | Faster |
| Logging | More | Less |
Q10. What is the difference between UNION and UNION ALL?
UNION
Stacks results and removes any duplicates.
UNION ALL
Stacks results and keeps everything, even duplicates.
Example:
SELECT Name FROM A
UNION ALL
SELECT Name FROM B;Q11. What is an Index?
An index speeds up data searches. Like a book's index — you don't read every page, you just look up what you need.
Example:
CREATE INDEX IX_Email
ON Employees(Email);Q12. What is the difference between Clustered and Non-Clustered Index?
| Feature | Clustered | Non-Clustered |
|---|---|---|
| Data Sorted | Yes | No |
| Count per Table | One | Multiple |
| Range Queries | Faster | Moderate |
Q13. What is a Composite Index?
An index built on multiple columns at once.
Example:
CREATE INDEX IX_DepSalary
ON Employees
(
DepartmentID,
Salary
);Q14. What is the Leftmost Prefix Rule?
For a composite index like:
(DepartmentID, Salary)SQL can efficiently use:
WHERE DepartmentID = 1but not always:
WHERE Salary > 50000alone — order matters.
Q15. What is a Covering Index?
An index that has all the columns your query needs. The database doesn't have to look elsewhere.
Example:
CREATE INDEX IX_Covering
ON Employees
(
DepartmentID,
EmployeeID,
EmployeeName
);Q16. What is Normalization?
The process of organizing data to cut down redundancy and improve consistency.
Forms:
1NF 2NF 3NF BCNF
Q17. What is Denormalization?
The opposite — combining data in ways that break normal forms to get faster reads. It's a tradeoff.
Q18. What is a View?
A virtual table built from a query. It looks like a table but it's just a saved query.
Example:
CREATE VIEW ActiveEmployees AS
SELECT *
FROM Employees
WHERE IsActive = 1;Q19. What is a Materialized View?
A view that actually stores the data physically, not just the query. Faster reads, but takes up space.
Pros:
Faster Reads
Cons:
Takes StorageQ20. What is a Stored Procedure?
SQL code saved inside the database that you can call over and over.
Example:
CREATE PROCEDURE GetEmployees
AS
SELECT *
FROM Employees;Q21. What is the difference between Procedure and Function?
| Feature | Procedure | Function |
|---|---|---|
| Returns Value | Optional | Required |
| In SELECT | No | Yes |
| Complex Logic | Yes | Limited |
Q22. What is a Trigger?
Code that fires automatically when something happens:
INSERT UPDATE DELETE
Q23. What are ACID Properties?
The four guarantees for transactions:
Atomicity
Consistency
Isolation
DurabilityQ24. What is Transaction Isolation Level?
Controls how much one transaction can see from another while both are running.
Levels:
Read Uncommitted
Read Committed
Repeatable Read
SerializableExam Focus
Revise definitions, diagrams, examples, and short-answer points for SQL Interview Questions (Intermediate).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL Complete Guide topic.
Search Terms
sql-complete-guide, sql complete guide, sql, complete, guide, interview, questions, intermediate
Related SQL Complete Guide Topics