SQL Tutorial 0/85 lessons ~6 min read Lesson 11
SELECT Queries
SELECT reads data.
Course progress0%
Focus
7 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
SELECT reads data. It is the bread-and-butter of SQL — you'll write thousands of SELECTs in your career. Mastering it (filters, joins, aggregations, windows) is what separates beginners from senior engineers.
Beginner analogy: SELECT is asking the librarian: 'find me all books from 2020 by author X, sorted by title, give me the first 10.'
Understanding the topic
Core concepts to understand:
SELECT col1, col2 FROM table WHERE ... ORDER BY ... LIMIT n;- Use
SELECT *only for exploration — name columns in production. - Logical execution order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.
DISTINCTremoves duplicates.- Combine with JOIN, GROUP BY, window functions for power.
Syntax reference
Visual workflow / architecture:
bash
SELECT email, count(*) AS ordersFROM orders oJOIN users u ON u.id = o.user_idWHERE o.created_at > now() - interval '30 days'GROUP BY u.emailHAVING count(*) > 5ORDER BY orders DESCLIMIT 10;
Syntax Pattern
SELECT <columns> FROM <table> [WHERE <predicate>] [GROUP BY <cols>] [HAVING <agg-predicate>] [ORDER BY <cols> [ASC|DESC]] [LIMIT <n> [OFFSET <m>]];
SELECTColumns or expressions to return (the projection).FROMSource table(s) — add JOINs here.WHERERow filter applied before grouping.GROUP BYBucket rows for aggregation.ORDER BYSort the final result.LIMITCap the number of rows returned.
Live Example
sql
SELECT email, planFROM usersWHERE plan = 'pro'ORDER BY created_at DESCLIMIT 3;
users
| id | plan | created_at | |
|---|---|---|---|
| 1 | leo@acme.io | free | 2026-04-10 |
| 2 | mia@acme.io | pro | 2026-05-01 |
| 3 | ana@acme.io | pro | 2026-05-12 |
| 4 | rio@acme.io | pro | 2026-05-18 |
runs
result
| plan | |
|---|---|
| rio@acme.io | pro |
| ana@acme.io | pro |
| mia@acme.io | pro |
SELECT projection
reads → filters → projects → orders
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.
Real-world use
Every dashboard, report, leaderboard, search page and analytics widget is a SELECT. Twitter's feed, Stripe's reports, Notion's search — all SELECTs over carefully designed tables and indexes.
Best practices
- Always specify columns — never
SELECT *in production. - Always have a
WHEREon big tables to use indexes. - Use
EXPLAINto verify the plan is good.
Common mistakes
SELECT *over a 200-column table → wasteful I/O.- Forgetting LIMIT on huge tables → app OOM.
- ORDER BY without an index on the order column → slow sorts.
Hands-on exercise
Interview preparation — practice these questions:
- Q1. Logical execution order of a SELECT.
- Q2. Why avoid SELECT * in production?
- Q3. Difference between WHERE and HAVING.
- Q4. What does DISTINCT do?
- Q5. How to limit rows in Postgres vs SQL Server?
Ready to mark this lesson complete?Track your journey across the entire course.