Skip to content

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, and LIKE 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.โ€