filmov
tv
How to write SQL CTEs

Показать описание
Learn how to write SQL SUBQUERIES in less than 30 minutes
#sqlsubqueries
#sqlqueries
#sqlpractice
#groupby
#sqlhaving
#dataanalysis
#dataanalyst
#sql
**0:01 - Introduction**
- Welcome back to the channel
- Overview: Transition from subqueries to CTEs
- Brief recap of subqueries and their limitations
**0:22 - What Are CTEs?**
- Definition: CTEs simplify complex queries by replacing nested subqueries
- Key benefits: Cleaner code, reusability, and improved readability
- Comparison to subqueries: CTEs act as named temporary result sets
**0:53 - Example 1: Simple CTE for Longest Book**
- Problem: Find the title and page count of the longest book
- **Subquery Approach**:
- Inner query to find `MAX(page_count)`
- Outer query to filter books matching the max page count
- **CTE Approach**:
- `WITH max_page AS (SELECT MAX(page_count) AS max FROM books)`
- Join `max_page` CTE with the books table to retrieve the title
**4:26 - CTE Syntax & Execution**
- Syntax breakdown: `WITH cte_name AS (query) SELECT ...`
- Execution flow: CTE runs first, then the main query references it
**7:30 - Example 2: Advanced CTE for Longest Book by Genre**
- Problem: Find the longest book title for each genre
#sqlsubqueries
#sqlqueries
#sqlpractice
#groupby
#sqlhaving
#dataanalysis
#dataanalyst
#sql
**0:01 - Introduction**
- Welcome back to the channel
- Overview: Transition from subqueries to CTEs
- Brief recap of subqueries and their limitations
**0:22 - What Are CTEs?**
- Definition: CTEs simplify complex queries by replacing nested subqueries
- Key benefits: Cleaner code, reusability, and improved readability
- Comparison to subqueries: CTEs act as named temporary result sets
**0:53 - Example 1: Simple CTE for Longest Book**
- Problem: Find the title and page count of the longest book
- **Subquery Approach**:
- Inner query to find `MAX(page_count)`
- Outer query to filter books matching the max page count
- **CTE Approach**:
- `WITH max_page AS (SELECT MAX(page_count) AS max FROM books)`
- Join `max_page` CTE with the books table to retrieve the title
**4:26 - CTE Syntax & Execution**
- Syntax breakdown: `WITH cte_name AS (query) SELECT ...`
- Execution flow: CTE runs first, then the main query references it
**7:30 - Example 2: Advanced CTE for Longest Book by Genre**
- Problem: Find the longest book title for each genre