0% found this document useful (0 votes)
4 views48 pages

Learn_Advanced_Sql (1)

The document provides an overview of advanced SQL concepts including window functions, common table expressions (CTEs), advanced joins, subqueries, performance optimization, stored procedures, transaction management, partitioning, pivoting, and advanced data types. It includes SQL queries and examples for each concept, demonstrating their application using a sample employee table and a projects table. Key topics covered include ranking functions, recursive CTEs, various types of joins, and the use of subqueries for complex queries.

Uploaded by

shivanshu soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views48 pages

Learn_Advanced_Sql (1)

The document provides an overview of advanced SQL concepts including window functions, common table expressions (CTEs), advanced joins, subqueries, performance optimization, stored procedures, transaction management, partitioning, pivoting, and advanced data types. It includes SQL queries and examples for each concept, demonstrating their application using a sample employee table and a projects table. Key topics covered include ranking functions, recursive CTEs, various types of joins, and the use of subqueries for complex queries.

Uploaded by

shivanshu soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 48

1.

Window Functions
* OVER, PARTITION BY, ORDER BY
* ROW_NUMBER(), RANK(), DENSE_RANK()
* LAG(), LEAD(), FIRST_VALUE(), LAST_VALUE()

2. Common Table Expressions (CTEs)


* WITH clause
* Recursive CTEs
* Multiple CTEs in a single query

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

6. Stored Procedures and Functions


* User-defined functions
* Triggers
* Exception handling
* Dynamic SQL

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

10. Advanced Data Types


* JSON operations
* XML handling
* Spatial data types
* User-defined types
========================================================================
========================================================================
================

This is the Sample Table Schema :

CREATE TABLE employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2),
hire_date DATE
);

-- Insert sample records


INSERT INTO employees VALUES
(1, 'John Doe', 'Sales', 60000, '2020-01-15'),
(2, 'Jane Smith', 'Sales', 65000, '2019-03-20'),
(3, 'Bob Wilson', 'Sales', 55000, '2021-05-10'),
(4, 'Alice Brown', 'HR', 62000, '2020-02-15'),
(5, 'Charlie Davis', 'HR', 58000, '2021-07-22'),
(6, 'Eva Green', 'IT', 72000, '2019-11-30'),
(7, 'Frank Miller', 'IT', 75000, '2018-08-12'),
(8, 'Grace Lee', 'IT', 70000, '2020-04-05');

Window Functions

ROW_NUMBER()

●​ Assigns a unique number to each row in a partition (grouped by department).

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():-

●​ Assigns a rank to rows in a window, but skips numbers for ties.


●​ Employees with the same salary get the same rank.
●​ Ranks skip numbers when there are ties.

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:-

NOTE:-Always Remember One thing inside over Partition is Optional.


5.LEAD()
Retrieves the value from the next row within the window.
Looks at the next row’s salary for each employee in the ordered window.
NULL appears if there is no next row.

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;

-- Update existing records to include manager_id


UPDATE employees SET manager_id = NULL WHERE emp_id IN (1, 4, 6); -- John Doe, Alice
Brown, Eva Green are managers

-- Assign managers to employees


UPDATE employees SET manager_id = 1 WHERE emp_id IN (2, 3); -- Jane Smith and Bob
Wilson report to John Doe
UPDATE employees SET manager_id = 4 WHERE emp_id = 5; -- Charlie Davis reports to Alice
Brown
UPDATE employees SET manager_id = 6 WHERE emp_id IN (7, 8); -- Frank Miller and Grace
Lee report to Eva Green;

2. Common Table Expressions (CTEs)


CTEs are temporary result sets that are defined within the scope of a query using the
WITH clause. They improve code readability and are particularly useful for simplifying
complex queries.

1. WITH Clause (Basic CTE)


The WITH clause allows you to define a temporary table that you can reference
later in your query.

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:

●​ The WITH clause creates a CTE named sales_employees.


●​ Inside the CTE, we filter for employees in the Sales department.
●​ In the main query, we use this CTE to further filter salaries greater than 60000.

2. Multiple CTEs in a Single Query

You can define multiple CTEs in a single query by separating them with commas.

Example:

Let’s create two CTEs:

●​ sales_cte: Employees in the Sales department.


●​ high_salary_cte: Employees with a salary greater than 65,000

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

--Comment:- Recursive member: Select employees reporting to the previous level


SELECT e.emp_id, e.emp_name, e.manager_id, eh.level + 1
FROM employees e
JOIN employee_hierarchy eh ON e.manager_id = eh.emp_id
)
SELECT emp_id, emp_name, manager_id, level
FROM employee_hierarchy
ORDER BY level, emp_id;

OUTPUT:-
Explanation

●​ Level 1: Top-level managers (manager_id IS NULL).


●​ Level 2: Employees who report to the top-level managers.
●​ The query recursively fetches employees reporting to the previous level.

========================================================================
========
New Table Added For Joins :-
CREATE TABLE projects (
project_id INT PRIMARY KEY,
project_name VARCHAR(50),
department VARCHAR(50),
project_lead_id INT
);

-- Insert sample data


INSERT INTO projects VALUES
(1, 'Project Alpha', 'Sales', 2), -- Jane Smith
(2, 'Project Beta', 'Sales', 3), -- Bob Wilson
(3, 'Project Gamma', 'HR', 4), -- Alice Brown
(4, 'Project Delta', 'IT', 6), -- Eva Green
(5, 'Project Omega', 'IT', NULL); -- No project lead

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:-

2. Left Join (or Left Outer Join)


It shows all rows from the left table and only the matching rows from the right table. If there’s no
match, the right table’s values will be NULL.
Example: Show all projects and their project leads, even if some projects don’t have a lead.
SQL_QUERY:-
SELECT
p.project_name,
e.emp_name AS project_lead
FROM
projects p
LEFT JOIN
employees e ON p.project_lead_id = e.emp_id;
OUTPUT:-
3. Right Join (or Right Outer Join)
It shows all rows from the right table and only the matching rows from the left table. If there’s no
match, the left table’s values will be NULL.
Example: Show all employees who lead projects, and include any unmatched projects.
SQL_QUERY:-
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:-

4. Full Outer Join


It combines all rows from both tables. If there’s no match, values from the other table will be
NULL.
Since MySQL does not support FULL OUTER JOIN directly, we can simulate it using UNION
Example: Show all employees and projects, even if they don’t match.
SQL_QUERY:-
SELECT
e.emp_name AS employee_name,
p.project_name
FROM
employees e
LEFT JOIN
projects p ON e.emp_id = p.project_lead_id
UNION

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:

●​ The subquery calculates the average salary for each department.


●​ For each employee in the outer query, it checks if their salary is greater than the average
salary of their department.

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.

Example: Find employees working in the same department as 'Jane Smith'

SQL_QUERY:-
SELECT emp_id, emp_name
FROM employees
WHERE department = (
SELECT department
FROM employees
WHERE emp_name = 'Jane Smith'
);

OUTPUT:-
Explanation:

●​ The subquery fetches the department of 'Jane Smith'.


●​ The outer query retrieves all employees from that department.

3. EXISTS and NOT EXISTS

These keywords are used to check if a subquery returns any rows.

Example: Find employees who lead a project


SQL_QUERY:-
SELECT emp_id, emp_name
FROM employees e
WHERE EXISTS (
SELECT 1
FROM projects p
WHERE p.project_lead_id = e.emp_id
);
OUTPUT:-

NOTE:-SELECT 1 is a common convention used in EXISTS subqueries where you don't


care about the actual values being returned - you just want to check for existence. The 1
is just a constant value and could actually be any number or value - it's essentially a
placeholder.

Why use SELECT 1?

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

Example: Find employees who do NOT lead any project


SQL_QUERY:-

SELECT emp_id, emp_name


FROM employees e
WHERE NOT EXISTS (
SELECT 1
FROM projects p
WHERE p.project_lead_id = e.emp_id
);

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.

4. ANY and ALL Operators

These are used to compare a value to a list of values returned by a subquery.

SQL_QUERY:-

SELECT emp_id, emp_name, salary


FROM employees
WHERE salary > ANY (
SELECT salary
FROM employees
WHERE department = 'IT'
);
OUTPUT:-
ALL Example: Find employees earning more than all employees in the 'HR' department
SQL_QUERY:-
SELECT emp_id, emp_name, salary
FROM employees
WHERE salary > ALL (
SELECT salary
FROM employees
WHERE department = 'HR'
);

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.

Here’s an explanation and examples of Subqueries with different types:

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:

●​ The subquery calculates the average salary for each department.


●​ For each employee in the outer query, it checks if their salary is greater than the average
salary of their department.

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.

Example: Find employees working in the same department as 'Jane Smith'


sql
=====
SELECT emp_id, emp_name
FROM employees
WHERE department = (
SELECT department
FROM employees
WHERE emp_name = 'Jane Smith'
);

Explanation:

●​ The subquery fetches the department of 'Jane Smith'.


●​ The outer query retrieves all employees from that department.

3. EXISTS and NOT EXISTS

These keywords are used to check if a subquery returns any rows.

Example: Find employees who lead a project


sql
=====
SELECT emp_id, emp_name
FROM employees e
WHERE EXISTS (
SELECT 1
FROM projects p
WHERE p.project_lead_id = e.emp_id
);

Example: Find employees who do NOT lead any project


sql
=====
SELECT emp_id, emp_name
FROM employees e
WHERE NOT EXISTS (
SELECT 1
FROM projects p
WHERE p.project_lead_id = e.emp_id
);

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.

4. ANY and ALL Operators

These are used to compare a value to a list of values returned by a subquery.

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.

ANY Condition is TRUE if it matches any Employees earning more than


one value in the subquery result. at least one employee in 'IT'.

ALL Condition is TRUE if it matches all Employees earning more than


values in the subquery result. all employees in 'HR'.
========================================================================
===
5. Performance Optimization
1. Index Optimization

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:

1.​ Single-Column Index: Optimizes queries filtering on one column.


2.​ Composite Index: Optimizes queries filtering on multiple columns.
3.​ Unique Index: Ensures column values are unique.

SQL_QUERY:-

-- Create an index on the `department` column


CREATE INDEX idx_department ON employees(department);

-- Query using the indexed column


SELECT emp_name, department
FROM employees
WHERE department = 'IT';

OUTPUT:-

2. Query Execution Plans

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.

How to View Execution Plan:

SQL_QUERY:-
-- Use EXPLAIN to get the query execution plan

EXPLAIN SELECT emp_name, department


FROM employees
WHERE department = 'IT';

OUTPUT:-

3. Statistics and Cardinality

Definition:​
Statistics help the SQL optimizer choose the best execution plan by estimating the number of
rows matching a query condition (cardinality).

Cardinality:

●​ High cardinality: Many unique values (e.g., emp_id).


●​ Low cardinality: Few unique values (e.g., department).

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.

Example: Create Materialized View


SQL_QUERY:-
-- Create a materialized view for department salaries
CREATE TABLE department_salary AS
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

-- Query the materialized view


SELECT department, avg_salary
FROM department_salary;

OUTPUT:-

Benefits:

●​ Speeds up repeated execution of complex queries (e.g., aggregations or joins).


●​ Avoids recomputation every time the view is queried.

Maintaining Materialized Views:

If the underlying data changes, the materialized view must be refreshed.

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

Index Speeds up data retrieval by Indexing department for quick


Optimization indexing frequently queried filtering in employee queries.
columns.

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.

Materialized Stores precomputed query Precomputing average salaries by


Views results to avoid department to avoid repeated
recomputation. aggregation on the employees table.
========================================================================
======
6. Stored Procedures and Functions

1. User-Defined Functions (UDFs)

Definition:

A user-defined function is a reusable piece of code that returns a value. It is often used for
calculations or transformations.

Example: Create a Function to Calculate Annual Bonus

SQL_QUERY:-

DELIMITER $$

CREATE FUNCTION calculate_bonus(salary DECIMAL(10, 2))

RETURNS DECIMAL(10, 2)
DETERMINISTIC

BEGIN

RETURN salary * 0.10; -- 10% of salary

END $$

DELIMITER ;

-- Use the function

SELECT emp_name, salary, calculate_bonus(salary) AS annual_bonus

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.

Example: Create a Procedure to Give Salary Raises

SQL_QUERY:-
DELIMITER $$

CREATE PROCEDURE raise_salary(IN emp_id INT, IN percent_increase DECIMAL(5,


2))

BEGIN

UPDATE employees

SET salary = salary + (salary * percent_increase / 100)

WHERE emp_id = emp_id;

END $$

DELIMITER ;

SET SQL_SAFE_UPDATES = 0;

-- Call the procedure

CALL raise_salary(1, 5); -- Increase salary of employee with ID 1 by 5%

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).

Example: Trigger to Log Salary Changes

SQL_QUERY:-

DELIMITER $$

CREATE TRIGGER log_salary_update

AFTER UPDATE ON employees

FOR EACH ROW

BEGIN

INSERT INTO salary_logs (emp_id, old_salary, new_salary, change_date)

VALUES (OLD.emp_id, OLD.salary, NEW.salary, NOW());

END $$

DELIMITER ;

OUTPUT:-

Explanation:

●​ OLD: Refers to the value before the update.


●​ NEW: Refers to the value after the update.
●​ Triggers ensure data integrity and automate logging or auditing.

4. Exception Handling

Definition:

Handle errors or exceptional cases in stored procedures or triggers using handlers.

Example: Exception Handling for Invalid Employee ID

SQL_QUERY:-

DELIMITER $$

CREATE PROCEDURE fetch_employee(IN emp_id INT)

BEGIN
DECLARE emp_not_found CONDITION FOR SQLSTATE '02000'; -- No rows found

DECLARE CONTINUE HANDLER FOR emp_not_found

SET @error_message = 'Employee not found!';

SELECT emp_name, department, salary

FROM employees

WHERE emp_id = emp_id;

END $$

DELIMITER ;

-- Call the procedure

CALL fetch_employee(999); -- Nonexistent ID

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.

Example: Execute Dynamic Query Based on Input Table Name


SQL_QUERY:-
DELIMITER $$

CREATE PROCEDURE dynamic_query(IN table_name VARCHAR(100))


BEGIN
SET @sql = CONCAT('SELECT * FROM ', table_name, ' LIMIT 5');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$

DELIMITER ;

-- Call the procedure


CALL dynamic_query('employees');

OUTPUT:-

Key Points:

●​ PREPARE and EXECUTE handle dynamic SQL.


●​ Useful for queries where table or column names are determined at runtime.

Summary Table
Feature Definition Example Use Case

User-Defined Returns a value and is used in Calculating annual bonuses.


Functions queries for calculations or
transformations.

Stored Encapsulates a sequence of SQL Giving a salary raise to an


Procedures statements for data manipulation or employee.
complex tasks.
Triggers Automatically executes code in Logging salary changes in a
response to table events like separate table.
INSERT, UPDATE, or DELETE.

Exception Catches and handles errors or Returning a custom message


Handling exceptional cases within stored when an employee ID is not
procedures or triggers. found.

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;

UPDATE employees SET salary = salary + 5000 WHERE emp_id = 1;

-- Simulate a failure

ROLLBACK; -- Undo the changes

-- Commit the transaction

COMMIT; -- Save changes permanently

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.

Example: Set Isolation Level


SQL_QUERY:-
-- Set isolation level to Repeatable Read
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

START TRANSACTION;

SELECT * FROM employees WHERE department = 'Sales';

-- Make changes during the transaction


UPDATE employees SET salary = salary + 1000 WHERE department = 'Sales';

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.

Techniques to Prevent Deadlocks:

1.​ Order Access to Resources: Access resources in a consistent order across


transactions.
2.​ Timeouts: Set a timeout for transactions waiting for locks.
3.​ Minimize Locking: Use appropriate isolation levels and avoid locking unnecessary
rows.

Example: Detect Deadlock in MySQL


SQL_QUERY:-
-- Transaction 1
START TRANSACTION;
UPDATE employees SET salary = salary + 1000 WHERE emp_id = 1;

-- Transaction 2
START TRANSACTION;
UPDATE employees SET salary = salary + 2000 WHERE emp_id = 2;

-- Transaction 1 tries to lock emp_id 2


-- Transaction 2 tries to lock emp_id 1
-- Deadlock occurs

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:

1.​ Dirty Reads: Reading uncommitted changes of another transaction.


2.​ Non-Repeatable Reads: A value read in a transaction changes due to another
transaction.
3.​ Phantom Reads: New rows added by another transaction appear in subsequent
reads.

Example: Concurrent Transactions Without Issues


SQL_QUERY:-
-- Transaction 1
START TRANSACTION;
SELECT salary FROM employees WHERE emp_id = 1;
UPDATE employees SET salary = salary + 5000 WHERE emp_id = 1;

-- 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

ACID Properties Ensures reliable transactions. Updating employee salaries


with rollback support in case
of failure.

Isolation Levels Controls the visibility of data Preventing dirty reads by


changes between transactions. using the Read Committed
isolation level.

Deadlock Techniques to avoid transactions Accessing resources in a


Prevention blocking each other indefinitely. consistent order to avoid
circular waits.

Concurrent Ensures data consistency and Reading and updating salaries


Transactions prevents issues during in a multi-user environment.
simultaneous transactions.
========================================================================
========================================================================
================
8. Partitioning

Definition:-Partitioning is a technique to split large tables into smaller, manageable pieces


(partitions) while keeping them in the same database. It helps improve query performance and
maintainability.

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.

Assign Department Codes


Before inserting data, use numeric department codes:

●​ 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

Range Splits table based on ranges of a Time-series data, salary-based


Partitioning column (e.g., date, salary). partitions.

List Divides table based on predefined Categories like department,


Partitioning values in a column. region.

Hash Uses a hash function to distribute Load balancing, when no


Partitioning rows evenly across partitions. natural partitioning key exists.

Partition Optimization technique that scans Queries on partitioned tables


Pruning only relevant partitions. with filters (WHERE clause).
========================================================================
=
SCHEMA FOR SALES TABLE:-
CREATE TABLE sales (
sale_id INT PRIMARY KEY AUTO_INCREMENT,
emp_id INT NOT NULL,
department VARCHAR(50) NOT NULL,
sales_month VARCHAR(10) NOT NULL,
sales_amount DECIMAL(10,2) NOT NULL
);
—----------------------------------------------------------------------------------------------------------------------------
---
INSERT INTO sales (emp_id, department, sales_month, sales_amount) VALUES
(1, 'Sales', 'Jan', 1000),
(1, 'Sales', 'Feb', 1500),
(1, 'Sales', 'Mar', 1800),
(2, 'HR', 'Jan', 1200),
(2, 'HR', 'Feb', 1600),
(2, 'HR', 'Mar', 1400),
(3, 'IT', 'Jan', 2000),
(3, 'IT', 'Feb', 2500),
(3, 'IT', 'Mar', 2200),
(4, 'Sales', 'Jan', 1300),
(4, 'Sales', 'Feb', 1700),
(4, 'Sales', 'Mar', 1900);

9. Pivot and Unpivot


Definition:-Pivoting is used to convert rows into columns, and unpivoting does the opposite
(converting columns into rows).
1. Pivot (Rows to Columns):-Transform row values into column headers to create a summary
table (cross-tabulation).

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.

2. Dynamic Pivoting (Using GROUP_CONCAT)


Definination:-If we don’t know all the possible months beforehand, we need a dynamic
query.
SQL_QUERY:-
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT CONCAT(
'SUM(CASE WHEN sales_month = "', sales_month, '" THEN sales_amount ELSE 0 END)
AS `', sales_month, '`'
)) INTO @sql
FROM sales;

SET @sql = CONCAT('SELECT emp_id, ', @sql, ' FROM sales GROUP BY emp_id;');

PREPARE stmt FROM @sql;


EXECUTE stmt;
DEALLOCATE PREPARE stmt;
OUTPUT:-

✅ Explanation:
●​ GROUP_CONCAT() dynamically generates column names.
●​ PREPARE and EXECUTE allow dynamic query execution.

3. Unpivot (Columns to Rows)


Definition:-Transform multiple columns into key-value pairs (normalize data).
Example: Unpivoting Manually Using UNION ALL
SQL_QUERY:-
SELECT emp_id, 'Jan' AS sales_month, Jan_Sales AS sales_amount FROM sales_pivoted
UNION ALL
SELECT emp_id, 'Feb' AS sales_month, Feb_Sales FROM sales_pivoted;

OUTPUT:-

✅ Explanation:
●​ UNION ALL is used to transform columns back into rows.

4. Cross Tabulation (Comparing Two Attributes)


Definition: Used to compare two categorical attributes in a matrix format.
SQL_QUERY:-
SELECT department,
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,
SUM(CASE WHEN sales_month = 'Mar' THEN sales_amount ELSE 0 END) AS March_Sales
FROM sales
GROUP BY department;

OUTPUT:-

✅ Explanation:
●​ This helps compare departments across different months.

📌 Key Takeaways
Concept Definition Method Used

Pivot Convert rows into columns CASE WHEN, SUM()

Dynamic Pivot Handle unknown column GROUP_CONCAT(),


values PREPARE

Unpivot Convert columns into rows UNION ALL

Cross Compare two categorical CASE WHEN with


Tabulation values SUM()
========================================================================
========================================================================
================

10. Advanced Data Types

1.JSON Operations (JavaScript Object Notation)


Definition:-Used for storing and querying structured data inside a single column.
📌 Creating a Table with a JSON Column
sql
=====
CREATE TABLE employees_json (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
details JSON
);

📌 Inserting JSON Data


sql
=====
INSERT INTO employees_json (emp_id, emp_name, details)
VALUES
(1, 'John Doe', '{"department": "Sales", "salary": 60000, "skills":
["Negotiation", "Marketing"]}'),
(2, 'Jane Smith', '{"department": "HR", "salary": 65000, "skills":
["Recruitment", "Training"]}');

📌 Querying JSON Data


sql
=====
-- Get department from JSON column
SELECT emp_name, details->>'$.department' AS department FROM
employees_json;

-- Get employees whose salary is above 60,000


SELECT emp_name FROM employees_json WHERE
JSON_UNQUOTE(details->>'$.salary') > 60000;

✅ MySQL JSON Functions:


●​ JSON_UNQUOTE() → Removes quotes from JSON values
●​ JSON_ARRAY() → Creates a JSON array
●​ JSON_OBJECT() → Creates a JSON object
●​ JSON_CONTAINS() → Checks if JSON contains a value
Output :-

========================================================================
========

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.

📌 Storing XML in MySQL


sql
=====
CREATE TABLE employees_xml (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
details XML
);

📌 Inserting XML Data


sql
=====
INSERT INTO employees_xml (emp_id, emp_name, details)
VALUES
(1, 'John Doe',
'<employee><department>Sales</department><salary>60000</salary></emplo
yee>');
📌 Extracting XML Data (Workaround Using String Functions)
Since MySQL lacks XML querying functions, you must use LIKE or SUBSTRING_INDEX().

sql
=====
SELECT emp_name, SUBSTRING_INDEX(SUBSTRING_INDEX(details,
'<department>', -1), '</department>', 1) AS department
FROM employees_xml;

✅ Other Databases with Native XML Support:


●​ PostgreSQL: xpath() function
●​ SQL Server: FOR XML PATH()
●​ Oracle: XMLTABLE()

3.Spatial Data Types (GIS)


Definition:-Used for geographic and geometric data (latitudes, longitudes, shapes, etc.)

📌 Creating a Table with Spatial Data


sql
=====
CREATE TABLE locations (
loc_id INT PRIMARY KEY,
loc_name VARCHAR(100),
coordinates POINT NOT NULL,
SPATIAL INDEX (coordinates) -- Improves query performance
);

📌 Inserting Geographic Data


sql
=====
INSERT INTO locations (loc_id, loc_name, coordinates)
VALUES
(1, 'New York', ST_GeomFromText('POINT(40.7128 -74.0060)')),
(2, 'San Francisco', ST_GeomFromText('POINT(37.7749 -122.4194)'));
📌 Querying Spatial Data
sql

-- Find all locations within 500 km of a point (Example: New York)


SELECT loc_name FROM locations
WHERE ST_Distance_Sphere(coordinates, ST_GeomFromText('POINT(40.7128
-74.0060)')) < 500000;

✅ MySQL Spatial Functions:


●​ ST_GeomFromText('POINT(x y)') → Convert text to spatial
●​ ST_Distance_Sphere() → Calculate distance between two points
●​ ST_Contains() → Check if a shape contains a point

Output:-

4.User-Defined Types (UDTs)

📌 MySQL does NOT support user-defined types like PostgreSQL or SQL Server.​
However, you can simulate them using ENUM, SET, or JSON.

📌 Example: Using ENUM for a Custom Type


sql

CREATE TABLE employees_enum (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
emp_role ENUM('Manager', 'Developer', 'Analyst') NOT NULL
);

📌 Inserting Data
sql
INSERT INTO employees_enum (emp_id, emp_name, emp_role)
VALUES
(1, 'John Doe', 'Manager'),
(2, 'Jane Smith', 'Developer');

📌 Querying ENUM Values


sql

SELECT emp_name FROM employees_enum WHERE emp_role = 'Manager';

✅ Alternative in MySQL:
●​ Use JSON for flexible custom data
●​ Use ENUM for fixed values (best for roles, categories)

Output:-

You might also like