SQL for Data Analysis: Beyond the Basics
SQL for Data Analysis: Beyond the Basics
SQL remains one of the most important tools in a data analyst's toolkit. While basic SELECT statements will get you started, mastering advanced SQL techniques can dramatically improve your efficiency and analytical capabilities.
Window Functions
Window functions perform calculations across a set of table rows related to the current row. They're incredibly powerful for analytics:
SELECT
department,
employee_name,
salary,
AVG(salary) OVER (PARTITION BY department) as dept_avg_salary,
salary - AVG(salary) OVER (PARTITION BY department) as diff_from_avg
FROM employees;
This query shows each employee's salary compared to their department's average.
Common Table Expressions (CTEs)
CTEs make complex queries more readable by breaking them into named temporary result sets:
WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', order_date) as month,
SUM(amount) as total_sales
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
)
SELECT
month,
total_sales,
LAG(total_sales) OVER (ORDER BY month) as prev_month_sales,
(total_sales - LAG(total_sales) OVER (ORDER BY month)) / LAG(total_sales) OVER (ORDER BY month) * 100 as growth_pct
FROM monthly_sales
ORDER BY month;
This query calculates month-over-month sales growth.
Advanced Joins and Set Operations
Beyond basic joins, techniques like self-joins and set operations can solve complex problems:
-- Self-join to find employees with the same manager
SELECT
e1.employee_name,
e2.employee_name as colleague
FROM employees e1
JOIN employees e2
ON e1.manager_id = e2.manager_id
AND e1.employee_id < e2.employee_id;
Optimizing Query Performance
As your datasets grow, query performance becomes crucial:
- Use indexes wisely on columns frequently used in WHERE, JOIN, and ORDER BY clauses
- Be specific in SELECT - avoid SELECT * when you only need specific columns
- Filter early - apply WHERE conditions before joins when possible
- Consider query execution plans to identify bottlenecks
By mastering these advanced SQL techniques, you'll be able to extract more insights from your data more efficiently.
Share this article
You might also like
Building Interactive Dashboards with Power BI
A step-by-step guide to creating interactive and insightful dashboards using Microsoft Power BI.
Data Cleaning Techniques with Python
Essential techniques for cleaning and preparing your data for analysis using Python libraries.
Sales Performance Dashboard
A comprehensive Power BI dashboard analyzing sales performance across regions and product categories.