SQL (Structured Query Language) is the backbone of every modern application. From startups to Fortune 500 companies, every app that stores data uses SQL. Whether you're a developer, data analyst, or aspiring data engineer — SQL is a must-have skill.
🗄️ What is SQL?
SQL is a standard language for managing and manipulating relational databases. You use it to:
- Create databases and tables
- Read data with powerful queries
- Update existing records
- Delete data you no longer need
🚀 Setting Up Your Database
-- Create a database
CREATE DATABASE shop_db;
-- Use the database
\c shop_db -- PostgreSQL
USE shop_db; -- MySQL
-- Create a table
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
category VARCHAR(50),
stock INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
📝 CRUD Operations
-- INSERT — Add new records
INSERT INTO products (name, price, category, stock)
VALUES ('Laptop', 999.99, 'Electronics', 50),
('Headphones', 79.99, 'Electronics', 200),
('Notebook', 4.99, 'Stationery', 1000);
-- SELECT — Read data
SELECT * FROM products;
SELECT name, price FROM products WHERE category = 'Electronics';
-- UPDATE — Modify existing records
UPDATE products SET price = 899.99 WHERE name = 'Laptop';
-- DELETE — Remove records
DELETE FROM products WHERE stock = 0;
🔍 Filtering & Sorting
-- WHERE with multiple conditions
SELECT * FROM products
WHERE price > 10 AND category = 'Electronics'
ORDER BY price DESC;
-- LIKE — Pattern matching
SELECT * FROM products WHERE name LIKE '%phone%';
-- IN — Match multiple values
SELECT * FROM products WHERE category IN ('Electronics', 'Accessories');
-- BETWEEN — Range queries
SELECT * FROM products WHERE price BETWEEN 50 AND 500;
-- LIMIT & OFFSET — Pagination
SELECT * FROM products ORDER BY created_at DESC LIMIT 10 OFFSET 20;
🔗 JOINs — Combining Tables
-- Create related tables
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE
);
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
customer_id INT REFERENCES customers(id),
product_id INT REFERENCES products(id),
quantity INT,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- INNER JOIN
SELECT c.name, p.name AS product, o.quantity
FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
INNER JOIN products p ON o.product_id = p.id;
-- LEFT JOIN — include customers with no orders
SELECT c.name, COUNT(o.id) AS total_orders
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
GROUP BY c.name;
📊 Aggregation & GROUP BY
-- Count, Sum, Average
SELECT category,
COUNT(*) AS total_products,
AVG(price) AS avg_price,
SUM(stock) AS total_stock
FROM products
GROUP BY category
HAVING AVG(price) > 50
ORDER BY total_products DESC;
⚡ Indexing for Performance
-- Create an index for faster lookups
CREATE INDEX idx_products_category ON products(category);
CREATE INDEX idx_orders_customer ON orders(customer_id);
-- Composite index
CREATE INDEX idx_products_cat_price ON products(category, price);
💡 Pro Tip: Always index columns used in WHERE, JOIN, and ORDER BY clauses. A missing index can make queries 100x slower on large datasets.
📚 SQL Learning Path
| Level | Topics | Time |
|---|---|---|
| Beginner | SELECT, WHERE, INSERT, UPDATE, DELETE | 1-2 weeks |
| Intermediate | JOINs, GROUP BY, Subqueries, Views | 2-3 weeks |
| Advanced | Window Functions, CTEs, Indexing, Transactions | 3-4 weeks |
| Expert | Query Optimization, Stored Procedures, Replication | Ongoing |