Post content
π Data Analyst Interview Questions with Answers β Part 2 π SQL & Databases 11. What is SQL and why is it critical for data analysts? SQL (Structured Query Language) is used to communicate with databases. It helps analysts retrieve, filter, clean, and analyze data efficiently. It is critical because most business data is stored in databases, and SQL allows analysts to extract insights directly from large datasets. 12. How do "SELECT", "WHERE", "ORDER BY", and "LIMIT" work? β "SELECT" β Used to choose columns from a table SELECT name, salary FROM employees; β "WHERE" β Filters rows based on conditions SELECT FROM employees WHERE salary > 50000; β "ORDER BY" β Sorts data ascending or descending SELECT FROM employees ORDER BY salary DESC; β "LIMIT" β Restricts the number of rows returned SELECT FROM employees LIMIT 5; 13. How do you join two tables ("INNER", "LEFT", "RIGHT", "FULL" joins)? π "INNER JOIN" β Returns matching records from both tables π "LEFT JOIN" β Returns all records from the left table + matching rows from the right table π "RIGHT JOIN" β Returns all records from the right table + matching rows from the left table π "FULL JOIN" β Returns all matching and non-matching records from both tables Example: SELECT customers.name, orders.order_id FROM customers INNER JOIN orders ON customers.id = orders.customer_id; 14. How do "GROUP BY" and aggregate functions work? Aggregate functions summarize data. Common functions: βοΈ "SUM()" βοΈ "AVG()" βοΈ "COUNT()" βοΈ "MAX()" βοΈ "MIN()" Example: SELECT department, AVG(salary) FROM employees GROUP BY department; This groups employees by department and calculates average salary. 15. How do you write subqueries and CTEs? π Subquery β Query inside another query SELECT name FROM employees WHERE salary > ( SELECT AVG(salary) FROM employees ); π CTE (Common Table Expression) β Temporary result set that improves readability WITH high_salary AS ( SELECT FROM employees WHERE salary > 50000 ) SELECT FROM high_salary; 16. How do you calculate running totals or rolling averages with window functions? Window functions perform calculations across rows without collapsing data. Example β Running Total: SELECT order_date, sales, SUM(sales) OVER (ORDER BY order_date) AS running_total FROM orders; Example β Rolling Average: SELECT order_date, AVG(sales) OVER ( ORDER BY order_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW ) AS rolling_avg FROM orders; 17. How do you clean and filter data directly in SQL? Data cleaning in SQL includes: βοΈ Removing duplicates βοΈ Handling NULL values βοΈ Standardizing text βοΈ Filtering invalid rows Example: SELECT TRIM(LOWER(name)) FROM customers WHERE email IS NOT NULL; 18. How do you handle duplicates and NULL values in SQL? β Remove duplicates using "DISTINCT" SELECT DISTINCT city FROM customers; β Find NULL values SELECT FROM employees WHERE salary IS NULL; β Replace NULL values SELECT COALESCE(salary, 0) FROM employees; 19. How do you optimize a slow query? Common optimization techniques: π Use indexes π Avoid unnecessary columns in "SELECT *" π Filter data early using "WHERE" π Optimize joins π Use proper aggregations π Analyze execution plans Efficient queries improve performance and reduce database load. 20. How do you design a simple schema for a business domain? A schema organizes data into related tables. Example for an e-commerce business: π "Customers" table π "Orders" table π "Products" table π "Payments" table Relationships are created using primary keys and foreign keys to maintain data integrity. πDouble Tap β€οΈ For Part-3