SELECT EmployeeName,
ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RankNo
FROM Employees;
21. Explain RANK() and DENSE_RANK()
RANK(): Ranks with gaps. Example: 1, 2, 2, 4
DENSE_RANK(): Ranks without gaps. Example: 1, 2, 2, 3
22. What are Indexes?
Indexes improve query speed.
Benefits:
✔ Faster searches,
✔ Faster filtering
Drawback:
❌ Extra storage
23. What Causes Slow SQL Queries?
Common reasons:
✔ Missing indexes
✔ Too many joins
✔ Large datasets
✔ SELECT _ usage
✔ Unoptimized subqueries
24. How Do You Optimize SQL Queries?
Best practices:
✔ Create indexes
✔ Avoid SELECT _
✔ Filter early
✔ Optimize joins
✔ Use execution plans
25. What are Views?
Virtual tables based on SQL queries.
CREATE VIEW EmployeeView AS
SELECT EmployeeID, EmployeeName
FROM Employees;
26. What are Stored Procedures?
Reusable SQL programs stored in database.
Benefits:
✔ Faster execution,
✔ Reusable code,
✔ Better security
27. What are Transactions?
A group of SQL operations treated as one unit.
Example: Bank transfer transaction.
Commands: BEGIN TRANSACTION; COMMIT; ROLLBACK;
28. Explain ACID Properties
Atomicity: All or nothing.
Consistency: Data remains valid.
Isolation: Transactions don't interfere.
Durability: Committed changes stay permanent.
29. Find Duplicate Records
SELECT Email, COUNT(*)
FROM Customers
GROUP BY Email
HAVING COUNT(*) > 1;
30. Find Second Highest Salary
SELECT MAX(Salary)
FROM Employees
WHERE Salary <
(
SELECT MAX(Salary)
FROM Employees
);
31. Calculate Running Totals
SELECT OrderDate, Sales,
SUM(Sales) OVER (ORDER BY OrderDate) AS RunningTotal
FROM Orders;
32. Find Top Selling Products
SELECT ProductName, SUM(Sales) AS TotalSales
FROM Orders
GROUP BY ProductName
ORDER BY TotalSales DESC;
33. Calculate Month-over-Month Growth
SELECT Month, Sales,
LAG(Sales) OVER(ORDER BY Month) AS PreviousMonth
FROM SalesData;
34. Difference Between UNION and UNION ALL?
UNION: Removes duplicates.
UNION ALL: Keeps duplicates. UNION ALL is faster.
35. What are NULL Values?
NULL means missing or unknown value.
SELECT * FROM Employees WHERE ManagerID IS NULL;
36. Difference Between CHAR and VARCHAR?
CHAR: Fixed length.
VARCHAR: Variable length.
VARCHAR saves storage.
37. What is a Primary Key?
A unique identifier for each record.
Properties:
✔ Unique,
✔ Not NULL
38. What is a Foreign Key?
Maintains relationships between tables. Ensures referential integrity.
39. Difference Between Clustered and Non-Clustered Indexes?
Clustered Index: Stores actual table data. Only one per table.
Non-Clustered Index: Separate structure pointing to data. Multiple allowed.
40. Explain Query Execution Plans
Execution plans show how SQL Server executes a query.
Used to identify:
✔ Full table scans,
✔ Expensive joins,
✔ Missing indexes,
✔ Performance bottlenecks
💡 Most Data Analyst SQL interviews focus heavily on:
• Joins
• Group By
• Window Functions
• CTEs
• Subqueries
• Ranking Functions
• Real-world SQL scenarios
Double Tap ❤️ For Part-2 🚀