0% found this document useful (0 votes)
5 views3 pages

SQL Roadmap Detailed

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

SQL Roadmap Detailed

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Basics (Beginner Level)


■ What is SQL & Why is it used?
SQL (Structured Query Language) is used to interact with relational databases (RDBMS like MySQL, PostgreSQL, SQ
It helps store, retrieve, update, and delete data efficiently.

■ Example:
SELECT * FROM employees;

■ DBMS vs RDBMS
DBMS: General database system (no relations, e.g., file-based, MongoDB).
RDBMS: Stores data in tables with relationships. Example: MySQL, PostgreSQL.

■ SQL Data Types


INT → numbers
VARCHAR(n) → strings
DATE/TIME → dates & timestamps
BOOLEAN → true/false

■ Example:
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
dob DATE,
is_active BOOLEAN
);

■ Basic Commands
CREATE DATABASE school;
CREATE TABLE employees (id INT, name VARCHAR(50));

INSERT INTO employees VALUES (1, 'John');


SELECT * FROM employees;
UPDATE employees SET name = 'Mike' WHERE id = 1;
DELETE FROM employees WHERE id = 1;

■ Filtering & Sorting


SELECT name FROM employees WHERE id > 5;
SELECT * FROM employees ORDER BY name DESC;
SELECT DISTINCT department FROM employees;
SELECT * FROM employees LIMIT 5;

■ Operators
SELECT * FROM employees WHERE salary BETWEEN 3000 AND 6000;
SELECT * FROM employees WHERE name LIKE 'A%';
SELECT * FROM employees WHERE department IN ('HR', 'IT');

2. Intermediate (Querying & Joins)


■ Joins
-- INNER JOIN (common records)
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN department d ON e.dept_id = d.id;

-- LEFT JOIN (all from left table)


SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN department d ON e.dept_id = d.id;

■ Aggregate Functions
SELECT COUNT(*) FROM employees;
SELECT AVG(salary) FROM employees;
SELECT MAX(salary), MIN(salary) FROM employees;

■ Grouping
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

■ Subqueries
-- IN
SELECT name FROM employees
WHERE dept_id IN (SELECT id FROM department WHERE location = 'NY');

-- EXISTS
SELECT name FROM employees e
WHERE EXISTS (SELECT 1 FROM department d WHERE d.id = e.dept_id);

■ Constraints
CREATE TABLE student (
id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE,
age INT CHECK (age > 18),
dept_id INT NOT NULL
);

3. Advanced SQL
■ Set Operations
SELECT name FROM employees
UNION
SELECT name FROM managers;

■ Views
CREATE VIEW active_employees AS
SELECT name, salary FROM employees WHERE is_active = TRUE;

SELECT * FROM active_employees;

■ Indexes
CREATE INDEX idx_name ON employees(name);
■ Speeds up search queries.

■ Transactions
BEGIN;
UPDATE employees SET salary = salary + 500 WHERE dept_id = 2;
COMMIT;
-- or ROLLBACK if error

■ Stored Procedures & Functions


CREATE PROCEDURE getEmployee()
BEGIN
SELECT * FROM employees;
END;
CALL getEmployee();

■ Triggers
CREATE TRIGGER before_insert_employee
BEFORE INSERT ON employees
FOR EACH ROW
SET NEW.created_at = NOW();

4. Expert SQL (Job-Level)


■ Window Functions
SELECT name, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;

■ CTE
WITH dept_count AS (
SELECT dept_id, COUNT(*) AS total
FROM employees
GROUP BY dept_id
)
SELECT * FROM dept_count WHERE total > 5;

■ Recursive Queries
WITH RECURSIVE nums AS (
SELECT 1 AS n
UNION ALL
SELECT n+1 FROM nums WHERE n < 5
)
SELECT * FROM nums;

■ Pivot & Unpivot


(Mostly in SQL Server / Oracle)

■ Performance Optimization
Use indexes wisely
Avoid SELECT *
Normalize DB (remove redundancy)
Check execution plan

■ Security & Access Control


CREATE USER 'john'@'localhost' IDENTIFIED BY 'pass123';
GRANT SELECT, INSERT ON school.* TO 'john'@'localhost';

You might also like