SQL Questions
1. How would you optimize a slow-running query with multiple joins?
- Use proper indexing on join and filter columns.
- Avoid SELECT *; select only necessary columns.
- Rewrite queries using CTEs or subqueries wisely.
- Use EXPLAIN PLAN or QUERY PLAN to analyze bottlenecks.
- Avoid functions on indexed columns in WHERE clause.
- Break complex queries into smaller parts and cache intermediate results if possible.
- Ensure statistics are up-to-date on tables.
2. What is a recursive CTE, and can you provide an example of when to use it?
A Recursive CTE is used to perform hierarchical or recursive queries.
Example:
WITH EmployeeHierarchy AS (
SELECT EmployeeID, ManagerID, Name
FROM Employees
WHERE ManagerID IS NULL
UNION ALL
SELECT e.EmployeeID, e.ManagerID, e.Name
FROM Employees e
JOIN EmployeeHierarchy eh ON e.ManagerID = eh.EmployeeID
SELECT * FROM EmployeeHierarchy;
3. Explain the difference between clustered and non-clustered indexes and when to use each.
- Clustered Index: Determines the physical order of data in a table. Only one per table.
- Non-Clustered Index: A separate structure pointing to the data rows. Can have multiple.
Use clustered when:
- Columns are used in range queries or sorting.
Use non-clustered when:
- Searching with columns not in the primary key or requiring multiple indexes.
4. Write a query to find the second highest salary in each department.
SELECT Department, Salary
FROM (
SELECT Department, Salary,
DENSE_RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS rk
FROM Employees
) sub
WHERE rk = 2;
5. How would you detect and resolve deadlocks in SQL?
- Use SQL Server Profiler or Extended Events to capture deadlock graphs.
- Resolve by:
- Accessing tables in a consistent order.
- Keeping transactions short.
- Using lower isolation levels.
- Adding retry logic in applications.
6. Explain window functions and provide examples of ROW_NUMBER, RANK, and DENSE_RANK.
SELECT Name, Department,
ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) AS RowNum,
RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS RankNum,
DENSE_RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS DenseRank
FROM Employees;
7. Describe the ACID properties in database transactions and their significance.
- Atomicity: All operations complete or none do.
- Consistency: Database remains valid after a transaction.
- Isolation: Concurrent transactions do not interfere.
- Durability: Results persist after transaction completion.
8. Write a query to calculate a running total with partitions based on specific conditions.
SELECT Department, EmployeeID, Salary,
SUM(Salary) OVER (PARTITION BY Department ORDER BY EmployeeID) AS RunningTotal
FROM Employees;
Power BI Questions
1. Explain the concept of context transition in DAX and provide an example.
Context Transition occurs when a row context is converted to a filter context.
Example:
TotalSales := CALCULATE(SUM(Sales[Amount]))
2. How would you optimize a complex Power BI report for faster performance?
- Use import mode.
- Reduce model size by removing unused columns/tables.
- Use star schema.
- Avoid complex nested CALCULATEs.
- Use aggregation tables.
- Limit visuals per page.
3. Describe the process of creating and using calculation groups in Power BI.
- Created in Tabular Editor.
- Used to simplify repetitive DAX calculations.
- One table contains a column for the group and calculation items.
4. How would you handle large datasets in Power BI without compromising performance?
- Use aggregations and pre-aggregated tables.
- Implement incremental refresh.
- Partition data in Power Query.
- Optimize data types.
5. What is a composite model in Power BI, and how can it be used effectively?
- Combines data from import and DirectQuery.
- Useful for real-time and historical blending.
6. How does the USERELATIONSHIP function work, and when would you use it?
Activates an inactive relationship in DAX.
Example:
SalesByShipDate = CALCULATE(SUM(Sales[Amount]), USERELATIONSHIP(Sales[ShipDate], Date[Date]))
7. Explain how to use Power Query M language for advanced data transformations.
Used in Power Query Editor.
Examples:
- Merge/Append queries.
- Conditional columns.
- Parsing JSON/XML.
- Custom columns using functions.
8. Differentiate between CROSSFILTER and TREATAS in DAX.
CROSSFILTER:
- Modifies relationship filtering.
- Used for bidirectional filtering.
TREATAS:
- Converts table values into filter context.
- Apply filter from one table to unrelated one.