SQL & PostgreSQL Coding Interview Questions
(100)
1 1. Write a query to display all records from a table.
2 2. Select only specific columns from a table.
3 3. Filter records using a WHERE condition.
4 4. Find employees with salary greater than 50,000.
5 5. Find employees belonging to a specific department.
6 6. Sort employees by salary in descending order.
7 7. Count total number of rows in a table.
8 8. Find the maximum salary from employees table.
9 9. Find the minimum salary.
10 10. Calculate average salary.
11 11. Count employees in each department.
12 12. Display unique values from a column.
13 13. Find employees whose name starts with 'A'.
14 14. Find employees whose name ends with 'n'.
15 15. Find employees whose salary is between two values.
16 16. Display top 5 highest salaries.
17 17. Find employees hired after 2022.
18 18. Update salary of an employee.
19 19. Delete employees with salary less than 20,000.
20 20. Create a new table.
21 21. Insert records into a table.
22 22. Add a new column to an existing table.
23 23. Rename a column.
24 24. Drop a table.
25 25. Use GROUP BY to count employees in departments.
26 26. Use HAVING to filter grouped results.
27 27. Use CASE to categorize salaries as High/Medium/Low.
28 28. Find duplicate values in a column.
29 29. Display first 10 rows using LIMIT.
30 30. Use DISTINCT to remove duplicate results.
31 31. Find the second highest salary.
32 32. Find the third highest salary.
33 33. Find Nth highest salary.
34 34. Find duplicate rows in a table.
35 35. Delete duplicate rows but keep one record.
36 36. Find employees earning more than average salary.
37 37. Find employees who joined in the last 30 days.
38 38. Find department with highest average salary.
39 39. Find employees with the same salary.
40 40. Find employees with salary greater than their manager.
41 41. Find employee-manager pairs using self join.
42 42. Find customers who never placed an order.
43 43. Find customers who placed more than 5 orders.
44 44. Calculate total sales per customer.
45 45. Calculate monthly sales revenue.
46 46. Find most selling product.
47 47. Find top 3 customers by revenue.
48 48. Find employees hired each year.
49 49. Find department-wise highest salary.
50 50. Use ROW_NUMBER() to rank employees.
51 51. Use RANK() to rank employees by salary.
52 52. Use DENSE_RANK() to rank salaries.
53 53. Calculate running total of sales.
54 54. Find employees with highest salary per department.
55 55. Find employees whose salary is above department average.
56 56. Convert rows into columns (pivot).
57 57. Find records present in one table but not another.
58 58. Use EXISTS to filter records.
59 59. Use IN with subquery.
60 60. Write correlated subquery example.
61 61. Write query using Common Table Expression (CTE).
62 62. Find employees working in multiple departments.
63 63. Find missing numbers in a sequence.
64 64. Calculate percentage of total sales.
65 65. Find consecutive dates in sales data.
66 66. Find users who logged in multiple days.
67 67. Identify gaps between dates.
68 68. Find median salary.
69 69. Rank employees by department salary.
70 70. Write query to paginate results.
71 71. Find top employee per department using window functions.
72 72. Calculate moving average of sales over 7 days.
73 73. Find customers who ordered consecutively for 3 months.
74 74. Detect duplicate transactions.
75 75. Find employees who changed departments.
76 76. Calculate year-over-year growth.
77 77. Identify churned customers.
78 78. Find longest consecutive login streak.
79 79. Generate calendar table using SQL.
80 80. Identify gaps in employee ID sequence.
81 81. Write recursive query to display hierarchical employee tree.
82 82. Use recursive CTE for organizational structure.
83 83. Calculate cumulative revenue per month.
84 84. Identify top 10% performing employees.
85 85. Write query to split comma-separated values.
86 86. Implement row-level ranking within partitions.
87 87. Write SQL to simulate pivot table.
88 88. Find overlapping date ranges.
89 89. Calculate rolling sum of sales.
90 90. Write SQL for dynamic ranking by category.
91 91. Create a table using SERIAL primary key in PostgreSQL.
92 92. Create table with JSONB column.
93 93. Insert JSON data into table.
94 94. Query values from JSONB column.
95 95. Create GIN index on JSONB column.
96 96. Create partitioned table in PostgreSQL.
97 97. Write PL/pgSQL function to return employee count.
98 98. Write trigger to update updated_at column automatically.
99 99. Analyze query performance using EXPLAIN ANALYZE.
100100. Write command to backup PostgreSQL database using pg_dump.