π SQL Coding Interview Questions
4 min read
656 words
π©βπΌ Business Analyst (BA)
Focus: Data exploration, summarization, and basic business insight queries.
πΉ Basic Selection & Filtering
- Q1: Retrieve all customer records located in a specific city
- Q2: Find employees whose names start with βAβ
- Q3: Display unique department names from the employee table
- Q4: Fetch employees sorted by their joining date (latest first)
- Q5: Get the top 10 highest-paid employees
- Q6: Retrieve all transactions made after January 1, 2024
πΉ Aggregation & Grouping
- Q7: Find the total salary expense for each department
- Q8: Count how many employees belong to each department
- Q9: Get departments with more than 5 employees
- Q10: Find the average salary per job title
- Q11: Retrieve total sales per region
- Q12: Count the total number of customers who placed at least one order
πΉ Business Insight Queries
- Q13: Find customers who made transactions above $5,000 more than once
- Q14: Identify the most profitable regions by total transaction amount
- Q15: Find customers who have not purchased anything in the last 6 months
- Q16: Calculate ARPU (Average Revenue Per User)
- Q17: Calculate the monthly customer retention rate
- Q18: Identify churned customers (inactive in the last 6 months)
π Data Analyst (DA)
Focus: Joins, subqueries, analytical windows, and transformations.
πΉ Joins & Relationships
- Q1: Combine employee and department data using an INNER JOIN
- Q2: List all employees with their departments (even if unassigned) using LEFT JOIN
- Q3: Retrieve all data from both tables with FULL OUTER JOIN
- Q4: Perform a SELF JOIN to show employees along with their managers
- Q5: Find employees who do not belong to any department
- Q6: Display products purchased by customers from more than one country
πΉ Subqueries & Nested Logic
- Q7: Find employees who earn more than the average salary
- Q8: Fetch the second-highest salary using subquery and DENSE_RANK
- Q9: Retrieve products that have never been sold
- Q10: Display customers who never placed an order
- Q11: Identify departments that donβt have any employees hired after 2020
- Q12: Retrieve customers whose total purchases exceed the overall customer average
πΉ Window Functions & Ranking
- Q13: Assign ranks to employees by salary within department
- Q14: Calculate cumulative (running) sales per employee
- Q15: Find the difference between each monthβs and previous monthβs sales
- Q16: Show employee salaries and the salary gap vs department average
- Q17: Find the Nth highest salary in each department
- Q18: Calculate the moving average of posts per user (7-day window)
πΉ String, Date & Scenario-Based Analytics
π§βπ» SQL Developer (SD)
Focus: Engineering-grade SQL β schema design, performance tuning, and optimization.
πΉ Advanced Joins & Relationships
- Q1: Use a CROSS JOIN to generate productβregion combinations
- Q2: Find students who enrolled in all available courses
- Q3: List managers who oversee more than 3 distinct departments
- Q4: Identify employees who earn less than their managers
πΉ Advanced Aggregations & Grouping
- Q5: Departments where average salary exceeds company average
- Q6: Employees earning more than their departmentβs average
- Q7: Cumulative percentage contribution of sales per region
πΉ Complex Window & Analytics
- Q8: Employee performance rank reset every quarter
- Q9: Monthly revenue change (month-over-month)
- Q10: YoY growth for each product
- Q11: Products with 3 consecutive months of sales decline
- Q12: Assign ROW_NUMBER to employees ordered by salary
πΉ Data Cleaning & Transformation
- Q13: Delete duplicate rows from a table
- Q14: Keep only the first occurrence (lowest ID)
- Q15: Standardize phone numbers (remove special characters)
- Q16: Split address into street, city, and state
- Q17: Convert customer names into Proper Case
πΉ Schema Design & Implementation
- Q18: Design tables to store credit card transactions (normalized)
- Q19: Create an index on customer_id for fast lookups
- Q20: Implement partitioning for large tables
- Q21: Create a temporary table for summary results
- Q22: Create a view summarizing customer purchases
πΉ Optimization & Query Design
- Q23: Rewrite a correlated subquery as a JOIN
- Q24: Compare indexed vs non-indexed query performance
- Q25: Demonstrate safe use of query hints (USE INDEX, FORCE ORDER)