What Is SQL and Why Learn It?
SQL (Structured Query Language) is the standard language for talking to relational databases like SQL Server, MySQL, PostgreSQL, MariaDB, and Oracle. If your job touches data—IT support, sysadmin, developer, analyst—SQL lets you create databases, insert data, and query answers fast.
Core Concepts (10 mins)
- Database: container of tables, views, procedures.
- Table: rows (records) × columns (fields) with data types.
- Primary Key (PK): unique identifier for a row, e.g.,
id. - Foreign Key (FK): column referencing a PK in another table to build relationships.
- Index: structure that speeds lookups (like a book index).
- Normalization: designing tables to reduce duplication (1NF, 2NF, 3NF).
- CRUD: Create, Read, Update, Delete operations.
1) Create a Database & Table
-- Create a database (SQL Server / MySQL)
CREATE DATABASE shopdb;
-- Switch to it (SQL Server)
USE shopdb;
-- Create a Products table
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Category VARCHAR(50),
Price DECIMAL(10,2) NOT NULL,
InStock INT DEFAULT 0,
CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
2) Insert Data
INSERT INTO Products (ProductID, Name, Category, Price, InStock)
VALUES
(1,'Logitech MX Master 3S','Accessories',99.99,15),
(2,'Dell UltraSharp 27"','Monitor',349.00,8),
(3,'Samsung 980 1TB','Storage',79.50,25),
(4,'Surface Laptop 5','Laptop',1299.00,3);
3) Basic Read — SELECT
-- All rows & columns
SELECT * FROM Products;
-- Choose columns + filter + sort
SELECT Name, Category, Price
FROM Products
WHERE Price < 200
ORDER BY Price ASC;
4) Filtering: WHERE Operators
- Comparison:
=, <>, >, >=, <, <= - Pattern:
LIKE 'Sam%',NOT LIKE '%Laptop%' - Set:
IN ('Laptop','Storage'),NOT IN (...) - NULL checks:
IS NULL,IS NOT NULL - Logical:
AND,OR,NOT
-- Example: affordable items in stock
SELECT Name, Price, InStock
FROM Products
WHERE Price BETWEEN 50 AND 200
AND InStock > 0;
5) Aggregate & Grouping
-- Count products per category
SELECT Category, COUNT(*) AS ItemCount, AVG(Price) AS AvgPrice
FROM Products
GROUP BY Category
HAVING COUNT(*) >= 1
ORDER BY AvgPrice DESC;
6) JOINs — Combine Tables
Let’s add an Orders table and link with Products by ProductID.
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
ProductID INT NOT NULL,
Qty INT NOT NULL,
OrderDate DATE NOT NULL,
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
INSERT INTO Orders (OrderID, ProductID, Qty, OrderDate) VALUES
(101,1,2,'2025-10-02'),
(102,2,1,'2025-10-03'),
(103,3,5,'2025-10-03'),
(104,1,1,'2025-10-05');
-- Join to see order details with product names & price
SELECT o.OrderID, p.Name, o.Qty, p.Price, (o.Qty * p.Price) AS LineTotal
FROM Orders o
JOIN Products p ON p.ProductID = o.ProductID
ORDER BY o.OrderDate DESC;
7) Update & Delete
-- Update stock after an order fulfillment
UPDATE Products
SET InStock = InStock - 1
WHERE ProductID = 2;
-- Delete a product (careful!)
DELETE FROM Products
WHERE ProductID = 4;
8) Real-World Patterns
Top N (e.g., Top 3 most expensive)
-- SQL Server / PostgreSQL (with LIMIT variation)
SELECT Name, Price
FROM Products
ORDER BY Price DESC
OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY;
-- MySQL / MariaDB / Postgres:
-- SELECT Name, Price FROM Products ORDER BY Price DESC LIMIT 3;
Paged Results (for UIs)
-- Page 2, size 10 (SQL Server style)
SELECT *
FROM Products
ORDER BY ProductID
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
Search by Keyword
-- Case-insensitive contains search (depends on collation)
SELECT *
FROM Products
WHERE Name LIKE '%lap%';
9) Schema Tips for Beginners
- Use proper data types: money →
DECIMAL(10,2), dates →DATE/DATETIME. - Primary keys: use
INT(identity/auto-increment) orUUID. - Foreign keys: actually enforce them—data stays consistent.
- Indexes: add on columns used in frequent
WHERE/JOIN/ORDER BY. - Name clearly:
Products,Orders,OrderItems—avoid cryptic names.
10) Performance Quick Wins
- Return only what you need (columns + rows).
- Use proper indexes; avoid
SELECT *in production. - Prefer set-based operations over loops/cursors.
- Check query plans to spot scans vs. seeks.
- Archive old data; keep hot tables slim.
Cheat Sheet (Copy-Paste)
-- CRUD
INSERT INTO T (c1,c2) VALUES (v1,v2);
SELECT c1,c2 FROM T WHERE ... ORDER BY ...;
UPDATE T SET c1=v WHERE ...;
DELETE FROM T WHERE ...;
-- Aggregate
SELECT k, COUNT(*), SUM(x), AVG(x), MIN(x), MAX(x)
FROM T
GROUP BY k
HAVING COUNT(*) > 0;
-- Join
SELECT a.*, b.*
FROM A a
JOIN B b ON b.key = a.key;
Final Thoughts
SQL is a career-long skill. Learn the patterns, practice daily, and keep building small datasets to answer real questions. Within weeks, you’ll feel the power of databases in your hands.