Skip to content

Introduction to SQL for Analysis

For people

The SQL every analyst needs: SELECT, WHERE, GROUP BY, JOIN and ORDER BY, with examples.

Editorial team 1 min read

Share X LinkedIn Reddit Bluesky Email

SQL (Structured Query Language) is the standard way to get data out of relational databases and data warehouses.

Selecting Data

SELECT customer_id, order_date, total
FROM orders
WHERE order_date >= '2026-01-01'
ORDER BY total DESC
LIMIT 10;

Aggregating

SELECT region, COUNT(*) AS orders, SUM(total) AS revenue, AVG(total) AS avg_order
FROM orders
GROUP BY region
HAVING SUM(total) > 10000;

WHERE filters rows before grouping; HAVING filters groups after aggregation.

Joining Tables

SELECT c.name, o.order_date, o.total
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id;

Use LEFT JOIN to keep all rows from the first table even without matches.

Handling Missing Values

NULL means unknown. Use IS NULL and IS NOT NULL (never = NULL), and COALESCE(value, default) to substitute defaults. Aggregates such as AVG ignore NULLs.

Good Habits

  • Select only the columns you need.
  • Check row counts after joins to catch duplication.
  • Use clear aliases and consistent formatting.
  • Comment complex logic.
  • Test queries on small date ranges first.

Next Steps

Window functions, common table expressions (WITH) and date functions unlock most analytical questions.

More in Data science & analytics

All Data science & analytics guides →
Data science & analytics Guide · 2 min

Descriptive Statistics Essentials

Mean, median, mode, spread and shape: the summary numbers every analysis starts with, and when each one misleads.

Data science & analytics 2 min read 14 Mar 2026 For people

Data science & analytics Guide · 2 min

Probability Basics for Data Work

The probability ideas analysts use every day: events, conditional probability, independence and Bayes' theorem.

Data science & analytics 2 min read 13 Mar 2026 For people

Data science & analytics Guide · 2 min

Common Probability Distributions

Normal, binomial, Poisson, exponential and more: recognising the shapes data takes and what they imply.

Data science & analytics 2 min read 12 Mar 2026 For people

Data science & analytics Guide · 2 min

Hypothesis Testing Explained

Null hypotheses, p-values and significance: what a hypothesis test tells you, and the misunderstandings to avoid.

Data science & analytics 2 min read 11 Mar 2026 For people

Discussion 0

Use @handle to bring somebody in, and #tag to file it.

Nothing here yet. Ask a question, or say how you used it.

Sign in to join the discussion. Sign in