Posts

Interview sql

Image
πŸš€ 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...

DSA

Image
   DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on processing this data. ========================================================================== Analysis of Algorithms Algorithm analysis is an important part of computational complexity theory, which provides theoretical estimation for the required resources of an algorithm to solve a specific computational problem. How do we compare two order of growths? The following are some standard terms that we must remember for comparison. c < Log Log n < Log n < n 1/3 < n 1/2 < n < n Log n < n 2 < n 2 Log n < n 3 < n 4 < 2 n < n n Here c is a constant To analyze loops for complexity: 1.  Understand the loop bounds Linear loop:   for...