066
LVL 03 — MID DEVELOPERSESSION 066DAY 66

SQL & POSTGRESQL

🎫 PIXELCRAFT-053
📚Learning / Feature | 🟠 Hard | Priority: 🟠 High

MongoDB works for flexible image metadata, but our analytics team needs structured reporting: "top 10 users by upload count," "average image size by format," "daily active users." These queries need SQL.
CONCEPTS.UNLOCKED
📊
Relational Databases
Data in tables (rows and columns) with relationships. Strict structure, powerful queries, guaranteed consistency. The foundation of most production systems.
📝
SQL
The universal query language: SELECT, INSERT, UPDATE, DELETE. Declarative — describe WHAT you want, not HOW to get it. The database figures out the optimal execution.
📐
Tables, Columns, Rows
Tables = collections. Columns = fields with types (INTEGER, TEXT, BOOLEAN, TIMESTAMP). Rows = documents. Primary keys = unique identifiers.
🔍
WHERE, ORDER BY, GROUP BY
WHERE filters rows. ORDER BY sorts results. GROUP BY aggregates (COUNT, SUM, AVG). LIMIT caps results. Compose them for powerful queries.
🔗
JOINs
Connecting related tables — the superpower of relational databases. JOIN users to events: "top 10 users by upload count" in one query, no loops, no code.
🐘
PostgreSQL
The most popular open-source relational database. Used by Instagram, Spotify, Reddit. Rock-solid reliability, advanced features, massive ecosystem.
HANDS-ON.TASKS
01
Set Up PostgreSQL
# If using Docker: docker run --name pixelcraft-postgres \ -e POSTGRES_PASSWORD=secret \ -p 5432:5432 -d postgres
02
Create Tables
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, created_at TIMESTAMP DEFAULT NOW() ); CREATE TABLE analytics_events ( id SERIAL PRIMARY KEY, user_id INTEGER REFERENCES users(id), event_type VARCHAR(50) NOT NULL, -- 'upload', 'filter_apply', -- 'export', 'login' metadata JSONB, created_at TIMESTAMP DEFAULT NOW() );
03
Insert and Query Data
-- Insert users INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'); INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); -- Insert events INSERT INTO analytics_events (user_id, event_type, metadata) VALUES (1, 'upload', '{"format": "jpeg", "size_mb": 4.2}'); -- Basic queries SELECT * FROM users WHERE name LIKE 'A%'; SELECT COUNT(*) FROM analytics_events WHERE event_type = 'upload'; SELECT event_type, COUNT(*) as count FROM analytics_events GROUP BY event_type ORDER BY count DESC;
04
JOINs — The Power of Relational Databases
-- Top 10 users by upload count SELECT u.name, COUNT(e.id) as upload_count FROM users u JOIN analytics_events e ON u.id = e.user_id WHERE e.event_type = 'upload' GROUP BY u.name ORDER BY upload_count DESC LIMIT 10;
One query. No loops. No code. Just a declarative question: "who are our top uploaders?" — and the database answers.
05
Compare MongoDB vs PostgreSQL
CriteriaMongoDBPostgreSQL
SchemaFlexible documentsStrict tables
Best forRapid prototyping, varied dataComplex queries, reporting
RelationshipsEmbedding / referencingJOINs (native)
PixelCraft useImage metadataAnalytics events
06
Close the Ticket
git switch -c feature/PIXELCRAFT-053-postgres-analytics git add server/ git commit -m "Add PostgreSQL analytics with SQL queries (PIXELCRAFT-053)" git push origin feature/PIXELCRAFT-053-postgres-analytics # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Relational algebra.

SQL is the practical implementation of a mathematical theory by Edgar Codd (1970).

// SQL maps to math:

SELECT   → projection
WHERE    → selection
JOIN     → Cartesian product
           with condition

// The relational model guarantees that
// any question about your data can be
// expressed as a query.

// This mathematical foundation is why
// SQL has dominated for 50+ years.
"SQL Lab"
[A]Write 5 more analytics queries: average image size by format, most active hour of the day, users who haven't logged in for 7 days, export count per month, filter usage ranking.
[B]Practice SQL interactively at SQLBolt.com or mode.com/sql-tutorial. Complete at least 10 exercises covering SELECT, WHERE, JOIN, GROUP BY, and subqueries.
[C]Research: can PostgreSQL handle PixelCraft's image metadata too? Add a JSONB column and query into it. Compare with MongoDB for the same data and queries.
REF.MATERIAL
ARTICLE
PostgreSQL Team
Official tutorial: creating databases, tables, queries, joins, and the fundamentals of PostgreSQL.
POSTGRESQLOFFICIALESSENTIAL
VIDEO
Fireship
Ultra-fast PostgreSQL overview: tables, SQL, ACID, JSONB, and why it powers the world's largest applications.
POSTGRESQLQUICK
ARTICLE
SQLBolt
Free interactive SQL tutorial: SELECT, WHERE, JOIN, GROUP BY, subqueries, and more. Practice directly in the browser.
SQLINTERACTIVEPRACTICE
ARTICLE
Wikipedia
Edgar Codd's 1970 paper, relational algebra, set theory foundations, and how it shaped 50 years of database design.
RELATIONALTHEORYCS
VIDEO
Fireship
SQL overview: CRUD operations, joins, aggregation, and why SQL is the most important language after your primary programming language.
SQLQUICK
// LEAVE EXCITED BECAUSE
SQL is incredibly powerful. One query answers "who are our top users?" — no loops, no code, just a declarative question. You now understand BOTH MongoDB and PostgreSQL — you can choose the right tool for the job.