๐ง What is SQL?
SQL (Structured Query Language) is the standard language used to communicate with relational databases. Whether you’re storing user info, app data, logs, or business recordsโSQL is how you query, modify, and manage that data.
๐ค Basic SQL Syntax
SQL statements are written in all caps by convention, but it’s not required.
SELECT column_name FROM table_name;
๐ Creating and Managing Tables
1. Creating a Table
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER,
email TEXT
);
2. Inserting Data
INSERT INTO users (name, age, email)
VALUES ('Doby', 25, 'doby@example.com');
3. Selecting Data
SELECT * FROM users;
SELECT name, age FROM users WHERE age > 20;
๐ Conditions and Filtering
SELECT * FROM users
WHERE age >= 18 AND name = 'Doby';
SELECT * FROM users
WHERE email LIKE '%@example.com';
๐ฏ Advanced SELECT Features
1. Ordering Results
SELECT * FROM users
ORDER BY age DESC;
2. Limiting Results
SELECT * FROM users
LIMIT 5;
3. Aliasing Columns
SELECT name AS username, age FROM users;
๐ Updating and Deleting Data
1. Update Rows
UPDATE users
SET age = 26
WHERE name = 'Doby';
2. Delete Rows
DELETE FROM users
WHERE id = 3;
๐ Working with Relationships
1. Foreign Keys
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
user_id INTEGER,
amount REAL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
2. JOIN Statements
SELECT users.name, orders.amount
FROM users
JOIN orders ON users.id = orders.user_id;
๐ฆ Aggregates and Grouping
SELECT COUNT(*) FROM users;
SELECT AVG(age) FROM users;
SELECT MIN(age), MAX(age) FROM users;
SELECT age, COUNT(*) FROM users
GROUP BY age
HAVING COUNT(*) > 1;
๐งช Case Insensitivity and Functions
Case-insensitive search (SQLite/PostgreSQL/MySQL):
SELECT * FROM users
WHERE LOWER(name) = 'doby';
Useful string/date functions:
SELECT UPPER(name), LENGTH(email) FROM users;
SELECT CURRENT_DATE;
๐พ Backups and Exports (SQLite)
Export to a file (SQLite CLI):
sqlite3 mydb.db .dump > backup.sql
๐ Security Tips
- Always sanitize user input to avoid SQL injection.
- Use prepared statements when working from code.
- Limit privileges on production databases.
๐ Learning Resources
๐ก Final Thoughts
SQL is the heart of data-driven applications. Mastering it means being able to build smarter apps, analyze more deeply, and create systems that remember. Itโs like giving structure to memory.
Start small. Query kindly. And rememberโyouโre building the language of digital memory.