Comprehensive SQL: Learn from basics to expert level—core concepts, advanced queries, optimization, security, and real-world case studies with hands-on examples and theory, by Ram Sir.
Start Learning SQLRam Sir, expert database architect, brings 2+ years of SQL and DBMS experience, training thousands in industry and academia.
Start with the foundations of SQL: tables, data types, constraints, and basic data manipulation.
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT,
Department VARCHAR(50),
HireDate DATE,
Salary DECIMAL(9,2)
);
-- Insert
INSERT INTO Employees (EmpID, Name, Age, Department, Salary) VALUES (1, 'John', 30, 'IT', 70000);
-- Select
SELECT Name, Salary FROM Employees WHERE Department = 'IT';
-- Update
UPDATE Employees SET Salary = Salary + 5000 WHERE EmpID = 1;
-- Delete
DELETE FROM Employees WHERE EmpID = 1;
-- Add UNIQUE and CHECK
ALTER TABLE Employees ADD CONSTRAINT unique_email UNIQUE (Email);
ALTER TABLE Employees ADD CONSTRAINT check_age CHECK (Age >= 18);
SQL is a declarative language: you specify what you want, not how. Tables are the core, but constraints (PK, FK, UNIQUE, CHECK, DEFAULT, NOT NULL) are critical for data integrity.
Go deeper with joins, subqueries, views, indexes, and transactions.
-- INNER JOIN
SELECT e.Name, d.DeptName
FROM Employees e
INNER JOIN Departments d ON e.Department = d.DeptID;
-- LEFT JOIN
SELECT e.Name, d.DeptName
FROM Employees e
LEFT JOIN Departments d ON e.Department = d.DeptID;
-- CROSS JOIN
SELECT * FROM Products CROSS JOIN Suppliers;
-- Subquery in WHERE
SELECT Name FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees);
-- Correlated Subquery
SELECT e.Name FROM Employees e
WHERE Salary > (SELECT AVG(Salary) FROM Employees WHERE Department = e.Department);
-- View
CREATE VIEW HighEarners AS
SELECT Name, Salary FROM Employees WHERE Salary > 80000;
-- Index
CREATE INDEX idx_dept_salary ON Employees(Department, Salary);
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 200 WHERE AccountID = 1;
UPDATE Accounts SET Balance = Balance + 200 WHERE AccountID = 2;
COMMIT;
Views = reusable queries; Indexes = performance boost; Transactions = atomicity, consistency, isolation, durability (ACID).
Unlock the power of SQL with advanced programming, analytics, optimization, and database design.
-- Procedure (MySQL style)
DELIMITER //
CREATE PROCEDURE GiveRaise(IN emp INT, IN amount DECIMAL(9,2))
BEGIN
UPDATE Employees SET Salary = Salary + amount WHERE EmpID = emp;
END //
DELIMITER ;
CALL GiveRaise(2, 10000);
CREATE TRIGGER audit_salary
AFTER UPDATE ON Employees
FOR EACH ROW
INSERT INTO SalaryAudit(EmpID, OldSalary, NewSalary, ChangeDate)
VALUES (OLD.EmpID, OLD.Salary, NEW.Salary, NOW());
-- CTE (WITH)
WITH DeptAvg AS (
SELECT Department, AVG(Salary) AS DeptAvgSalary
FROM Employees
GROUP BY Department
)
SELECT e.Name, e.Department, e.Salary, d.DeptAvgSalary
FROM Employees e
JOIN DeptAvg d ON e.Department = d.Department;
-- Window Function (RANK)
SELECT Name, Salary, RANK() OVER (ORDER BY Salary DESC) AS SalaryRank
FROM Employees;
EXPLAIN SELECT * FROM Employees WHERE Department = 'Finance';
-- Optimize with index:
CREATE INDEX idx_dept ON Employees(Department);
Use CTEs for complex queries; window functions for analytics; triggers for automation; study execution plans for speed.
Keep your database safe with user management, privileges, backup/restore, and auditing.
CREATE USER analyst IDENTIFIED BY 'pass123';
GRANT SELECT, INSERT ON Employees TO analyst;
REVOKE INSERT ON Employees FROM analyst;
DROP USER analyst;
-- MySQL dump
mysqldump -u root -p mydb > backup.sql
-- Restore
mysql -u root -p mydb < backup.sql
-- Enable audit log (MySQL example)
INSTALL PLUGIN audit_log SONAME 'audit_log.so';
SHOW VARIABLES LIKE 'audit%';
Always use least privilege, strong passwords, regular backups, and audit logs for compliance.
Practice advanced SQL with real-world scenarios, reporting, and analytics.
SELECT Department, Name, Salary
FROM (
SELECT e.*, RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS rnk
FROM Employees e
) ranked
WHERE rnk = 1;
SELECT Year, SUM(Sales) AS TotalSales,
LAG(SUM(Sales)) OVER (ORDER BY Year) AS PrevYearSales,
((SUM(Sales) - LAG(SUM(Sales)) OVER (ORDER BY Year))/LAG(SUM(Sales)) OVER (ORDER BY Year))*100 AS YoYGrowth
FROM Sales
GROUP BY Year;
DELETE FROM Employees
WHERE EmpID NOT IN (
SELECT MIN(EmpID) FROM Employees GROUP BY Email
);
SELECT a.Name, b.Sales
FROM HR.Employees a
JOIN Sales.Records b ON a.EmpID = b.EmpID;
Real SQL skills come from practice—try to solve these and tweak them for your data!
Results will appear here...
Most-used SQL statements and features, with query examples.
CREATE TABLE Products (...);
ALTER TABLE Products ADD COLUMN Price DECIMAL(5,2);
INSERT INTO t (a,b) VALUES (1,2);
UPDATE t SET a=2 WHERE b=2;
SELECT col FROM t WHERE a=1 ORDER BY b;
SELECT COUNT(*), SUM(amount), AVG(salary) FROM t;
SELECT * FROM a INNER JOIN b ON a.id = b.id;
SELECT name, RANK() OVER (ORDER BY salary) FROM t;
GRANT SELECT ON db.table TO user;
mysqldump -u root -p db > backup.sql;