๐Ÿ—ƒ๏ธ Introduction to SQL

๐Ÿง  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.