{"id":84,"date":"2026-03-06T06:37:53","date_gmt":"2026-03-06T06:37:53","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=84"},"modified":"2026-03-16T18:51:25","modified_gmt":"2026-03-16T18:51:25","slug":"common-table-expressions-cte","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/","title":{"rendered":"\u00a0Common Table Expressions (CTE)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A <strong>Common Table Expression (CTE)<\/strong> is a temporary result set in SQL that you can reference within a <code>SELECT<\/code>, <code>INSERT<\/code>, <code>UPDATE<\/code>, or <code>DELETE<\/code> statement. CTEs improve query readability, modularity, and allow recursive operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CTEs are often used to simplify complex joins, subqueries, or recursive queries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The basic syntax of a CTE is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">WITH cte_name (column1, column2, ...)<br>AS<br>(<br>    SQL query<br>)<br>SELECT *<br>FROM cte_name;<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cte_name<\/code>: Name you give to the CTE.<\/li>\n\n\n\n<li><code>column1, column2<\/code>: Optional column names for the CTE.<\/li>\n\n\n\n<li><code>SQL query<\/code>: The query whose results will be stored in the CTE.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Key Features<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Improves readability of complex SQL queries.<\/li>\n\n\n\n<li>Can be recursive for hierarchical data.<\/li>\n\n\n\n<li>Temporary in scope; only exists during the execution of the query.<\/li>\n\n\n\n<li>Can be used multiple times in the same query.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Simple CTE<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we have an <code>Employees<\/code> table. We want to select employees with a salary above 50000.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">WITH HighSalary AS<br>(<br>    SELECT EmployeeID, Name, Salary<br>    FROM Employees<br>    WHERE Salary &gt; 50000<br>)<br>SELECT *<br>FROM HighSalary;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2: CTE with Aggregation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Calculate the total salary for each department.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">WITH DeptSalary AS<br>(<br>    SELECT DepartmentID, SUM(Salary) AS TotalSalary<br>    FROM Employees<br>    GROUP BY DepartmentID<br>)<br>SELECT *<br>FROM DeptSalary;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example 3: Recursive CTE<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">List all employees and their managers in a hierarchy.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">WITH EmployeeHierarchy AS<br>(<br>    SELECT EmployeeID, Name, ManagerID<br>    FROM Employees<br>    WHERE ManagerID IS NULL    UNION ALL    SELECT e.EmployeeID, e.Name, e.ManagerID<br>    FROM Employees e<br>    INNER JOIN EmployeeHierarchy eh<br>    ON e.ManagerID = eh.EmployeeID<br>)<br>SELECT *<br>FROM EmployeeHierarchy;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use meaningful names for CTEs.<\/li>\n\n\n\n<li>Keep CTEs simple; avoid very large or unnecessary CTEs.<\/li>\n\n\n\n<li>Use recursive CTEs only when necessary for hierarchical data.<\/li>\n\n\n\n<li>Consider performance, as CTEs are not indexed and may impact execution time for large datasets.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CTEs are a powerful SQL feature for breaking down complex queries into readable, reusable components. They are particularly useful for hierarchical data and modular query design.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/sql\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate SQL for Data Professionals (SQL-201) > Advanced Joins &#038; Subqueries > Derived Tables<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773570002972\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":32,"template":"","class_list":["post-84","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0Common Table Expressions (CTE) - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"&quot;Learn SQL CTEs with examples, including recursive and aggregated queries. Improve query readability and simplify complex SQL tasks\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Common Table Expressions (CTE) - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"&quot;Learn SQL CTEs with examples, including recursive and aggregated queries. Improve query readability and simplify complex SQL tasks\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:51:25+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/common-table-expressions-cte\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/common-table-expressions-cte\\\/\",\"name\":\"\u00a0Common Table Expressions (CTE) - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T06:37:53+00:00\",\"dateModified\":\"2026-03-16T18:51:25+00:00\",\"description\":\"\\\"Learn SQL CTEs with examples, including recursive and aggregated queries. Improve query readability and simplify complex SQL tasks\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/common-table-expressions-cte\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/common-table-expressions-cte\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/common-table-expressions-cte\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate SQL for Data Professionals (SQL-201) > Advanced Joins & Subqueries > Derived Tables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\",\"name\":\"SQL Learning Hub\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00a0Common Table Expressions (CTE) - SQL Learning Hub","description":"\"Learn SQL CTEs with examples, including recursive and aggregated queries. Improve query readability and simplify complex SQL tasks","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Common Table Expressions (CTE) - SQL Learning Hub","og_description":"\"Learn SQL CTEs with examples, including recursive and aggregated queries. Improve query readability and simplify complex SQL tasks","og_url":"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:51:25+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/","url":"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/","name":"\u00a0Common Table Expressions (CTE) - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T06:37:53+00:00","dateModified":"2026-03-16T18:51:25+00:00","description":"\"Learn SQL CTEs with examples, including recursive and aggregated queries. Improve query readability and simplify complex SQL tasks","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/common-table-expressions-cte\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/sql\/"},{"@type":"ListItem","position":2,"name":"Intermediate SQL for Data Professionals (SQL-201) > Advanced Joins & Subqueries > Derived Tables"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/sql\/#website","url":"https:\/\/gigz.pk\/sql\/","name":"SQL Learning Hub","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/sql\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/media?parent=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}