Spark SQL Tutorials for Data Engineers

Learn Spark SQL step by step, from your first SELECT to window functions and query plans. Every query comes with its real result.

Spark SQL Basics
Step 1

Introduction to Spark SQL

Learn what Spark SQL is, where to run it, and write your first queries on a sample employees table.

24 min readRead
Step 2

Tables & Views

Understand temp views, global temp views, managed and external tables, and how to create, fill and inspect them.

40 min readRead
Step 3

SELECT, Aliases & Sorting

Select columns and expressions, rename them with aliases, remove duplicates with DISTINCT, and sort with ORDER BY.

31 min readRead
Step 4

Filtering with WHERE

Filter rows with comparisons, AND/OR, IN, BETWEEN, LIKE and IS NULL, and avoid the common traps.

37 min readRead
Step 5

GROUP BY & Aggregations

Summarize data with COUNT, SUM, AVG, MIN and MAX, group it with GROUP BY, and filter groups with HAVING.

34 min readRead
Step 6

CASE WHEN & Conditional Logic

Add if-then-else logic to queries with CASE WHEN, IF, COALESCE and NULLIF, including conditional aggregation.

38 min readRead
Step 7

Handling NULLs

Learn how NULL behaves in comparisons, arithmetic, aggregates and NOT IN, and the functions that handle it safely.

37 min readRead
Step 8

Joins

Combine tables with inner, left, right, full, semi, anti, cross and self joins, and avoid duplicate and NULL key traps.

50 min readRead
Step 9

UNION, INTERSECT & EXCEPT

Stack and compare query results with UNION, UNION ALL, INTERSECT and EXCEPT.

34 min readRead
Step 10

Subqueries & CTEs

Break complex queries into steps with scalar subqueries, IN and EXISTS, derived tables and the WITH clause.

40 min readRead

Advanced Spark SQL Topics ๐Ÿ“˜

Window functions, nested data, pivots and query plans, explained with runnable examples.

Advanced Spark SQL
Step 11

String Functions

Clean and reshape text with concat, substring, trim, split, regexp_extract, regexp_replace and more.

40 min readRead
Step 12

Date & Time Functions

Parse, format, truncate and do arithmetic on dates and timestamps in Spark SQL.

37 min readRead
Step 13

Window Functions #1

Rank rows and compute per-group aggregates without collapsing rows, using OVER, PARTITION BY and ORDER BY.

35 min readRead
Step 14

LEAD & LAG #2

Compare each row with the previous or next one to compute changes, gaps and trends.

37 min readRead
Step 15

Window Frames #3

Build running totals and moving averages with ROWS and RANGE frames, and avoid the default frame trap.

35 min readRead
Step 16

ROLLUP, CUBE & GROUPING SETS

Produce subtotals and grand totals in a single query with GROUPING SETS, ROLLUP and CUBE.

33 min readRead
Step 17

PIVOT & UNPIVOT

Turn rows into columns with PIVOT and columns back into rows with UNPIVOT.

36 min readRead
Step 18

Arrays, Maps & Structs

Work with nested data: build and query arrays, maps and structs, explode them, and use higher-order functions.

40 min readRead
Step 19

EXPLAIN, Hints & Caching

Read query plans with EXPLAIN, steer joins with hints, and speed up repeated queries with caching.

48 min readRead