{"id":1587,"date":"2019-03-16T09:11:59","date_gmt":"2019-03-16T02:11:59","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=1587"},"modified":"2020-04-11T20:12:32","modified_gmt":"2020-04-11T13:12:32","slug":"sql-server-cte","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/","title":{"rendered":"SQL Server CTE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the common table expression or CTE in SQL Server by using the <code>WITH<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-cte-in-sql-server'>Introduction to CTE in SQL Server <a href=\"#introduction-to-cte-in-sql-server\" class=\"anchor\" id=\"introduction-to-cte-in-sql-server\" title=\"Anchor for Introduction to CTE in SQL Server\">#<\/a><\/h2>\n\n\n\n<p>CTE stands for common table expression. A CTE allows you to define a temporary named result set that available temporarily in the execution scope of a statement such as <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code>, <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-insert\/\">INSERT<\/a><\/code>, <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-update\/\">UPDATE<\/a><\/code>, <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-delete\/\">DELETE<\/a><\/code>, or <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-merge\/\">MERGE<\/a><\/code>.<\/p>\n\n\n\n<p>The following shows the common syntax of a CTE in SQL Server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> expression_name&#91;(column_name &#91;,...])]\n<span class=\"hljs-keyword\">AS<\/span>\n    (CTE_definition)\nSQL_statement;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, specify the expression name (<code>expression_name<\/code>) to which you can refer later in a query.<\/li><li>Next, specify a list of comma-separated columns after the expression_name. The number of columns must be the same as the number of columns defined in the <code>CTE_definition<\/code>.<\/li><li>Then, use the AS keyword after the expression name or column list if the column list is specified.<\/li><li>After, define a <code>SELECT<\/code> statement whose result set populates the common table expression.<\/li><li>Finally, refer to the common table expression in a query (<code>SQL_statement<\/code>) such as <code>SELECT<\/code>, <code>INSERT<\/code>, <code>UPDATE<\/code>, <code>DELETE<\/code>, or <code>MERGE<\/code>.<\/li><\/ul>\n\n\n\n<p>We prefer to use common table expressions rather than to use subqueries because common table expressions are more readable. We also use CTE in the queries that contain <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-window-functions\/\">analytic functions<\/a> (or <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-window-functions\/\">window functions<\/a>)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-cte-examples'>SQL Server CTE examples <a href=\"#sql-server-cte-examples\" class=\"anchor\" id=\"sql-server-cte-examples\" title=\"Anchor for SQL Server CTE examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using common table expressions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='a-simple-sql-server-cte-example'>A) Simple SQL Server CTE example <a href=\"#a-simple-sql-server-cte-example\" class=\"anchor\" id=\"a-simple-sql-server-cte-example\" title=\"Anchor for A) Simple SQL Server CTE example\">#<\/a><\/h3>\n\n\n\n<p>This query uses a CTE to return the sales amounts by sales staffs in 2018:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> cte_sales_amounts (staff, sales, <span class=\"hljs-keyword\">year<\/span>) <span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span>    \n        first_name + <span class=\"hljs-string\">' '<\/span> + last_name, \n        <span class=\"hljs-keyword\">SUM<\/span>(quantity * list_price * (<span class=\"hljs-number\">1<\/span> - discount)),\n        <span class=\"hljs-keyword\">YEAR<\/span>(order_date)\n    <span class=\"hljs-keyword\">FROM<\/span>    \n        sales.orders o\n    <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.order_items i <span class=\"hljs-keyword\">ON<\/span> i.order_id = o.order_id\n    <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.staffs s <span class=\"hljs-keyword\">ON<\/span> s.staff_id = o.staff_id\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n        first_name + <span class=\"hljs-string\">' '<\/span> + last_name,\n        <span class=\"hljs-keyword\">year<\/span>(order_date)\n)\n\n<span class=\"hljs-keyword\">SELECT<\/span>\n    staff, \n    sales\n<span class=\"hljs-keyword\">FROM<\/span> \n    cte_sales_amounts\n<span class=\"hljs-keyword\">WHERE<\/span>\n    <span class=\"hljs-keyword\">year<\/span> = <span class=\"hljs-number\">2018<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following picture shows the result set:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"184\" height=\"132\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-example.png\" alt=\"SQL Server CTE example\" class=\"wp-image-1589\"\/><\/figure>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, we defined <code>cte_sales_amounts<\/code> as the name of the common table expression. the CTE returns a result that that consists of three columns <code>staff<\/code>, <code>year<\/code>, and <code>sales<\/code> derived from the definition query.<\/li><li>Second, we constructed a query that returns the total sales amount by sales staff and year by querying data from the <code>orders<\/code>, <code>order_items<\/code> and <code>staffs<\/code> tables.<\/li><li>Third, we referred to the CTE in the outer query and select only the rows whose year are 2018.<\/li><\/ul>\n\n\n\n<p>Noted that this example is solely for the demonstration purpose to help you gradually understand how common table expressions work. There is a more optimal way to achieve the result without using CTE.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='b-using-a-common-table-expression-to-make-report-averages-based-on-counts'>B) Using a common table expression to make report averages based on counts <a href=\"#b-using-a-common-table-expression-to-make-report-averages-based-on-counts\" class=\"anchor\" id=\"b-using-a-common-table-expression-to-make-report-averages-based-on-counts\" title=\"Anchor for B) Using a common table expression to make report averages based on counts\">#<\/a><\/h3>\n\n\n\n<p>This example uses the CTE to return the average number of sales orders in 2018 for all sales staffs.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> cte_sales <span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        staff_id, \n        <span class=\"hljs-keyword\">COUNT<\/span>(*) order_count  \n    <span class=\"hljs-keyword\">FROM<\/span>\n        sales.orders\n    <span class=\"hljs-keyword\">WHERE<\/span> \n        <span class=\"hljs-keyword\">YEAR<\/span>(order_date) = <span class=\"hljs-number\">2018<\/span>\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n        staff_id\n\n)\n<span class=\"hljs-keyword\">SELECT<\/span>\n    <span class=\"hljs-keyword\">AVG<\/span>(order_count) average_orders_by_staff\n<span class=\"hljs-keyword\">FROM<\/span> \n    cte_sales;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">average_orders_by_staff\n<span class=\"hljs-comment\">-----------------------<\/span>\n48\n\n(1 row affected)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example:<\/p>\n\n\n\n<p>First, we used <code>cte_sales<\/code> as the name of the common table expression. We skipped the column list of the CTE so it is derived from the CTE definition statement. In this example, it includes <code>staff_id<\/code> and <code>order_count<\/code> columns.<\/p>\n\n\n\n<p>Second, we use the following query to define the result set that populates the common table expression <code>cte_sales<\/code>. The query returns the number of orders in 2018 by sales staff.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>    \n    staff_id, \n    <span class=\"hljs-keyword\">COUNT<\/span>(*) order_count\n<span class=\"hljs-keyword\">FROM<\/span>    \n    sales.orders\n<span class=\"hljs-keyword\">WHERE<\/span> \n    <span class=\"hljs-keyword\">YEAR<\/span>(order_date) = <span class=\"hljs-number\">2018<\/span>\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    staff_id;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, we refer to the <code>cte_sales<\/code> in the outer statement and use the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-avg\/\">AVG()<\/a><\/code> function to get the average sales order by all staffs.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    <span class=\"hljs-keyword\">AVG<\/span>(order_count) average_orders_by_staff\n<span class=\"hljs-keyword\">FROM<\/span> \n    cte_sales;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='c-using-multiple-sql-server-cte-in-a-single-query-example'>C) Using multiple SQL Server CTE in a single query example <a href=\"#c-using-multiple-sql-server-cte-in-a-single-query-example\" class=\"anchor\" id=\"c-using-multiple-sql-server-cte-in-a-single-query-example\" title=\"Anchor for C) Using multiple SQL Server CTE in a single query example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses two CTE <code>cte_category_counts<\/code> and <code>cte_category_sales<\/code> to return the number of the products and sales for each product category. The outer query <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-joins\/\">joins<\/a> two CTEs using the <code>category_id<\/code> column.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> cte_category_counts (\n    category_id, \n    category_name, \n    product_count\n)\n<span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        c.category_id, \n        c.category_name, \n        <span class=\"hljs-keyword\">COUNT<\/span>(p.product_id)\n    <span class=\"hljs-keyword\">FROM<\/span> \n        production.products p\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> production.categories c \n            <span class=\"hljs-keyword\">ON<\/span> c.category_id = p.category_id\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n        c.category_id, \n        c.category_name\n),\ncte_category_sales(category_id, sales) <span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span>    \n        p.category_id, \n        <span class=\"hljs-keyword\">SUM<\/span>(i.quantity * i.list_price * (<span class=\"hljs-number\">1<\/span> - i.discount))\n    <span class=\"hljs-keyword\">FROM<\/span>    \n        sales.order_items i\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> production.products p \n            <span class=\"hljs-keyword\">ON<\/span> p.product_id = i.product_id\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.orders o \n            <span class=\"hljs-keyword\">ON<\/span> o.order_id = i.order_id\n    <span class=\"hljs-keyword\">WHERE<\/span> order_status = <span class=\"hljs-number\">4<\/span> <span class=\"hljs-comment\">-- completed<\/span>\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n        p.category_id\n) \n\n<span class=\"hljs-keyword\">SELECT<\/span> \n    c.category_id, \n    c.category_name, \n    c.product_count, \n    s.sales\n<span class=\"hljs-keyword\">FROM<\/span>\n    cte_category_counts c\n    <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> cte_category_sales s \n        <span class=\"hljs-keyword\">ON<\/span> s.category_id = c.category_id\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    c.category_name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the result set:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"361\" height=\"153\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-join-two-CTEs-example.png\" alt=\"SQL Server CTE join two CTEs example\" class=\"wp-image-1588\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-join-two-CTEs-example.png 361w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-join-two-CTEs-example-300x127.png 300w\" sizes=\"auto, (max-width: 361px) 100vw, 361px\" \/><\/figure>\n\n\n\n<p>In this tutorial, you have learned how to use common table expressions or CTE in SQL Server to construct complex queries in an easy-to-understand manner.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1587\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/\"\n\t\t\t\tdata-post-title=\"SQL Server CTE\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1587\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/\"\n\t\t\t\tdata-post-title=\"SQL Server CTE\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the common table expressions or CTE in SQL Server to construct complex queries in an easy-to-understand manner.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":100,"menu_order":43,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1587","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Mastering Common Table Expression or CTE in SQL Server<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the common table expressions or CTE in SQL Server to construct complex queries in an easy-to-understand manner.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mastering Common Table Expression or CTE in SQL Server\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the common table expressions or CTE in SQL Server to construct complex queries in an easy-to-understand manner.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-11T13:12:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-example.png\" \/>\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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cte\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cte\\\/\",\"name\":\"Mastering Common Table Expression or CTE in SQL Server\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cte\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cte\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-CTE-example.png\",\"datePublished\":\"2019-03-16T02:11:59+00:00\",\"dateModified\":\"2020-04-11T13:12:32+00:00\",\"description\":\"This tutorial shows you how to use the common table expressions or CTE in SQL Server to construct complex queries in an easy-to-understand manner.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cte\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cte\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cte\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-CTE-example.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-CTE-example.png\",\"width\":184,\"height\":132,\"caption\":\"SQL Server CTE example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cte\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Basics\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server CTE\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?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":"Mastering Common Table Expression or CTE in SQL Server","description":"This tutorial shows you how to use the common table expressions or CTE in SQL Server to construct complex queries in an easy-to-understand manner.","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:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/","og_locale":"en_US","og_type":"article","og_title":"Mastering Common Table Expression or CTE in SQL Server","og_description":"This tutorial shows you how to use the common table expressions or CTE in SQL Server to construct complex queries in an easy-to-understand manner.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2020-04-11T13:12:32+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-example.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/","name":"Mastering Common Table Expression or CTE in SQL Server","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-example.png","datePublished":"2019-03-16T02:11:59+00:00","dateModified":"2020-04-11T13:12:32+00:00","description":"This tutorial shows you how to use the common table expressions or CTE in SQL Server to construct complex queries in an easy-to-understand manner.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-example.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-CTE-example.png","width":184,"height":132,"caption":"SQL Server CTE example"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cte\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Basics","item":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/"},{"@type":"ListItem","position":3,"name":"SQL Server CTE"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/1587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=1587"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/1587\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/100"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}