- SQL Basics
-
Overview
- Introduction to Databases and SQL
- Creating and Managing Tables in SQL
- Data Types and Constraints
- SELECT Queries and Filtering Data
- GROUP BY, HAVING, and ORDER BY
- SQL Joins (INNER, LEFT, RIGHT, FULL)
- Subqueries and Nested Queries
- UNION, INTERSECT, and EXCEPT
- Common Table Expressions (CTE)
- SQL Views and Stored Procedures
Common Table Expressions (CTE)
Add to BookmarkIntroduction to CTE
Common Table Expressions (CTE) in SQL provide a temporary result set that can be referenced within a SELECT
, INSERT
, UPDATE
, or DELETE
statement. CTEs improve readability and maintainability, making complex queries easier to manage.
Why Use CTEs?
- Improves Query Readability: Organizes complex queries into logical sections.
- Enhances Code Reusability: A CTE can be referenced multiple times within a query.
- Self-Referencing (Recursive CTEs): Enables hierarchical queries like organizational structures.
- Simplifies Queries: Reduces the need for nested subqueries.
Syntax of CTE
A CTE is defined using the WITH
keyword, followed by the CTE name and the query.
WITH CTE_Name (column1, column2, ...)
AS (
SELECT column1, column2, ... FROM table_name WHERE condition
)
SELECT * FROM CTE_Name;
Example of a Simple CTE
Let’s consider an employees
table with the following columns: id
, name
, department
, and salary
.
WITH HighSalaryEmployees AS (
SELECT name, department, salary
FROM employees
WHERE salary > 50000
)
SELECT * FROM HighSalaryEmployees;
This query selects employees earning more than 50,000 and stores them in a temporary result set called HighSalaryEmployees
.
CTE vs Subquery
Feature | CTE | Subquery |
---|---|---|
Readability | More readable | Less readable in complex queries |
Reusability | Can be referenced multiple times | Cannot be reused |
Performance | Better in complex queries | May slow down execution |
When to Use CTEs
- When dealing with complex queries requiring multiple references to the same subquery.
- When working with hierarchical or recursive data.
- When breaking down long queries into manageable sections.
Conclusion
CTEs offer a powerful way to simplify and optimize SQL queries. By making code more readable and reusable, they are an essential tool for SQL developers dealing with complex data operations. Understanding both simple and recursive CTEs will help in handling advanced query requirements effectively.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Generative AI - The Future of Artificial Intelligence
- Grow your business with Facebook Marketing
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Datasets for Exploratory Data Analysis for Beginners
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- AI Agents & Autonomous Systems – The Future of Self-Driven Intelligence
- How AI is Making Humans Weaker – The Hidden Impact of Artificial Intelligence
- Navigating AI Careers in 2025: Data Science, Machine Learning, Deep Learning, and More
- Data Analytics: The Power of Data-Driven Decision Making
- The Ultimate Guide to Data Science: Everything You Need to Know
- Top 10 Knowledge for Machine Learning & Data Science Students
- The Beginner’s Guide to Normalization and Denormalization in Databases
- Top 15 Recommended SEO Tools
- Avoiding the Beginner’s Trap: Key Python Fundamentals You Shouldn't Skip
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset