SQL

SQL Complete Tutorial โ€“ Learn SQL Queries from Scratch | Esikhcha

๐Ÿ—„๏ธ SQL Complete Tutorial

Structured Query Language โ€” from basic SELECT queries to advanced stored procedures

๐Ÿ“– SELECT & FROM ๐Ÿ” WHERE & FILTERS ๐Ÿ”— JOINS ๐Ÿ“Š GROUP BY & HAVING โš™๏ธ SUBQUERIES
CHAPTER 1

What is SQL?

SQL (Structured Query Language) is the standard language for communicating with relational databases. It allows you to retrieve, insert, update, and delete data stored in tables.

Think of SQL as a way to ask questions to a database: “Give me all customers from New York” or “Show me the total sales for last month.”

๐Ÿ’ก Key concept: Databases store data in tables (rows and columns), and SQL is the tool you use to interact with those tables.

Sample Database: Employees Table

idnamedepartmentsalarycity
1AliceIT75000New York
2BobSales65000Chicago
3CharlieIT82000New York
CHAPTER 2 ยท THE FOUNDATION

The Basic SQL Query: SELECT and FROM

Every SQL query starts with SELECT (what columns you want) and FROM (which table contains the data). This is the simplest and most important SQL pattern.

Basic Syntax

๐Ÿ“Œ SELECT columns FROM table
SELECT column1, column2, column3
FROM table_name;

Example 1: Select specific columns

โ–ถ Query
SELECT name, salary, city
FROM employees;
๐Ÿ“Š Result:
Alice | 75000 | New York
Bob | 65000 | Chicago
Charlie | 82000 | New York

Example 2: Select ALL columns using * (asterisk)

โ–ถ Query
SELECT * FROM employees;
๐Ÿ“Š Returns every column from the employees table
โšก Best practice: Avoid SELECT * in production โ€” explicitly name columns for better performance and clarity.
CHAPTER 3

Filtering Rows with WHERE

The WHERE clause filters rows based on a condition. Only rows that meet the condition are returned.

๐Ÿ“Œ WHERE syntax
SELECT column1, column2
FROM table_name
WHERE condition;

Example: Find IT employees

โ–ถ Query
SELECT name, department, salary
FROM employees
WHERE department = 'IT';
๐Ÿ“Š Result:
Alice | IT | 75000
Charlie | IT | 82000

Comparison Operators

OperatorMeaningExample
=Equal toWHERE salary = 75000
>Greater thanWHERE salary > 70000
<Less thanWHERE salary < 80000
>=Greater than or equalWHERE salary >= 65000
<=Less than or equalWHERE salary <= 75000
<>Not equalWHERE department <> 'Sales'
CHAPTER 4

Logical Operators: AND, OR, NOT, IN, BETWEEN

AND โ€” all conditions must be true

SELECT name, department, salary, city
FROM employees
WHERE department = 'IT' AND salary > 70000;

OR โ€” at least one condition must be true

SELECT name, city
FROM employees
WHERE city = 'New York' OR city = 'Chicago';

IN โ€” matches any value in a list

SELECT name, department
FROM employees
WHERE department IN ('IT', 'Sales');

BETWEEN โ€” range filter (inclusive)

SELECT name, salary
FROM employees
WHERE salary BETWEEN 70000 AND 80000;
CHAPTER 5

Sorting Results with ORDER BY

-- Ascending (lowest to highest, default)
SELECT name, salary FROM employees ORDER BY salary ASC;

-- Descending (highest to lowest)
SELECT name, salary FROM employees ORDER BY salary DESC;

-- Multiple columns
SELECT name, department, salary 
FROM employees 
ORDER BY department ASC, salary DESC;
CHAPTER 6

DISTINCT & LIMIT

DISTINCT removes duplicate rows. LIMIT restricts number of rows returned.

-- Get unique departments
SELECT DISTINCT department FROM employees;

-- Get top 2 highest paid employees
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 2;
CHAPTER 7

Aggregate Functions: COUNT, SUM, AVG, MIN, MAX

-- Count total employees
SELECT COUNT(*) AS total_employees FROM employees;

-- Average salary
SELECT AVG(salary) AS average_salary FROM employees;

-- Highest and lowest salary
SELECT MAX(salary) AS highest, MIN(salary) AS lowest FROM employees;

-- Total salary sum
SELECT SUM(salary) AS payroll FROM employees;
CHAPTER 8

GROUP BY & HAVING

GROUP BY groups rows that have the same values. HAVING filters groups (like WHERE for groups).

-- Average salary per department
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

-- Departments with average salary above 70000
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 70000;
CHAPTER 9

JOINS: Combining Multiple Tables

Real databases have many related tables. JOINs allow you to combine them.

INNER JOIN โ€” only matching rows

SELECT orders.id, customers.name, orders.total
FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;

LEFT JOIN โ€” all rows from left table

SELECT customers.name, orders.total
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id;
CHAPTER 10

UNION & UNION ALL

-- Combine results, remove duplicates
SELECT city FROM customers
UNION
SELECT city FROM suppliers;

-- Keep all duplicates
SELECT city FROM customers
UNION ALL
SELECT city FROM suppliers;
CHAPTER 11

Subqueries (Nested Queries)

A subquery is a query inside another query.

-- Find employees earning more than average
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

-- EXISTS checks if subquery returns any rows
SELECT name FROM departments d
WHERE EXISTS (SELECT 1 FROM employees e WHERE e.dept_id = d.id);
CHAPTER 12

CASE โ€” Conditional Logic

SELECT name, salary,
  CASE 
    WHEN salary >= 80000 THEN 'High'
    WHEN salary >= 60000 THEN 'Medium'
    ELSE 'Low'
  END AS salary_level
FROM employees;
CHAPTER 13

INSERT INTO โ€” Adding Data

-- Insert specific columns
INSERT INTO employees (name, department, salary, city)
VALUES ('Diana', 'Marketing', 70000, 'Boston');

-- Insert all columns (order must match table)
INSERT INTO employees VALUES (4, 'Diana', 'Marketing', 70000, 'Boston');
CHAPTER 14

UPDATE & DELETE

โš ๏ธ WARNING: Always use WHERE with UPDATE/DELETE, otherwise ALL rows will be affected!
-- Update specific rows
UPDATE employees SET salary = 78000 WHERE name = 'Alice';

-- Delete specific rows
DELETE FROM employees WHERE name = 'Bob';
CHAPTER 15

NULL Values

NULL means missing or unknown data. Use IS NULL or IS NOT NULL (not = NULL).

SELECT * FROM employees WHERE bonus IS NULL;
SELECT COALESCE(bonus, 0) AS bonus_amount FROM employees;
CHAPTER 16

Stored Procedures

Reusable SQL code blocks that accept parameters.

-- MySQL syntax
DELIMITER //
CREATE PROCEDURE GetEmployeesByDept(IN dept_name VARCHAR(50))
BEGIN
  SELECT * FROM employees WHERE department = dept_name;
END //
DELIMITER ;

-- Call the procedure
CALL GetEmployeesByDept('IT');
PRACTICE

๐ŸŽฏ Complete Query Example

-- Find top 3 departments by average salary (min 2 employees)
SELECT 
    department, 
    COUNT(*) AS emp_count,
    AVG(salary) AS avg_salary,
    MAX(salary) AS max_salary
FROM employees
WHERE city = 'New York'
GROUP BY department
HAVING COUNT(*) >= 2
ORDER BY avg_salary DESC
LIMIT 3;
This query filters, groups, aggregates, filters groups, sorts, and limits โ€” combining everything you've learned!

๐Ÿ“˜ Complete SQL Tutorial โ€” from basic SELECT queries to advanced stored procedures.

ยฉ Esikhcha ยท SQL Mastery โ€” Practice on MySQL, PostgreSQL, or SQL Server