0% found this document useful (0 votes)
16 views

SQL Questions

Uploaded by

gokul nath
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

SQL Questions

Uploaded by

gokul nath
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

: You have three tables: Customers, Orders, and


Payments. Write a query to find all customers who have made
an order but have not completed a payment in the last 30 days.

2. For each product sold, calculate the running total of


the number of sales made over time, ordered by sale date

3. Given a Purchases table, find customers who have


made more than 5 purchases in the last 6 months, along with
their total spend.

4. You have a table of student grades. Write a query to


find students who do not have grades for all subjects.

5. Write a query to find the top 3 employees with the


highest sales in the last year.

6. How would you write a query to find the 90th


percentile of sales from a Sales table?

7. How can you write a query that sums the total sales
but treats null values as zero?

8. : Write a query to find the average time taken


between order creation and delivery

9. Write a query to calculate the 90th percentile of


sales amounts.

10. Write a query to calculate a running total of sales for


each product

11. How can you handle NULL values in a SUM operation


to ensure they are treated as zero?

12. : Write a query to identify duplicate rows in a table


based on a combination of columns

13. : Write a query that categorizes sales amounts into


different ranges using the CASE statement.

14. : Write a query to rank employees based on their sales


performance within each department.

15. Write a query to compare each employee's sales with


their previous month's sales.

16. Write a query to count the number of unique


customers who placed orders in the last year

17. Write a query to calculate the total sales for each


product category, with a separate total for 'Electronics'.

18. Write a query to calculate the number of days


between an order date and a delivery date

19. How can you write a query to detect missing invoice


numbers in a sequence of invoices?
20. How would you calculate the moving average in SQL?

21. How would you identify customers who made a purchase


last year but not this year

22. Given a sales table, how do you find the top 3 sales
representatives based on total sales?

23. How would you find customers who’ve bought all products
in a given product list?

24. How do you identify consecutive absences in attendance


data?

25. How would you calculate the percentage growth in sales


month over month?

26. How can you identify orders that have been shipped but
not yet billed?

27. How would you rank employees within departments based


on their performance score?

28. How do you find the first purchase made by each


customer?

29. How do you identify orders with a price change compared


to the previous order?

30. How do you find customers who placed orders in two


consecutive months

31. How would you handle a situation where you need to


delete duplicate records, keeping only the latest entry?

32. How would you retrieve the order with the maximum
amount for each customer?

33. How would you identify the best-selling product each


month?

34. How do you retrieve the last three months’ average sales
per day?

35. How do you select all employees with higher salaries than
their department average

36. find employee whose salary is greater than avg salary in a dept.

---------------------------------------------------------------
advanced:

Write a dynamic SQL query to generate a report of


sales per month for a given year.

Dynamic SQL Execution


Question: Write a dynamic SQL query that generates a report
of sales by region for a given year.

Time-Based Performance Data Analysis


Question: You are given a table Service_Requests with columns
request_id, created_at, and resolved_at. Write a query to
calculate the average time taken to resolve a request (in
hours), excluding weekends.

: How do you implement full-text search on a column


in SQL?

Working with Hierarchical Data


Question: How do you query hierarchical data stored in a self referencing table

Recursive CTE for Hierarchies


Question: How do you write a recursive CTE to navigate a
hierarchy of departments?

Write a recursive CTE to find all employees in an


organization and their respective managers, given an
Employees table.

Advanced Use of GROUP BY with ROLLUP and CUBE


Question: Write a query to calculate total sales with subtotals
for each product and grand total.

How would you manage concurrency issues in SQL,


such as the lost update problem?

------------------------------------------------------------
theory

How can you identify and optimize slow-running


queries in SQL?

Explain how partitioning works in SQL and provide


an example of partitioning a table by date.

What is the purpose of the EXPLAIN statement in SQL?

Explain a scenario where you’d use a SELF JOIN.

What is the difference between UNION and UNION ALL?

What is a TRIGGER and when would you use it?

What is the difference between DELETE and TRUNCATE?

How would you handle time zone conversions in SQL?

difference btw exists, IN (affecting join performance)

cardinality

You might also like