A complete, developer-focused cheat sheet that covers SQL, MySQL, PostgreSQL, MongoDB (NoSQL), and Django ORM — with commands, examples, architecture diagrams, use cases, pros/cons, and real-life references.
- Introduction
- Overview
- ERD Diagram
- Architecture
- SQL Cheat Sheet
- MySQL Cheat Sheet
- PostgreSQL Cheat Sheet
- NoSQL Cheat Sheet
- Django ORM Cheat Sheet
- Use Cases
- Pros & Cons
- Which Database Should You Use?
- Real-Life Examples
Databases power everything — from banking systems to social media and large AI pipelines. This repository gives you a single place to reference:
✔ SQL commands ✔ MySQL & PostgreSQL features ✔ NoSQL (MongoDB) commands ✔ Django ORM Python examples ✔ ERDs, diagrams, and best practices ✔ Real-life usage patterns
Structured Query Language used for relational databases.
Fast, popular, open-source SQL database used widely in web applications.
Advanced SQL database known for reliability, performance, and extensions.
Non-relational databases used for flexibility, scalability & unstructured data.
Queries written in Python instead of SQL.
A simple Entity Relationship Diagram used in many examples:
erDiagram
USER ||--o{ ORDER : places
ORDER ||--|{ ORDERITEM : contains
PRODUCT ||--o{ ORDERITEM : listed
USER {
int id
string name
string email
}
ORDER {
int id
int user_id
date created_at
}
ORDERITEM {
int id
int order_id
int product_id
int qty
}
PRODUCT {
int id
string name
float price
}
flowchart TD
A[Client] --> B[Backend API]
B --> C[(Database Layer)]
C --> C1[SQL DB: MySQL/PostgreSQL]
C --> C2[NoSQL: MongoDB]
B --> D[Django ORM Abstraction]
D --> C
C1 --> E[Tables, Joins, ACID]
C2 --> F[Collections, Documents]
Basic SQL syntax applies to all relational databases.
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE
);INSERT INTO users (id, name, email)
VALUES (1, 'Alice', 'alice@example.com');SELECT name, email FROM users WHERE id = 1;UPDATE users SET name = 'Alice Updated' WHERE id = 1;DELETE FROM users WHERE id = 1;SELECT orders.id, users.name
FROM orders
JOIN users ON orders.user_id = users.id;SHOW DATABASES;CREATE TABLE items (
id INT AUTO_INCREMENT PRIMARY KEY,
item_name VARCHAR(100)
);SELECT * FROM users LIMIT 10;mysqldump -u root -p dbname > backup.sqlpsql -U postgresCREATE DATABASE company;SELECT row_to_json(users) FROM users;SELECT * FROM users WHERE name ILIKE '%john%';ALTER TABLE users ADD COLUMN tags TEXT[];db.users.insertOne({ name: "Alice", age: 22 })db.users.find({ age: { $gt: 20 } })db.users.updateOne({ name: "Alice" }, { $set: { age: 23 } })db.users.deleteOne({ name: "Alice" })db.orders.aggregate([
{ $match: { status: "completed" }},
{ $group: { _id: "$user_id", total: { $sum: "$amount" }}}
])Below is the most complete Django ORM cheat sheet for CRUD, joins, filters, annotations, raw queries, and aggregations.
user = User.objects.create(name="Alice", email="alice@example.com")User.objects.get(id=1)User.objects.filter(age__gte=18)User.objects.exclude(status="inactive")User.objects.order_by('-created_at')User.objects.filter(id=1).update(name="New Name")User.objects.filter(id=1).delete()orders = Order.objects.select_related("user").all()products = Product.objects.prefetch_related("categories")from django.db.models import Sum, Count
Order.objects.aggregate(total_amount=Sum("amount"))
User.objects.annotate(order_count=Count("order"))from django.db.models import F
Order.objects.annotate(
total_price = F("qty") * F("product__price")
)from django.db.models import Q
User.objects.filter(
Q(name__icontains="a") |
Q(email__icontains="gmail")
)User.objects.raw("SELECT * FROM users WHERE age > 25")from django.db.models import Subquery, OuterRef
latest_order = Order.objects.filter(
user_id=OuterRef('id')
).order_by('-created_at')
User.objects.annotate(
last_order_id=Subquery(latest_order.values('id')[:1])
)User.objects.bulk_create([
User(name="A"),
User(name="B")
])| Database | Perfect For |
|---|---|
| SQL / MySQL | Small–medium web apps, WordPress, CRMs |
| PostgreSQL | Financial systems, analytics, big queries |
| MongoDB (NoSQL) | Unstructured data, JSON, large-scale apps |
| Django ORM | Python/Django web apps |
✔ Structured ✔ ACID compliant ✔ Strong relationships ❌ Harder to scale horizontally
✔ Fast ✔ Great for web apps ❌ Fewer advanced features than Postgres
✔ Strongest SQL engine ✔ JSON + SQL hybrid ❌ Slightly slower for simple queries
✔ Highly scalable ✔ Schema-free ❌ No joins, weaker consistency
✔ Easy ✔ Safe ❌ Slower than raw SQL
| Project Type | Best Choice |
|---|---|
| Social Media App | PostgreSQL + Redis |
| Real-time Chat | MongoDB |
| Banking System | PostgreSQL |
| Small Business Website | MySQL |
| Python Web App | Django ORM + PostgreSQL |
| IoT Data Ingestion | NoSQL |
INSERT INTO users (name, email) VALUES ('Mark', 'mark@corp.com');db.sensors.insertOne({
temperature: 34.5,
created_at: new Date()
})orders = Order.objects.filter(status="completed").select_related("user")SELECT info->>'email' FROM customers WHERE id = 1;