0% found this document useful (0 votes)
2 views

Advanced_SQL_Training_Phase2 (1)

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

Advanced_SQL_Training_Phase2 (1)

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

Advanced SQL Training Document: Phase 2

Table of Contents

1. Subqueries
- Definition
- Types
- Practical Examples
2. Correlated Subqueries
- Definition
- Practical Examples
3. Recursive Queries
- Definition
- Practical Examples
4. Indexing
- Types of Indexes
- Best Practices
- Practical Examples
5. Query Execution Plans
- Understanding Execution Plans
- Tools to Analyze Plans
6. Performance Tuning
- Tips and Best Practices
- Common Pitfalls
7. Partial Query Selection
- Definition
- Practical Examples

1. Subqueries

Definition
A subquery is a query nested inside another query. It provides results to the outer query
and is often used in WHERE, FROM, or SELECT clauses.

Types

1. Single-row Subqueries: Returns a single row and is used with operators like =, <, >.
2. Multi-row Subqueries: Returns multiple rows and is used with operators like IN, ANY,
ALL.
3. Scalar Subqueries: Returns a single value and is used in the SELECT clause.
Example
Find employees earning more than the average salary.

SELECT name, salary


FROM Employees
WHERE salary > (SELECT AVG(salary) FROM Employees);

2. Correlated Subqueries

Definition
A correlated subquery depends on the outer query for its values. It is executed once for
every row processed by the outer query.

Example
Find customers who have placed orders exceeding their average order amount.

SELECT customer_id, order_id, amount


FROM Orders o1
WHERE amount > (
SELECT AVG(amount)
FROM Orders o2
WHERE o1.customer_id = o2.customer_id
);

3. Recursive Queries

Definition
Recursive queries are used to query hierarchical data, such as organizational structures or
category trees. It uses a Common Table Expression (CTE) with recursive logic.

Example
Fetch all employees reporting (directly or indirectly) to a specific manager.

WITH RECURSIVE EmployeeHierarchy AS (


SELECT employee_id, name, manager_id
FROM Employees
WHERE manager_id IS NULL -- Start with top-level manager
UNION ALL
SELECT e.employee_id, e.name, e.manager_id
FROM Employees e
INNER JOIN EmployeeHierarchy eh ON e.manager_id = eh.employee_id
)
SELECT * FROM EmployeeHierarchy;

4. Indexing

Types of Indexes

1. Clustered Index: Sorts the data in the table based on key values.
2. Non-Clustered Index: Creates a separate structure for quick lookups.
3. Composite Index: Combines multiple columns into a single index.
4. Unique Index: Ensures all values in a column are unique.

Best Practices

- Use indexes on columns frequently used in WHERE, JOIN, and GROUP BY.
- Avoid indexing small tables.
- Limit the number of indexes on write-heavy tables to minimize overhead.

Example
Create an index to optimize searches by customer_id.

CREATE INDEX idx_customer_id ON Orders(customer_id);

5. Query Execution Plans

Understanding Execution Plans


An execution plan is a breakdown of how the database executes a query. It helps identify
bottlenecks and optimize queries.

Tools to Analyze Plans

1. MySQL: Use EXPLAIN or EXPLAIN ANALYZE.


2. PostgreSQL: Use EXPLAIN or EXPLAIN ANALYZE.
3. SQL Server: Use Query Analyzer or Execution Plan viewer.

Example
Analyze the execution plan for a query.
EXPLAIN SELECT * FROM Orders WHERE amount > 1000;

6. Performance Tuning

Tips and Best Practices

1. Avoid SELECT *; fetch only required columns.


2. Use appropriate data types for columns.
3. Optimize joins using indexed columns.
4. Use caching for frequently executed queries.
5. Analyze and tune queries using execution plans.

Common Pitfalls

- Missing indexes on key columns.


- Overuse of correlated subqueries.
- Performing operations on indexed columns in WHERE.

7. Partial Query Selection

Definition
Partial query selection retrieves a subset of data based on specific conditions, often used
with LIMIT, OFFSET, or window functions.

Example
Fetch the top 5 highest-paying customers.

SELECT customer_id, SUM(amount) AS total_spent


FROM Orders
GROUP BY customer_id
ORDER BY total_spent DESC
LIMIT 5;

Paginate order results for customer reviews.

SELECT order_id, item, amount


FROM Orders
ORDER BY order_id
LIMIT 10 OFFSET 20; -- Fetch 10 rows starting from the 21st row

You might also like