beginnerdbms

SQL Basics

The language you use to ask a database a question.

Key Points

What it is

SQL (Structured Query Language) is how you interact with a relational database. You write queries that describe what data you want, and the database figures out how to get it. The four core operations are SELECT, INSERT, UPDATE, and DELETE.

Why it exists

You need a standard way to talk to a database regardless of which system it's running on. SQL was designed to be readable, close to English, and declarative — you say what you want, not how to fetch it.

Intuition

You have a table of orders. You want to know which customers spent more than $500 in the last 30 days. In plain English: look at the orders table, filter to last 30 days, group by customer, add up their totals, and only keep the ones over $500. SQL is almost that sentence: SELECT customer_id, SUM(amount) AS total FROM orders WHERE order_date >= NOW() - INTERVAL 30 DAY GROUP BY customer_id HAVING total > 500 That's it. The structure mirrors the thought process.

How it works

  1. FROM

    Database picks the table (or joins) to work with first.

  2. WHERE

    Rows that don't match the condition are filtered out.

  3. GROUP BY

    Remaining rows are collapsed into groups.

  4. HAVING

    Groups that don't match the condition are filtered out.

  5. SELECT

    The columns and expressions in SELECT are computed and returned.

  6. ORDER BY / LIMIT

    Results are sorted and capped if specified.

When to use

Common mistakes

Notes

In practice

SQL is the most common way analysts and data scientists pull data from production databases or data warehouses. Tools like dbt, Pandas, and Spark all have SQL interfaces. Knowing SQL well means you can explore data directly without waiting for someone else to extract it for you.

Was this concept helpful?

Spot an error or gap? Suggest an edit →

Related concepts

relational-modeljoinsnormalizationindexes
Chirp at me!