🗄 SQL Basics: A Beginner’s Guide to Structured Query
Language
📌 What is SQL?
SQL (Structured Query Language) is the standard language used to store, retrieve, and
manage data in a relational database. Whether you’re building a small blog or a complex
enterprise application, SQL is an essential tool for working with data efficiently.
📚 Common SQL Databases
• MySQL – Open-source, widely used in web applications
• PostgreSQL – Open-source, known for robustness and advanced features
• SQLite – Lightweight, file-based database, great for mobile and embedded systems
• Microsoft SQL Server – Enterprise-level, developed by Microsoft
• Oracle Database – Powerful, widely used in large enterprises
🧠 Key SQL Concepts
1. Tables
• Data in SQL is stored in tables, which are like spreadsheets with rows and columns.
sql
CopyEdit
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
2. Basic SQL Commands
Command Description
SELECT Retrieves data from one or more tables
INSERT Adds new data to a table
UPDATE Modifies existing data
DELETE Removes data
CREATE Creates a new table or database
DROP Deletes a table or database
🛠 CRUD Operations (Core of SQL)
🔍 SELECT
sql
CopyEdit
SELECT name, email FROM users;
➕ INSERT
sql
CopyEdit
INSERT INTO users (id, name, email)
VALUES (1, 'Alice', 'alice@example.com');
✏ UPDATE
sql
CopyEdit
UPDATE users
SET name = 'Alice B.'
WHERE id = 1;
❌ DELETE
sql
CopyEdit
DELETE FROM users
WHERE id = 1;
🔗 Filtering & Sorting
WHERE Clause
sql
CopyEdit
SELECT * FROM users WHERE name = 'Alice';
ORDER BY
sql
CopyEdit
SELECT * FROM users ORDER BY name ASC;
LIMIT
sql
CopyEdit
SELECT * FROM users LIMIT 5;
🔄 Joins (Combining Tables)
INNER JOIN
sql
CopyEdit
SELECT users.name, orders.total
FROM users
INNER JOIN orders ON users.id = orders.user_id;
Other types include:
• LEFT JOIN – Includes all records from the left table
• RIGHT JOIN – Includes all records from the right table
• FULL OUTER JOIN – Includes all records from both tables
🧮 Aggregation Functions
• COUNT() – Number of rows
• SUM() – Total value
• AVG() – Average
• MAX() / MIN() – Highest / Lowest value
sql
CopyEdit
SELECT COUNT(*) FROM users;
🧩 SQL Best Practices
• Use parameterized queries to prevent SQL injection
• Normalize tables to avoid duplication
• Always backup your data
• Use PRIMARY KEY for uniqueness
• Use indexes for faster searches (but don't overuse them)
✅ Conclusion
SQL is a powerful, easy-to-learn language that’s foundational for data-driven applications.
With just a handful of commands, you can perform everything from simple queries to
complex data analysis and reporting.