Module 1, Session 2. Physical table anatomy, strict data types, primary & foreign keys, database hierarchy, declarative thinking and the 5 SQL command families.
Open in Present mode · Live class schedule · Try the playground
Jo class mein bola, yahan nukte mein likha hai.
What I said in class, written here as key points.
Session 01 established the mental models (Data vs Info, DBMS, SQL vs NoSQL, 80/20 Rule). Today we crack open the engine: Table Anatomy, Strict Typing, Keys, Hierarchy & The 5 SQL Languages.
Mastering physical table anatomy, relational integrity, system hierarchies, and declarative execution.
How your contacts, WhatsApp, and call logs run on embedded SQL.
Strict data typing, entity rows, and schema rejection gates.
Primary keys for uniqueness, foreign keys for table glue.
Server → Database → Schema (16) → Table (55) → Row → Cell.
Why 24-lakh rows crash spreadsheets, but run in 0.3s in PostgreSQL.
DDL (build), DML (modify), DQL (read 90%), DCL (secure), TCL (save).
Tell the chef WHAT you want (SQL), not step-by-step HOW (Python).
Parser → Analyzer → Planner (Optimizer) → Executor pipeline.
Sneha's real analyst day at Flipkart + 8 top tech interview drills.
.txt file without columns. Finding "Rahul" requires scanning every single character sequentially. Your phone lags, burns CPU cycles, and drains battery on every search.
name column. The engine navigates directly to matching rows in 0.08 milliseconds with near-zero CPU overhead.
| id | first_name | salary (NUMERIC) | is_active |
|---|---|---|---|
| 101 | Rahul | ₹85,000 | true |
| 102 | Sneha | ₹92,000 | true |
| 103 | Amit | ₹64,000 | false |
TEXT, NUMERIC, BOOLEAN, DATE). You cannot mix types within a column.| id | first_name | salary | is_active |
|---|---|---|---|
| 101 | Rahul | ₹85,000 | true |
| 102 | Sneha | ₹92,000 | true |
| 103 | Amit | ₹64,000 | false |
| customer_id PK | first_name | last_name | |
|---|---|---|---|
| 101 | Rahul | Sharma | [email protected] |
| 102 | Priya | Patel | [email protected] 0 orders |
| 103 | Amit | Kumar | [email protected] |
| order_id PK | cust_id FK → cust | store_id | order_date | net_total |
|---|---|---|---|---|
| 5001 | 101 | 47 | 2025-03-15 | ₹2,499.00 |
| 5002 | 103 | 12 | 2025-03-16 | ₹899.00 |
| 5003 | 101 | 47 | 2025-04-01 | ₹5,999.00 |
5001 & 5003). One customer row links to infinite order rows.
cust_id = 999 cannot exist if no customer 999 exists in customers. PostgreSQL blocks ghost orders at the engine level.
Every schema, table, column definition, foreign key link, and sample row is browseable on this website. Use it whenever you wonder “which table has this data?”
| Feature | Excel / Sheets | PostgreSQL Engine |
|---|---|---|
| Max Rows | 10,48,576 (hard limit) | Crores of rows (no practical limit) |
| Concurrency | Conflicts, file overwriting | 1,000s of concurrent users |
| Data Integrity | Anyone can type anything | Enforced types & constraints |
| Relationships | VLOOKUP (fragile, breaks on sort) | Foreign Keys (guaranteed link) |
| Speed on 10 Lakh Rows | Freezes / crashes 💀 | < 1 second ⚡ |
| Undo a Mistake | Ctrl+Z (if you are lucky) | ROLLBACK (guaranteed safe) |
| Audit Trail | None | Full WAL change logging |
| Security | File-level password | Role-based per-table permissions |
| Automation | Macros (limited, fragile) | Triggers, functions, schedules |
| Cost | ₹5,000/yr (Office 365) | ₹0 (Open Source, Forever) 🏆 |
You learned columns, rows, schemas, primary keys, and foreign keys. Now work through the 4 challenge portals below: Easy, Medium, Hard, and Crazy.
SELECT, FROM, WHERE), and set-theory rules.SELECT). You do not pour concrete foundations every day (DDL), but you search, aggregate, and inspect business metrics every second.
SELECT * FROM sales.orders WHERE net_total > 10000;. You do NOT tell PostgreSQL which disk block to read, whether to use a B-Tree index, or how to allocate RAM. The Query Planner calculates lakhs of combinations and picks the fastest physical path in milliseconds.
# Open file & manual loop row-by-row
customers = open('customers.csv')
results = []
for line in customers:
fields = line.split(',')
if fields[5] == 'Mumbai':
results.append(fields)
for r in results: print(r)
-- Just declare what you need. Zero loops. SELECT * FROM customers.addresses WHERE city = 'Mumbai';
You learned DDL/DML/DQL/DCL/TCL, declarative thinking, and the 4-stage query pipeline. Now work through the 4 challenge portals below: Easy, Medium, Hard, and Crazy.
SELECT s.seller_id, s.seller_name, COUNT(o.order_id) AS total_orders, ROUND(AVG(CASE WHEN o.status = 'Returned' THEN 1.0 ELSE 0.0 END) * 100, 1) AS return_rate_pct FROM sellers s JOIN orders o ON s.seller_id = o.seller_id WHERE s.category = 'Electronics' GROUP BY s.seller_id, s.seller_name HAVING COUNT(o.order_id) >= 50 AND AVG(CASE WHEN o.status = 'Returned' THEN 1.0 ELSE 0.0 END) > 0.50 ORDER BY return_rate_pct DESC LIMIT 10;
read_sql() and Power BI uses DirectQuery. SQL extracts clean tables before other tools take over.
| Task | SQL (Native Engine) | Python (Pandas / Stats) |
|---|---|---|
| Filter 10 Lakh Rows by Date | 50ms (native index scan) | 3s (loads full array into pandas) |
| JOIN 3 Tables on Keys | Optimized by Planner algorithms | Manual pd.merge() • High RAM |
| GROUP BY + Aggregate | Clean one-liner • In-engine | df.groupby() in memory |
| Build an ML Model | Not possible • Not designed for it | scikit-learn, PyTorch, XGBoost |
| Interactive Web App | Not possible | Streamlit, Dash, FastAPI |
| Visualize Charts | Not possible | Matplotlib, Seaborn, Plotly |
You learned what Data Analysts, Data Engineers, and Data Scientists do daily with SQL. Now work through the 4 challenge portals below: Easy, Medium, Hard, and Crazy.
sales, inventory). It prevents table name collisions and enables domain-based security permissions.
BEGIN ... COMMIT ensures atomicity. If a crash occurs, ROLLBACK restores previous balances completely.
The 6 foundational principles governing database hierarchy, integrity, command taxonomy, and the execution engine.
-- DDL: Structure (Schemas/Tables) CREATE, ALTER, DROP, TRUNCATE -- DML: Rows (Data Manipulation) INSERT, UPDATE, DELETE -- DQL: Query (90% of Analyst Time) SELECT col1, col2 FROM table; -- DCL: Security & Permissions GRANT, REVOKE -- TCL: Transaction Integrity (ACID) COMMIT, ROLLBACK, SAVEPOINT
-- Universal 6-Level Hierarchy: Server Cluster (Port 5432) └── Database (RetailMart) └── Schema (Folder Namespace) └── Table (customers, orders) ├── Column (Type + Rule) └── Row (Atomic Entity) └── Cell (Scalar) -- Production Scope: RetailMart: 16 schemas, 55 tables, ~24L rows.
-- Catalog Navigation: \l -- List all databases \c retailmart -- Connect to RetailMart \dn -- List all 16 schemas \dt sales.* -- List all tables in schema \d sales.orders -- Describe columns & keys \q -- Quit psql shell
-- Query Execution Flow: 1. Parser: Validates SQL syntax tree 2. Analyzer: Verifies tables, columns, types 3. Planner: Evaluates cost (Seq vs Index) 4. Executor: Fetches data pages from disk -- The Declarative Rule: You declare WHAT. Planner decides HOW.
Connect to RetailMart on your local machine using psql -U postgres -d retailmart (or using the online playground) and perform system introspection:
\l to view all databases, then switch into retailmart with \c retailmart.\dn to catalog all 16 schemas. Confirm that sales, customers, and products exist.\dt sales.* to inspect all tables inside the sales namespace. How many tables are listed?\d sales.orders. Identify the Primary Key constraint and every Foreign Key constraint.customers.customers and sales.orders. Label the PK and FK clearly.CREATE TABLE, INSERT INTO, SELECT, REVOKE, and ROLLBACK into the 5 SQL families.SELECT * FROM orders; fails if you are connected to the public schema instead of sales.