SQL & Analytics
Query, analyse, and summarise data with the language every data professional uses every single day.
Start Learning SQL & AnalyticsWhat You'll Learn
- The core SQL syntax: SELECT, FROM, WHERE, ORDER BY, LIMIT
- Aggregations: GROUP BY, COUNT, SUM, AVG, MIN, MAX, HAVING
- All types of JOINs: INNER, LEFT, RIGHT, FULL OUTER, CROSS
- Subqueries and Common Table Expressions (CTEs) for readable queries
- Window functions: ROW_NUMBER, RANK, LAG, LEAD, SUM OVER
- Date functions, string functions, and CASE WHEN logic
- Query optimisation: indexes, query plans, and avoiding common slow patterns
- Analytical SQL for business intelligence and reporting
Introduction to SQL & Analytics
SQL (Structured Query Language) is the universal language for working with relational databases and analytical data warehouses. It's been around since the 1970s, yet it remains one of the most in-demand skills in data engineering, data analysis, and data science in 2025. Whether you're working with PostgreSQL, MySQL, BigQuery, Snowflake, Redshift, or running Spark SQL — SQL is everywhere.
The core idea of SQL is simple: you describe what data you want (SELECT columns FROM a table WHERE some condition), and the database figures out how to retrieve it efficiently. For most data analysis tasks — filtering rows, calculating totals, ranking results, and joining tables — SQL is both faster to write and faster to run than equivalent Python code because it pushes computation to the database engine.
Modern analytics SQL goes beyond basic queries. Window functions let you calculate running totals, moving averages, and ranks without collapsing your data. Common Table Expressions (CTEs) let you write readable, modular queries. Advanced features like QUALIFY, UNPIVOT, and lateral joins are supported by modern warehouses like BigQuery and Snowflake, making SQL powerful enough to replace most pandas transformations at scale.
Video Tutorials
Handpicked free YouTube videos to accelerate your understanding
SQL Tutorial — Full Database Course for Beginners
The most popular SQL tutorial on YouTube — everything from CREATE TABLE to complex JOINs, subqueries, aggregate functions, and indexes.
Advanced SQL: Window Functions & CTEs
Master window functions (ROW_NUMBER, RANK, LAG, LEAD) and CTEs used in real data analyst and data engineer roles.
Window functions — the most powerful SQL feature for analytics
Copy the code below and paste it into your Python environment or our free online compiler.
-- Scenario: Sales data — calculate running total, rank, and growth
CREATE TABLE sales (
id INT,
salesperson VARCHAR(50),
region VARCHAR(50),
sale_date DATE,
amount DECIMAL(10,2)
);
-- 1. BASIC QUERY: Total sales per salesperson
SELECT
salesperson,
SUM(amount) AS total_sales,
COUNT(*) AS num_deals,
AVG(amount) AS avg_deal_size
FROM sales
GROUP BY salesperson
ORDER BY total_sales DESC;
-- 2. WINDOW FUNCTION: Running total by date (no GROUP BY needed!)
SELECT
sale_date,
salesperson,
amount,
SUM(amount) OVER (
PARTITION BY salesperson
ORDER BY sale_date
) AS running_total
FROM sales;
-- 3. RANK: Who is #1 in each region?
SELECT *
FROM (
SELECT
salesperson,
region,
SUM(amount) AS total,
RANK() OVER (
PARTITION BY region
ORDER BY SUM(amount) DESC
) AS regional_rank
FROM sales
GROUP BY salesperson, region
) ranked
WHERE regional_rank = 1;
-- 4. CTE + LAG: Month-over-month growth %
WITH monthly AS (
SELECT
DATE_TRUNC('month', sale_date) AS month,
SUM(amount) AS revenue
FROM sales
GROUP BY 1
)
SELECT
month,
revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_month,
ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
/ LAG(revenue) OVER (ORDER BY month), 2) AS growth_pct
FROM monthly;Key Concepts Explained
Master these terms and you'll understand 80% of the conversations in this field.
SELECT & FROM
The foundation of every SQL query. SELECT specifies which columns to return; FROM specifies the table. SELECT * FROM users returns all columns from the users table.
WHERE vs HAVING
WHERE filters individual rows before aggregation. HAVING filters groups after aggregation. Use WHERE for row-level conditions, HAVING for aggregate conditions like "groups with COUNT > 10".
JOIN
Combines rows from two tables based on a matching key. INNER JOIN returns only matching rows. LEFT JOIN returns all rows from the left table plus matches from the right (NULL if no match).
Window Function
Performs a calculation across a set of rows related to the current row without collapsing them into one. Uses the OVER() clause. Examples: ROW_NUMBER(), RANK(), LAG(), SUM() OVER().
CTE (Common Table Expression)
A named temporary result set defined with WITH clause_name AS (...). Makes long queries readable by breaking them into named steps. Think of it as a temporary view.
Index
A data structure that speeds up lookups on a column. Like a book's index — instead of scanning every row, the database jumps directly to matching rows. Critical for query performance.
Query Planner / EXPLAIN
Run EXPLAIN SELECT ... to see how the database will execute your query. Look for "Seq Scan" (slow, scans all rows) vs "Index Scan" (fast, uses an index).
Spark SQL
Apache Spark's SQL interface. You can run standard SQL on DataFrames registered as temp views: spark.sql("SELECT department, AVG(salary) FROM employees GROUP BY department").
Your SQL & Analytics Learning Path
Follow these steps in order — each one builds on the last. Designed for complete beginners.
- 1
Basic Queries
SELECT, WHERE, ORDER BY, LIMIT. Practice on a free tool like DB Fiddle or install PostgreSQL locally.
- 2
Aggregations
GROUP BY, COUNT, SUM, AVG, MAX, MIN. Understand why column aliases matter in GROUP BY.
- 3
JOINs
Master INNER, LEFT, and FULL OUTER JOINs. Practice joining 3+ tables. Understand NULL behaviour in joins.
- 4
Subqueries & CTEs
Write subqueries in WHERE and FROM. Refactor to CTEs for readability. Understand correlated vs non-correlated subqueries.
- 5
Window Functions
Learn ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, and SUM/AVG OVER with PARTITION BY and ORDER BY. These appear in almost every data interview.
- 6
Performance Tuning
Use EXPLAIN to read query plans. Add indexes. Rewrite queries to avoid full table scans. Understand partitioned tables.
- 7
Analytical Warehouses
Practice BigQuery, Snowflake, or Redshift SQL. Learn their unique features: QUALIFY, FLATTEN, UNNEST, and materialised views.
Ready to master SQL & Analytics?
Explore our free tutorials, hands-on code examples, and interview questions. No sign-up. No paywalls. Forever free.