30. Write retention query in SQL?
Example logic: Count users who appeared in month X and also in month X+1.
WITH this_month AS ( SELECT user_id FROM users WHERE MONTH(join_date) = '2024-01' ), next_month AS ( SELECT user_id FROM users WHERE MONTH(join_date) = '2024-02' ) SELECT COUNT(*) FROM this_month WHERE user_id IN (SELECT user_id FROM next_month);
31. Write year-on-year growth in SQL?
SELECT year, revenue, revenue - LAG(revenue) OVER (ORDER BY year) AS YoY_Growth FROM sales;
32. Write a query for cumulative sum in SQL?
SELECT date, revenue, SUM(revenue) OVER (ORDER BY date) AS cumulative_revenue FROM sales;
33. Difference between Function and Stored Procedure?
Functions return values and are used in expressions.
Stored Procedures do not return values directly, can contain complex logic.
34. Do we use variable in views?
No, variables cannot be used in views.
35. What are the limitations of views?
Cannot use variables
Can't call stored procedures
Not always updatable
Performance depends on underlying query
Hope it helps :)
Post #3755
1.32K
- ❤ 4