SQL Basics
SQL is how you talk to databases — and it's the most in-demand skill in data. Here's how it works, what you can do with it, and how to write your first queries.
The spreadsheet that broke
A marketing team at a mid-size SaaS company tracked everything in spreadsheets. Customer data, campaign results, revenue — all in interconnected Google Sheets. It worked when they had 500 customers.
Then they hit 50,000 customers. The spreadsheets froze, crashed, and gave wrong answers because formulas broke when rows were added. Reports that took 10 minutes now took 2 hours. Nobody trusted the numbers.
The fix wasn't a bigger spreadsheet. It was a database — and SQL to talk to it. Within a month, the same reports ran in seconds. Queries that would have taken an analyst all day took 3 lines of SQL.
What SQL actually is
SQL (Structured Query Language, pronounced "sequel" or "S-Q-L" — both are correct) is a language for managing and querying data stored in relational databases.
A relational database is just organized tables — like spreadsheets, but with superpowers:
✗ Without AI
- ✗Rows and columns in a file
- ✗Manual formulas for calculations
- ✗Breaks with large data
- ✗One person edits at a time
- ✗Data gets messy over time
✓ With AI
- ✓Rows and columns in structured tables
- ✓Queries for calculations (instant, reusable)
- ✓Handles millions of rows easily
- ✓Many people query simultaneously
- ✓Rules enforce data quality
The key concepts
| Concept | What it means | Spreadsheet equivalent |
|---|---|---|
| Table | A collection of related data | A sheet/tab |
| Row | A single record | A row in your sheet |
| Column | A specific attribute | A column header |
| Query | A question you ask the database | A formula or filter |
| Schema | The structure of your tables | Your column headers and data types |
There Are No Dumb Questions
Do I need to install anything to learn SQL?
No. Free browser tools like SQLite Online, DB Fiddle, or Mode Analytics let you write queries immediately. No setup required.
Is SQL a programming language?
It's a query language — simpler than Python or JavaScript. You're describing WHAT data you want, not HOW to get it. Most people learn the basics in a weekend.
Which database should I learn?
The SQL syntax is 90% the same across all databases. Start with any. The most common are PostgreSQL (most popular), MySQL, SQLite (simplest), and SQL Server (enterprise).
Your first SQL queries
SELECT — Getting data
The most fundamental command. "Show me this data."
SELECT name, email, signup_date
FROM customers;
This says: "From the customers table, show me the name, email, and signup_date columns." That's it.
Want everything? Use *:
SELECT * FROM customers;
WHERE — Filtering data
"Show me this data, but only rows that match a condition."
SELECT name, email
FROM customers
WHERE country = 'United States';
You can combine conditions:
SELECT name, email, total_spent
FROM customers
WHERE country = 'United States'
AND total_spent > 100
AND signup_date > '2025-01-01';
ORDER BY — Sorting results
SELECT name, total_spent
FROM customers
ORDER BY total_spent DESC;
DESC = descending (highest first). ASC = ascending (lowest first, default).
LIMIT — Capping results
SELECT name, total_spent
FROM customers
ORDER BY total_spent DESC
LIMIT 10;
"Show me the top 10 customers by spending."
Write your first query
25 XPAggregating data
This is where SQL gets powerful — summarizing thousands of rows into useful numbers.
COUNT, SUM, AVG, MIN, MAX
-- How many customers do we have?
SELECT COUNT(*) FROM customers;
-- What's our total revenue?
SELECT SUM(amount) FROM orders;
-- What's the average order value?
SELECT AVG(amount) FROM orders;
-- What's the biggest single order?
SELECT MAX(amount) FROM orders;
GROUP BY — Aggregating by category
"Show me the total revenue broken down by country."
SELECT country, SUM(amount) as total_revenue, COUNT(*) as order_count
FROM orders
GROUP BY country
ORDER BY total_revenue DESC;
This is the equivalent of a pivot table — but faster and reproducible.
There Are No Dumb Questions
What's the difference between WHERE and HAVING?
WHERE filters individual rows BEFORE grouping. HAVING filters groups AFTER grouping. Example: WHERE filters out small orders; HAVING filters out countries with few orders.
Can I save a query and reuse it?
Yes — that's called a "view" or a "saved query." Most database tools let you save queries. In production, developers create views that act like virtual tables.
JOINs — Combining tables
Real databases have multiple related tables. JOINs connect them.
Imagine two tables:
customers— id, name, emailorders— id, customer_id, product, amount
To see customer names with their orders:
SELECT customers.name, orders.product, orders.amount
FROM orders
JOIN customers ON orders.customer_id = customers.id;
This "joins" the two tables on the matching IDs. Every analyst uses JOINs daily.
Analyze a dataset
50 XPReal-world SQL use cases
| Role | How they use SQL |
|---|---|
| Marketing analyst | Pull campaign performance, segment customers, track conversion funnels |
| Product manager | Analyze feature usage, track retention, measure A/B test results |
| Financial analyst | Generate revenue reports, track expenses, audit transactions |
| Data scientist | Extract training data, validate model predictions, create features |
| Software developer | Build application backends, optimize queries, manage data |
| Operations | Track inventory, monitor KPIs, generate compliance reports |
Getting started today
Key takeaways
- SQL is how you query databases — the most in-demand data skill across all industries
- SELECT gets data, WHERE filters it, GROUP BY summarizes it, JOIN connects tables
- Aggregate functions (COUNT, SUM, AVG) turn thousands of rows into useful numbers
- SQL appears in over half of data job postings and 58.6% of developers use it regularly (Stack Overflow Developer Survey, 2023)
- You can learn the core commands in a weekend with free browser-based tools
- Every role that touches data benefits from SQL — marketing, product, finance, engineering
Knowledge Check
1.What does the SQL keyword SELECT do?
2.What is the difference between WHERE and HAVING?
3.What does a JOIN do in SQL?
4.Which SQL command would you use to find the average order amount per country?