Skip to main content
Albero Academy

SQL Intro — SELECT, WHERE, ORDER BY

Get started with SQL — the SELECT statement, filtering with WHERE, sorting with ORDER BY, and the basics of relational thinking.

Chapter 1SQL14 min readBeginner friendly
  • #SQL
  • #Basics
  • #Queries

SQL is the language every analyst, data engineer, and backend developer needs. It's been around since the 1970s and isn't going anywhere — every major database speaks a dialect of it. This chapter covers the four statements that handle most everyday work.

Why SQL?

  • Universal. Postgres, MySQL, SQL Server, BigQuery, Snowflake, Redshift, ClickHouse — they all speak SQL.
  • Declarative. You describe what you want, not how to get it. The query planner figures out the rest.
  • Hire-able. Every analytics, data, and BI role lists SQL as a primary requirement.

Anatomy of a SELECT

employees
SELECT name, department, salary
FROM employees
WHERE salary > 100000
ORDER BY salary DESC
LIMIT 10;

SQL clauses run in a fixed logical order, even though you write them in a different order:

Written orderLogical order
SELECTFROM
FROMWHERE
WHEREGROUP BY
GROUP BYHAVING
HAVINGSELECT
ORDER BYORDER BY
LIMITLIMIT

Filtering with WHERE

SQL
-- Comparison operators
SELECT * FROM employees
WHERE salary >= 100000;
-- AND / OR
SELECT * FROM employees
WHERE department = 'Engineering' AND salary > 80000;
-- IN — match any value in a list
SELECT * FROM employees
WHERE department IN ('Engineering', 'Design', 'Product');
-- BETWEEN — inclusive range
SELECT * FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-03-31';
-- LIKE — wildcards
SELECT * FROM customers
WHERE email LIKE '%@gmail.com'; -- ends with
SELECT * FROM products
WHERE name LIKE 'Apple%'; -- starts with
-- NULL checks — = NULL never matches!
SELECT * FROM users
WHERE referral_code IS NULL;

ORDER BY & LIMIT

SQL
-- Sort ascending (default)
SELECT name, salary FROM employees ORDER BY salary;
-- Sort descending
SELECT name, salary FROM employees ORDER BY salary DESC;
-- Multi-column sort
SELECT name, department, salary FROM employees
ORDER BY department ASC, salary DESC;
-- Top 5
SELECT name, salary FROM employees
ORDER BY salary DESC
LIMIT 5;
-- Pagination — Postgres / MySQL
SELECT name FROM employees
ORDER BY name
LIMIT 20 OFFSET 40; -- skip 40, take 20

Aggregations & GROUP BY

The other half of SQL: collapsing many rows into one.

FunctionReturns
COUNT(*)number of rows
COUNT(col)non-NULL values in col
SUM(col)total of numeric col
AVG(col)mean of col
MIN(col) / MAX(col)extremes
SQL
-- Count rows
SELECT COUNT(*) FROM orders;
-- Group by department
SELECT department,
COUNT(*) AS headcount,
AVG(salary) AS avg_salary,
MAX(salary) AS top_salary
FROM employees
GROUP BY department
ORDER BY headcount DESC;
-- Filter on the aggregate — use HAVING, not WHERE
SELECT department, COUNT(*) AS cnt
FROM employees
GROUP BY department
HAVING COUNT(*) >= 10;

Key takeaways

  1. 01SQL is declarative — describe what you want; the engine figures out how.
  2. 02Logical evaluation order is FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.
  3. 03Always use IS NULL / IS NOT NULL — never = NULL.
  4. 04WHERE filters rows before aggregation; HAVING filters groups after.
  5. 05Five aggregates handle 90% of work: COUNT, SUM, AVG, MIN, MAX.