1.1 What Is SQL, Tables & How Databases Think
Start from absolute zero: understand how relational databases store data on disk, SQL vs NoSQL differences, PostgreSQL vs MySQL vs SQLite, and the #1 secret of SQL query execution order.
1. Why Do Databases Exist? (Real-World Intuition)
Imagine you are building an e-commerce app like Flipkart or Amazon. At first, you might think: "Why not just save all users and orders in a CSV or Excel file?"
Here is what breaks the moment you get real traffic:
- Concurrency crashes: If 5,000 customers buy the last iPhone at the exact same millisecond, a plain file gets overwritten and corrupted.
- Speed collapses: Searching for one order inside a 50-million-row CSV requires reading the entire file from top to bottom (taking minutes!).
- Crash safety: If the server loses power mid-write, a plain file is left half-written.
A Relational Database Management System (RDBMS) solves all three:
- Stores billions of rows durably on disk with crash-recovery logs (WAL).
- Handles thousands of concurrent reads & writes safely using transactions.
- Finds 1 exact row out of 100 million in ~2 milliseconds using B+ Tree Indexes.
SQL (Structured Query Language) is the universal, declarative language you use to talk to any RDBMS. "Declarative" means you tell the database WHAT result you want — not how to loop through rows in memory. The database's Query Optimizer figures out the fastest execution plan automatically!
2. Visual Anatomy of a Database Table
Every relational database organizes information into Tables (also called relations):
| Database Term | Formal Relational Term | Everyday Equivalent | Strict Rule |
|---|---|---|---|
| Database | Schema / Catalog | An entire Excel workbook | Contains multiple related tables (users, orders, payments) |
| Table | Relation | One worksheet tab (students) | Stores one specific kind of entity |
| Column | Attribute / Field | A column header (score) | Every value in that column must match its Data Type |
| Row | Tuple / Record | One horizontal entry | Represents one concrete item (e.g., student Meera) |
| Primary Key (PK) | Unique Identifier | Passport / Roll Number | Strictly UNIQUE and NOT NULL — never changes |
3. Key Differences Every Beginner & Interviewee Must Know
A. SQL (Relational) vs NoSQL (Document / Key-Value)
| Dimension | SQL (PostgreSQL, MySQL, Oracle) | NoSQL (MongoDB, Redis, DynamoDB) |
|---|---|---|
| Data Model | Structured tables with rows & columns | Flexible JSON documents, key-value pairs, or graphs |
| Schema | Strict schema enforced before writing | Schema-less / dynamic per document |
Relationships (JOINs) | First-class, fast, normalized via Foreign Keys | Denormalized (data often duplicated inside documents) |
| Transactions (ACID) | Rock-solid multi-table ACID guarantees | Traditionally eventual consistency (limited multi-doc ACID) |
| Best Used For | Banking, e-commerce orders, SaaS, analytics | Caching (Redis), chat logs, CMS catalogs, IoT telemetry |
B. PostgreSQL vs MySQL vs SQLite (Which One Should You Learn?)
The great news: 90% of SQL syntax is identical across all of them! Here are the practical differences:
| Database | Best Known For | Key Technical Difference |
|---|---|---|
| PostgreSQL | Modern startups, fintech, analytics, AI (pgvector) | Strictest SQL standards compliance, rich types (JSONB, arrays), ILIKE, FULL OUTER JOIN |
| MySQL | Web apps, WordPress, legacy & high-read backends | Very fast simple reads, uses AUTO_INCREMENT and backticks `col`, no native FULL OUTER JOIN |
| SQLite | Mobile apps (iOS/Android), local dev, embedded | Single .db file on disk — zero server setup required! |
4. The #1 Secret of SQL: Written Order vs Execution Order
Beginners often get confused by SQL error messages like "column bonus_score does not exist in WHERE".
The reason is simple: the order you WRITE a SQL query is NOT the order the database EXECUTES it!
| Written Order (How You Type It) | Logical Execution Order (How DB Runs It) | What Happens at That Step |
|---|---|---|
1. SELECT | 5th | Picks/computes columns & creates AS aliases |
2. FROM / JOIN | 1st | Loads base table(s) and combines joined tables |
3. WHERE | 2nd | Filters individual rows (cannot see SELECT aliases!) |
4. GROUP BY | 3rd | Groups surviving rows into buckets |
5. HAVING | 4th | Filters entire groups (e.g., HAVING COUNT(*) > 5) |
6. ORDER BY | 6th | Sorts final rows (CAN use SELECT aliases!) |
7. LIMIT / OFFSET | 7th | Slices the top N rows for pagination |
Watch the Database Engine Execute a Query Step-by-Step
Click Next below to see the exact intermediate table inside the database engine at every step:
| id | name | course | score |
|---|---|---|---|
| 101 | Aarav | Python | 92 |
| 102 | Meera | SQL | 96 |
| 103 | Kabir | DSA | 88 |
| 104 | Riya | SQL | 91 |
5. The 5 Families of SQL Commands (Interview Reference)
| Family | Full Name | Core Commands | Real-World Purpose |
|---|---|---|---|
| DQL | Data Query Language | SELECT | Reading, filtering, joining, and analyzing data |
| DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE | Defining or changing table structure (auto-commits in MySQL!) |
| DML | Data Manipulation Language | INSERT, UPDATE, DELETE | Adding, modifying, or removing rows |
| TCL | Transaction Control Language | BEGIN, COMMIT, ROLLBACK, SAVEPOINT | Making multi-step writes atomic (all-or-nothing) |
| DCL | Data Control Language | GRANT, REVOKE | Controlling user permissions (read-only analyst vs admin) |
-- Clean, industry-standard SQL formatting:
-- 1. UPPERCASE keywords (SELECT, FROM, WHERE)
-- 2. snake_case table & column names (order_items, created_at)
-- 3. Always end statements with a semicolon (;)
SELECT
name,
course,
score
FROM students
WHERE score >= 90
ORDER BY score DESC;
Beginner Watch-Out: String literals in standard SQL MUST use single quotes (
'SQL'), never double quotes ("SQL"). In PostgreSQL and standard SQL, double quotes refer to column/table identifiers, not text values!
6. Mini Practice Challenge
Try solving this before peeking at the answer:
Why does the query below fail with an error, and how do you fix it?
SELECT name, salary * 12 AS annual_pay
FROM employees
WHERE annual_pay > 1200000;
7. Quick Check Your Understanding
Module 1.1 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Convert Unix epoch seconds/milliseconds to ISO-8601 and local timezones, and bulk-generate UUID v4, time-ordered UUID v7, and sortable ULIDs.
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.