Learn_Advanced_Sql (1)
Learn_Advanced_Sql (1)
Window Functions
* OVER, PARTITION BY, ORDER BY
* ROW_NUMBER(), RANK(), DENSE_RANK()
* LAG(), LEAD(), FIRST_VALUE(), LAST_VALUE()
3. Advanced Joins
* Self joins
* Cross joins
* Full outer joins
* Multiple table joins with complex conditions
4. Subqueries
* Correlated subqueries
* Nested subqueries
* EXISTS and NOT EXISTS
* ANY, ALL operators
5. Performance Optimization
* Index optimization
* Query execution plans
* Statistics and cardinality
* Materialized views
7. Transaction Management
* ACID properties
* Isolation levels
* Deadlock prevention
* Concurrent transactions
8. Partitioning
* Range partitioning
* List partitioning
* Hash partitioning
* Partition pruning
9. Pivot and Unpivot
* Dynamic pivoting
* Cross tabulation
* Converting rows to columns and vice versa
Window Functions
ROW_NUMBER()
SQL_QUERY:-
SELECT emp_id, emp_name, department, salary, ROW_NUMBER() OVER
(PARTITION BY department ORDER BY salary DESC) AS row_num FROM employees;
OUTPUT:-
2. RANK():-
SQL_QUERY:-
SELECT
emp_id,
emp_name,
department,
salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS
row_num
FROM employees;
OUTPUT:-
3. DENSE_RANK()
● Similar to RANK(), but does not skip numbers for ties.
● Employees with the same salary get the same rank.
● Ranks are sequential, without skipping numbers for ties
SQL_QUERY:-
SELECT
emp_id,
emp_name,
department,
salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS
dense_rank_num
FROM employees;
OUTPUT:-
4.LAG():-
Looks at the previous row’s salary for each employee in the ordered window.
NULL appears if there is no previous row.
SQL_QUERY:-
SELECT
emp_id,
emp_name,
department,
salary,
LAG(salary) OVER (PARTITION BY department ORDER BY salary ASC) AS prev_salary
FROM employees;
OUTPUT:-
SQL_QUERY:-
SELECT
emp_id,
emp_name,
department,
salary,
LEAD(salary) OVER (PARTITION BY department ORDER BY salary ASC) AS
next_salary
FROM employees;
OUTPUT:-
6.FIRST_VALUE()
Returns the first value in the ordered window.
Fetches the smallest salary (ORDER BY salary ASC) in each department.
Always returns the first value in the ordered window.
SQL_QUERY:-
SELECT
emp_id,
emp_name,
department,
salary,
FIRST_VALUE(salary) OVER (PARTITION BY department ORDER BY salary ASC)
AS first_salary
FROM employees;
OUTPUT:-
7.LAST_VALUE
Returns the last value in the ordered window.
Fetches the largest salary (ORDER BY salary ASC) in each department.
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
ensures the full partition is
SQL_QUERY:-
SELECT
emp_id,
emp_name,
department,
salary,
LAST_VALUE(salary) OVER (PARTITION BY department ORDER BY salary ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS
last_salary
FROM employees;
OUTPUT:-
========================================================================
========
Updated Table For CTE:-
ALTER TABLE employees
ADD COLUMN manager_id INT;
Example:
Suppose we want to find employees in the Sales department and their salaries.
SQL_QUERY:-
WITH sales_employees AS (
SELECT emp_id, emp_name, salary
FROM employees
WHERE department = 'Sales'
)
SELECT emp_id, emp_name, salary
FROM sales_employees
WHERE salary > 60000;
OUTPUT:-
Explanation:
You can define multiple CTEs in a single query by separating them with commas.
Example:
SQL_QUERY:-
WITH sales_cte AS (
SELECT emp_id, emp_name, salary
FROM employees
WHERE department = 'Sales'
),
high_salary_cte AS (
SELECT emp_id, emp_name, salary
FROM employees
WHERE salary > 65000
)
SELECT s.emp_id, s.emp_name, s.salary
FROM sales_cte s
JOIN high_salary_cte h ON s.emp_id = h.emp_id;
OUTPUT:-
3. Recursive CTEs
A recursive CTE is a type of CTE that references itself to perform recursive operations. It is
commonly used for hierarchical data (like employee-manager relationships or organizational
charts).
Example:
Assume we have an employees table where emp_id is the employee ID, emp_name is the
employee name, and manager_id references the employee's manager.
SQL_QUERY:-
WITH RECURSIVE employee_hierarchy AS (
--Comment:- Anchor member: Select top-level employees (managers without a
manager)
SELECT emp_id, emp_name, manager_id, 1 AS level
FROM employees
WHERE manager_id IS NULL
UNION ALL
OUTPUT:-
Explanation
========================================================================
========
New Table Added For Joins :-
CREATE TABLE projects (
project_id INT PRIMARY KEY,
project_name VARCHAR(50),
department VARCHAR(50),
project_lead_id INT
);
3. Advanced Joins
A join combines rows from two or more tables based on a column they share in common.
1. Inner Join
It only shows rows that have matching values in both tables. If there’s no match, the row won’t
appear.
Example: Show employees who are project leads.
SQL_QUERY:-
SELECT
e.emp_name AS employee_name,
p.project_name,
p.department
FROM
employees e
INNER JOIN
projects p ON e.emp_id = p.project_lead_id;
OUTPUT:-
OUTPUT:-
SELECT
e.emp_name AS employee_name,
p.project_name
FROM
employees e
RIGHT JOIN
projects p ON e.emp_id = p.project_lead_id;
OUTPUT:-
5. Cross Join
It creates all possible combinations of rows from both tables (a "Cartesian product").
Example: Pair every employee with every project.
SQL_QUERY:-
SELECT
e.emp_name AS employee_name,
p.project_name
FROM
employees e
CROSS JOIN
projects p;
OUTPUT:-
6. Self Join
It joins a table to itself, typically used for hierarchical data.
Example: Show employees and their managers from the same table.
SQL_QUERY:-
SELECT
e.emp_name AS employee_name,
m.emp_name AS manager_name
FROM
employees e
LEFT JOIN
employees m ON e.manager_id = m.emp_id;
OUTPUT:-
========================================================================
========
Subqueries
* Correlated subqueries
* Nested subqueries
* EXISTS and NOT EXISTS
* ANY, ALL operators
1. Correlated Subqueries
A correlated subquery depends on the outer query for its values. It is executed repeatedly,
once for each row selected by the outer query.
Example: Find employees earning more than the average salary of their department
SQL_QUERY:-
SELECT emp_id, emp_name, salary, department
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department = e.department
);
OUTPUT:-
Explanation:
2. Nested Subqueries
A nested subquery (also called a simple subquery) is an inner query that is executed only
once and its result is passed to the outer query.
SQL_QUERY:-
SELECT emp_id, emp_name
FROM employees
WHERE department = (
SELECT department
FROM employees
WHERE emp_name = 'Jane Smith'
);
OUTPUT:-
Explanation:
1. It's more efficient - there's no need to retrieve actual column values when you just
want to check existence
2. It clearly communicates intent - other developers can quickly see that you're only
checking for existence
3. It's slightly faster than SELECT * since it doesn't need to fetch actual column data
OUTPUT:-
Explanation:
● EXISTS checks if the subquery returns any rows; if it does, the condition is TRUE.
● NOT EXISTS checks if the subquery does not return any rows; if it doesn’t, the
condition is TRUE.
SQL_QUERY:-
OUTPUT:-
Explanation:
● ANY: The condition is TRUE if it matches at least one value in the subquery result.
● ALL: The condition is TRUE only if it matches all values in the subquery result.
1. Correlated Subqueries
A correlated subquery depends on the outer query for its values. It is executed repeatedly,
once for each row selected by the outer query.
Example: Find employees earning more than the average salary of their department
sql
=====
SELECT emp_id, emp_name, salary, department
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department = e.department
);
Explanation:
2. Nested Subqueries
A nested subquery (also called a simple subquery) is an inner query that is executed only
once and its result is passed to the outer query.
Explanation:
Explanation:
● EXISTS checks if the subquery returns any rows; if it does, the condition is TRUE.
● NOT EXISTS checks if the subquery does not return any rows; if it doesn’t, the condition
is TRUE.
ANY Example: Find employees earning more than at least one employee in the 'IT'
department
sql
=====
SELECT emp_id, emp_name, salary
FROM employees
WHERE salary > ANY (
SELECT salary
FROM employees
WHERE department = 'IT'
);
ALL Example: Find employees earning more than all employees in the 'HR' department
sql
=====
SELECT emp_id, emp_name, salary
FROM employees
WHERE salary > ALL (
SELECT salary
FROM employees
WHERE department = 'HR'
);
Explanation:
● ANY: The condition is TRUE if it matches at least one value in the subquery result.
● ALL: The condition is TRUE only if it matches all values in the subquery result.
Summary Table
Type Definition Example Use Case
Correlated Subquery depends on the outer query. Employees earning more than
Subquery Executed for each row in the outer the department average.
query.
Nested Subquery Subquery is executed once and its Employees in the same
result is passed to the outer query. department as 'Jane Smith'.
EXISTS/NOT Checks if the subquery returns any Employees who lead or don’t
EXISTS rows. lead a project.
Definition:
Indexes speed up data retrieval by reducing the amount of data scanned. However, they can
slow down write operations (e.g., INSERT, UPDATE, DELETE). Choosing the right columns to
index is critical.
Types of Indexes:
SQL_QUERY:-
OUTPUT:-
Definition:
A query execution plan shows how the SQL engine retrieves data. It helps identify inefficiencies
like full table scans, missing indexes, or incorrect join orders.
SQL_QUERY:-
-- Use EXPLAIN to get the query execution plan
OUTPUT:-
Definition:
Statistics help the SQL optimizer choose the best execution plan by estimating the number of
rows matching a query condition (cardinality).
Cardinality:
SQL_QUERY:-
-- Update table statistics
ANALYZE TABLE employees;
OUTPUT:-
—-------------------------------------------------------------------------------------------------
-- Show index statistics
SQL_QUERY:-
SHOW INDEX FROM employees;
OUTPUT:-
4. Materialized Views
Definition:
Materialized views store the result of a query physically in the database, unlike regular views
which are virtual. They are useful for improving performance on complex queries by
precomputing results.
OUTPUT:-
Benefits:
SQL_QUERY:-
-- Refresh the materialized view
REPLACE INTO department_salary
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
Summary Table
Optimization Purpose Example Use Case
Technique
Query Analyze how SQL retrieves Replace a full table scan (ALL) with
Execution Plans data and optimize inefficient an index scan (index).
queries.
Statistics & Helps the optimizer choose Updating table statistics ensures
Cardinality the best execution plan. accurate cardinality estimates for
filtering and joins.
Definition:
A user-defined function is a reusable piece of code that returns a value. It is often used for
calculations or transformations.
SQL_QUERY:-
DELIMITER $$
RETURNS DECIMAL(10, 2)
DETERMINISTIC
BEGIN
END $$
DELIMITER ;
FROM employees;
OUTPUT:-
Key Points:
● DETERMINISTIC: The function always returns the same result for the same input.
● Functions are often used in SELECT or WHERE clauses
2. Stored Procedures
Definition:
A stored procedure is a block of SQL code stored in the database that can perform actions
(e.g., insert, update, delete, or complex queries). Unlike functions, procedures don’t return a
value directly.
SQL_QUERY:-
DELIMITER $$
BEGIN
UPDATE employees
END $$
DELIMITER ;
SET SQL_SAFE_UPDATES = 0;
OUTPUT:-
Key Points:
● IN, OUT, INOUT: Parameters can be input (IN), output (OUT), or both (INOUT).
● Use procedures for data manipulation and complex operations.
3. Triggers
Definition:
A trigger is a piece of code that automatically executes in response to certain events on a table
(e.g., INSERT, UPDATE, or DELETE).
SQL_QUERY:-
DELIMITER $$
BEGIN
END $$
DELIMITER ;
OUTPUT:-
Explanation:
4. Exception Handling
Definition:
SQL_QUERY:-
DELIMITER $$
BEGIN
DECLARE emp_not_found CONDITION FOR SQLSTATE '02000'; -- No rows found
FROM employees
END $$
DELIMITER ;
OUTPUT:-
Key Points:
● Use DECLARE HANDLER to define how to handle specific SQL errors or conditions.
● SQLSTATE codes specify different types of errors.
5. Dynamic SQL
Definition:
Dynamic SQL allows SQL code to be constructed and executed at runtime.
DELIMITER ;
OUTPUT:-
Key Points:
Summary Table
Feature Definition Example Use Case
Dynamic SQL Constructs and executes SQL Fetching data from a table
queries dynamically at runtime. based on the table name
passed as a parameter.
========================================================================
========================================================================
================
7. Transaction Management
1. ACID Properties
Definition:
ACID ensures that database transactions are processed reliably. The properties are:
● Atomicity: A transaction is all-or-nothing. If one part fails, the entire transaction rolls
back.
● Consistency: Ensures the database transitions from one valid state to another.
● Isolation: Ensures concurrent transactions don’t interfere with each other.
● Durability: Once a transaction is committed, its changes are permanent.
Example:
SQL_QUERY:-
START TRANSACTION;
-- Simulate a failure
OUTPUT:-
2. Isolation Levels
Definition:
Isolation levels control how transactions interact with each other. Higher isolation reduces
concurrency issues but can reduce performance.
Isolation Levels:
1. Read Uncommitted: Allows transactions to read uncommitted changes. Risky but fast.
2. Read Committed: Transactions can only read committed changes.
3. Repeatable Read: Ensures rows read in a transaction remain unchanged until it
completes.
4. Serializable: Highest level, transactions are fully isolated.
START TRANSACTION;
COMMIT;
OUTPUT:-
3. Deadlock Prevention
Definition:
A deadlock occurs when two or more transactions wait for each other to release
resources, causing an infinite wait.
-- Transaction 2
START TRANSACTION;
UPDATE employees SET salary = salary + 2000 WHERE emp_id = 2;
OUTPUT:-
MySQL will handle the deadlock by rolling back one of the transactions automatically.
4. Concurrent Transactions
Definition:
Concurrent transactions are multiple transactions executed simultaneously. Proper
management ensures data consistency and prevents issues like race conditions.
Common Issues:
-- Transaction 2
START TRANSACTION;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT salary FROM employees WHERE emp_id = 1; -- Waits for Transaction 1 to commit or
rollback
OUTPUT:-
Summary Table
Concept Definition Example Use Case
1. Range Partitioning
Definition: Divides the table based on a range of values in a specific column (e.g., date or
salary).
Example: Partition Employees Based on Salary Range
Sql_Query:-
CREATE TABLE employees_partitioned (
emp_id INT NOT NULL,
emp_name VARCHAR(50),
department VARCHAR(50),
salary INT NOT NULL, -- Changed DECIMAL to INT
hire_date DATE,
PRIMARY KEY (emp_id, salary) -- Required for partitioning
)
PARTITION BY RANGE (salary) (
PARTITION p1 VALUES LESS THAN (50000),
PARTITION p2 VALUES LESS THAN (70000),
PARTITION p3 VALUES LESS THAN (100000)
);
Output:-
✅ How it Works?
● Employees with salary < 50,000 go into p1.
● Employees with 50,000 ≤ salary < 70,000 go into p2.
● Employees with 70,000 ≤ salary < 100,000 go into p3.
2. List Partitioning
Definition: Divides data based on predefined lists of values.
SQL_QUERY:-
CREATE TABLE employees_list (
emp_id INT NOT NULL,
emp_name VARCHAR(50),
department_code INT NOT NULL, -- Changed from VARCHAR to INT
salary DECIMAL(10,2),
hire_date DATE,
PRIMARY KEY (emp_id, department_code) -- Required for partitioning
)
PARTITION BY LIST (department_code) (
PARTITION p_sales VALUES IN (1),
PARTITION p_hr VALUES IN (2),
PARTITION p_it VALUES IN (3)
);
NOTE:-MySQL only allows integer columns for LIST partitioning. The department
column is a VARCHAR(50), which is not allowed.
OUTPUT:-
✅ How it Works?
● Employees in the Sales department go into p_sales.
● Employees in the HR department go into p_hr.
● Employees in the IT department go into p_it.
● 1 → Sales
● 2 → HR
● 3 → IT
3. Hash Partitioning
Definition: Divides data evenly across partitions using a hash function (useful for load
balancing).
Example: Partition Employees Using Hash on Employee ID
SQL_QUERY:-
CREATE TABLE employees_hash (
emp_id INT NOT NULL,
emp_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2),
hire_date DATE
)
PARTITION BY HASH(emp_id) PARTITIONS 4;
OUTPUT:-
✅ How it Works?
● The system automatically assigns records to 4 partitions based on emp_id % 4.
● Helps distribute data evenly when no natural partitioning criteria exist.
4. Partition Pruning
Definition: Optimizes queries by scanning only the relevant partitions instead of the entire table.
Example: Query Optimization Using Range Partitioning
SQL_QUERY:-
EXPLAIN SELECT * FROM employees_partitioned WHERE salary BETWEEN 50000 AND
70000;
OUTPUT:-
✅ How it Works?
● Instead of scanning the entire table, MySQL will only look at partitions p2 (where
salary falls within the range).
● Faster queries, improved performance!
Summary Table
Partition Type Definition Best Used For
SQL_QUERY:-
SELECT
emp_id,
SUM(CASE WHEN sales_month = 'Jan' THEN sales_amount ELSE 0 END) AS Jan_Sales,
SUM(CASE WHEN sales_month = 'Feb' THEN sales_amount ELSE 0 END) AS Feb_Sales
FROM sales
GROUP BY emp_id;
OUTPUT:-
✅ Explanation:
● We use CASE WHEN inside SUM() to convert row values (sales_month) into
separate columns (Jan_Sales, Feb_Sales).
● GROUP BY emp_id ensures the pivoted values are grouped for each employee.
SET @sql = CONCAT('SELECT emp_id, ', @sql, ' FROM sales GROUP BY emp_id;');
✅ Explanation:
● GROUP_CONCAT() dynamically generates column names.
● PREPARE and EXECUTE allow dynamic query execution.
OUTPUT:-
✅ Explanation:
● UNION ALL is used to transform columns back into rows.
OUTPUT:-
✅ Explanation:
● This helps compare departments across different months.
📌 Key Takeaways
Concept Definition Method Used
========================================================================
========
2.XML Handling
Definition:-Used for storing and querying hierarchical data structures like XML
MySQL does not have built-in XML functions like PostgreSQL or SQL Server, but you can
store XML as TEXT and process it using external tools.
sql
=====
SELECT emp_name, SUBSTRING_INDEX(SUBSTRING_INDEX(details,
'<department>', -1), '</department>', 1) AS department
FROM employees_xml;
Output:-
📌 MySQL does NOT support user-defined types like PostgreSQL or SQL Server.
However, you can simulate them using ENUM, SET, or JSON.
📌 Inserting Data
sql
INSERT INTO employees_enum (emp_id, emp_name, emp_role)
VALUES
(1, 'John Doe', 'Manager'),
(2, 'Jane Smith', 'Developer');
✅ Alternative in MySQL:
● Use JSON for flexible custom data
● Use ENUM for fixed values (best for roles, categories)
Output:-