Introduction to SQL
SQL (Structured Query Language) is the standard language for interacting with relational databases such as MySQL, PostgreSQL, Oracle, SQL Server, and SQLite.
No matter if you are a data analyst, software developer, or database administrator, SQL is an essential skill.
1. SQL Basics (Beginner Level)
1.1 What is a Database?
A database is an organized collection of data. Relational databases store this data in tables, similar to Excel spreadsheets.
1.2 Basic SQL Syntax
SELECT column1, column2
FROM table_name
WHERE condition;
1.3 Common SQL Commands
- SELECT – Retrieves data
- INSERT – Adds new data
- UPDATE – Modifies existing data
- DELETE – Removes data
- CREATE – Creates database objects (tables, views)
- DROP – Deletes database objects
2. SQL Intermediate Concepts
2.1 Filtering & Sorting
SELECT * FROM employees
WHERE department = 'Sales'
ORDER BY salary DESC
LIMIT 10;
2.2 SQL Functions
COUNT()
,SUM()
,AVG()
,MIN()
,MAX()
2.3 Grouping with GROUP BY
SELECT department, COUNT(*) AS total
FROM employees
GROUP BY department;
2.4 Table Joins
INNER JOIN Example:
SELECT e.name,e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.id;d.id;
3. SQL Table Operations
Creating a Table:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10,2),
dept_id INT
);
Inserting Data:
INSERT INTO employees (id, name, salary, dept_id)
VALUES (1, 'Alice', 55000, 2);
Updating & Deleting:
UPDATE employees SET salary = 60000 WHERE name = 'Alice';
DELETE FROM employees WHERE id = 1;
4. Advanced SQL Concepts
Subqueries:
SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
Window Functions:
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
CTEs (Common Table Expressions):
WITH HighEarners AS (
SELECT * FROM employees WHERE salary > 70000
)
SELECT * FROM HighEarners;
Stored Procedures (MySQL Example):
DELIMITER //
CREATE PROCEDURE GetHighEarners()
BEGIN
SELECT * FROM employees WHERE salary > 70000;
END //
DELIMITER ;
Triggers:
CREATE TRIGGER before_insert
BEFORE INSERT ON employees
FOR EACH ROW
SET NEW.created_at = NOW();
5. SQL Performance Optimization
- Utilize appropriate indexes on columns used in
WHERE
,JOIN
, orORDER BY
- - Avoid
SELECT *,*
; specify only the required columns - Use
EXPLAIN
to analyze query performance
6. Real-World Use Cases
- Data Analysis – Aggregating sales, marketing, or user behavior data
- Web Development – Back-end CRUD operations
- ETL Pipelines – Extracting and transforming data for dashboards
- Reporting – Generating KPIs, metrics, and operational insights
SQL is one of the most sought-after skills in the data and software fields. Whether you’re analyzing data or developing scalable applications, mastering SQL will elevate your career.
Leave a Reply