Real-time SQL practical tasks are frequently asked in interviews for SQL Developer, Data Engineer, MSBI Developer, and DBA roles.
This guide covers essential hands-on problems to test your querying, optimization, joins, window functions, indexing, transactions, and more.
1. Query Writing & Joins
1️⃣ Retrieve the second highest salary from the Employees table.
Table: Employees(EmpID, EmpName, Salary)
✔️ Answer
2️⃣ Get the nth highest salary (write a reusable query).
Input: n = 3
✔️ Answer
3️⃣ Display employees who have never placed an order.
Tables: Employees, Orders
✔️ Answer
4️⃣ Find the department with the highest total salary.
Tables: Employee(EmpID, DeptID, Salary)
✔️ Answer
2. Aggregations & Grouping
5️⃣ Count employees in each department and filter only departments with more than 5 employees.
✔️ Answer
6️⃣ Find the highest-paid employee in each department.
✔️ Answer
3. Window Functions
7️⃣ Calculate running total of sales by date.
Table: Sales(SaleDate, Amount)
✔️ Answer
8️⃣ Rank products based on revenue within each category.
✔️ Answer
4. Subqueries & CTEs
9️⃣ Get employees earning more than the average salary.
✔️ Answer
🔟 Recursively retrieve a hierarchy of employees (CTE).
Table: Employees(EmpID, EmpName, ManagerID)
✔️ Answer
5. String, Date & Data Cleaning
1️⃣1️⃣ Reverse a string without using REVERSE()
✔️ Answer
1️⃣2️⃣ Extract only numeric characters from a mixed string (A12B34).
✔️ Answer
1️⃣3️⃣ Find the age of an employee using DOB.
✔️ Answer
6. Advanced Joins & Set Operations
1️⃣4️⃣ Retrieve customers who bought 'Product A' but never bought 'Product B'.
✔️ Answer
1️⃣5️⃣ Find orders that appear in one table but not in another.
✔️ Answer
7. Indexing & Performance
1️⃣6️⃣ Create a non-clustered index to improve a search query.
✔️ Answer
1️⃣7️⃣ Find queries causing table scans (interviewer expects explanation).
✔️ How to answer (talk + demo query)
Use execution plan:
If index missing → table/clustered scan.
8. Stored Procedures, Functions & Triggers
1️⃣8️⃣ Write a stored procedure to fetch employees by department.
✔️ Answer
1️⃣9️⃣ Create a function to return yearly salary.
✔️ Answer
2️⃣0️⃣ Trigger to log updates in a table.
✔️ Answer
9. Transactions, Error Handling, ACID
2️⃣1️⃣ Write a transaction with TRY…CATCH.
✔️ Answer
10. Real-Time Data Engineering Tasks
2️⃣2️⃣ Pivot monthly sales into columns.
✔️ Answer
2️⃣3️⃣ Split comma-separated values into multiple rows.
✔️ Answer
2️⃣4️⃣ Merge updates from a staging table into main table.
✔️ Answer
0 comments:
Post a Comment