📘 Table of Contents
1. Introduction to SQL
SQL (Structured Query Language) is the standard language used to manage and manipulate relational databases such as MySQL, PostgreSQL, and SQL Server.
2. Setting Up Environment
- Install MySQL or use SQLite for lightweight databases.
- Use a client like phpMyAdmin or DBeaver to run SQL commands.
3. Basic SQL Commands
CREATE DATABASE company;
USE company;
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
position VARCHAR(50),
salary DECIMAL(10,2)
);
4. WHERE, ORDER BY, and LIMIT
SELECT * FROM employees WHERE salary > 50000;
SELECT * FROM employees ORDER BY name ASC;
SELECT * FROM employees LIMIT 5;
5. Joins
SELECT e.name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.id;
6. Aggregate Functions
SELECT COUNT(*) FROM employees;
SELECT AVG(salary) FROM employees;
SELECT MAX(salary) FROM employees;
7. CRUD Operations
-- Insert
INSERT INTO employees (name, position, salary)
VALUES ('Alice', 'Manager', 75000);
-- Read
SELECT * FROM employees;
-- Update
UPDATE employees SET salary = 80000 WHERE name = 'Alice';
-- Delete
DELETE FROM employees WHERE name = 'Alice';
8. Mini Project — Employee Database
Create a database named company and practice adding, updating, and deleting employee records.
Use JOIN to connect multiple tables like departments and projects.
9. Conclusion
SQL is essential for anyone working with data. Mastering it allows you to query, manage, and analyze information efficiently — a must-have skill for developers and analysts alike.
Comments
Post a Comment