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.