SQL Basics Guidebook
๐ฏ Purpose¶
This guidebook covers foundational SQL skills for analysts โ focusing on querying, filtering, joining, grouping, and handling common data types and null logic.
1๏ธโฃ SELECT and Filtering¶
๐น Basic Query¶
SELECT column1, column2
FROM table_name;
๐น WHERE Clause¶
SELECT *
FROM users
WHERE age >= 18 AND status = 'active';
- Use
AND
,OR
,NOT
,IN
,BETWEEN
, andLIKE
as needed
๐น ORDER BY¶
ORDER BY created_at DESC;
๐น LIMIT (Top N Rows)¶
LIMIT 10;
2๏ธโฃ JOINs (Core Tool for Analysts)¶
๐น INNER JOIN¶
SELECT u.id, o.order_id
FROM users u
JOIN orders o ON u.id = o.user_id;
- Returns only matching rows between both tables
๐น LEFT JOIN¶
SELECT u.id, o.order_id
FROM users u
LEFT JOIN orders o ON u.id = o.user_id;
- Includes all users, even those with no orders (NULL-filled)
๐น FULL OUTER JOIN (If supported)¶
- Includes all rows from both tables
๐น SELF JOIN (e.g., user referral networks)¶
SELECT a.name, b.name AS referred_by
FROM users a
JOIN users b ON a.referred_by = b.id;
3๏ธโฃ GROUP BY + Aggregations¶
๐น Basic GROUP BY¶
SELECT status, COUNT(*)
FROM users
GROUP BY status;
๐น HAVING Clause (Filter post-aggregation)¶
SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 5;
๐น Common Aggregates¶
COUNT(*)
,SUM()
,AVG()
,MAX()
,MIN()
4๏ธโฃ Null Handling¶
๐น COALESCE (Fallback value)¶
SELECT COALESCE(email, 'missing@example.com') AS email_cleaned
FROM users;
๐น IS NULL / IS NOT NULL¶
WHERE deleted_at IS NULL
5๏ธโฃ CASE Statements¶
SELECT
user_id,
CASE
WHEN score >= 90 THEN 'gold'
WHEN score >= 70 THEN 'silver'
ELSE 'bronze'
END AS tier
FROM scores;
- Useful for tiering, segmentation, conditional logic
โ Analyst Use Cases This Covers¶
- Pulling raw data for EDA
- Segmenting users, orders, or events
- Counting or ranking behaviors (signup, conversion)
- Filtering production tables for modeling
๐ก Tip¶
โMost analysis starts with SELECT. Master joins, filters, and groups โ everything else builds from there.โ