Interview sql
π Find Duplicate Records SELECT col1, col2, COUNT ( * ) FROM table_name GROUP BY col1, col2 HAVING COUNT ( * ) > 1 ; π Second Highest Salary SELECT MAX(salary) FROM employees WHERE salary < ( SELECT MAX(salary) FROM employees); π Nth Highest Salary SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER ( ORDER BY salary DESC ) rnk FROM employees ) t WHERE rnk = N; π Top 3 Sales per Region SELECT * FROM ( SELECT * , ROW_NUMBER() OVER (PARTITION BY region ORDER BY sales DESC ) rn FROM sales ) t WHERE rn <= 3 ; π Consecutive Purchases SELECT customer_id FROM ( SELECT customer_id, order_date, LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date) prev_date FROM orders ) t WHERE order_date = prev_date + INTERVAL '1' DAY ; πRemove Duplicates DELETE FROM table_name WHERE id NOT IN ( SELECT MIN(id) FROM table_name GROUP BY col1, col2 ); π Write a qu...