SQL Basics: A Beginner’s Guide
SQL (Structured Query Language) is used to store, retrieve, update, and manage data in
relational databases. It is the standard language for interacting with databases like MySQL,
PostgreSQL, SQL Server, and Oracle.
1️⃣SQL Commands Categories
SQL commands are divided into five major categories:
🔹 DDL (Data Definition Language) – Defines database structures.
CREATE – Create a new table, database, or index.
ALTER – Modify an existing table structure.
DROP – Delete a table or database.
TRUNCATE – Delete all records but keep the table structure.
🔹 DML (Data Manipulation Language) – Manipulates table data.
INSERT – Add new records to a table.
UPDATE – Modify existing records.
DELETE – Remove records from a table.
🔹 DQL (Data Query Language) – Retrieves data.
SELECT – Fetch data from a database.
🔹 DCL (Data Control Language) – Manages permissions.
GRANT – Give access to users.
REVOKE – Remove access.
🔹 TCL (Transaction Control Language) – Manages transactions.
COMMIT – Save changes permanently.
ROLLBACK – Undo changes.
2️⃣Basic SQL Queries
1. Creating a Table (DDL)
sql
CopyEdit
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Department VARCHAR(50)
);
2. Inserting Data (DML)
sql
CopyEdit
INSERT INTO Employees (ID, Name, Age, Department)
VALUES (1, 'John Doe', 30, 'IT');
3. Fetching Data (DQL)
sql
CopyEdit
SELECT * FROM Employees;
4. Updating Data (DML)
sql
CopyEdit
UPDATE Employees
SET Age = 31
WHERE ID = 1;
5. Deleting Data (DML)
sql
CopyEdit
DELETE FROM Employees WHERE ID = 1;
3️⃣Filtering Data with WHERE Clause
sql
CopyEdit
SELECT * FROM Employees WHERE Age > 25;
4️⃣Sorting Data with ORDER BY
sql
CopyEdit
SELECT * FROM Employees ORDER BY Age DESC;
5️⃣Aggregate Functions (SUM, COUNT, AVG, MAX, MIN)
sql
CopyEdit
SELECT COUNT(*) FROM Employees;
SELECT AVG(Age) FROM Employees;
6️⃣Grouping Data with GROUP BY
sql
CopyEdit
SELECT Department, COUNT(*)
FROM Employees
GROUP BY Department;
7️⃣Joining Tables
sql
CopyEdit
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
JOIN Departments ON Employees.Department = Departments.ID;
🚀 SQL is a powerful language for managing data in databases. Mastering
these basics will help you write complex queries efficiently!