{"id":207,"date":"2023-11-15T11:18:57","date_gmt":"2023-11-15T10:18:57","guid":{"rendered":"https:\/\/bitwise.exposed\/?p=207"},"modified":"2023-11-15T13:45:54","modified_gmt":"2023-11-15T12:45:54","slug":"common-table-expressions","status":"publish","type":"post","link":"https:\/\/bitwise.exposed\/index.php\/2023\/11\/15\/common-table-expressions\/","title":{"rendered":"Common Table Expressions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Common Table Expressions (CTEs) are a feature in SQL that allows you to define a temporary result set within a SELECT, INSERT, UPDATE, or DELETE statement. The basic syntax for CTEs is quite consistent across different SQL dialects, but there can be some variations and differences. I&#8217;ll provide a general overview, but keep in mind that specifics can vary.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Here is a basic template for a CTE:<\/h5>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">SQL<\/span><span role=\"button\" tabindex=\"0\" data-code=\"WITH cte_name (column1, column2, ...) AS (\n    -- CTE query here\n    SELECT column1, column2, ...\n    FROM your_table\n    WHERE some_condition\n)\n-- The main query using the CTE\nSELECT *\nFROM cte_name;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">WITH<\/span><span style=\"color: #000000\"> cte_name (column1, column2, ...) <\/span><span style=\"color: #0000FF\">AS<\/span><span style=\"color: #000000\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #008000\">-- CTE query here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">SELECT<\/span><span style=\"color: #000000\"> column1, column2, ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">FROM<\/span><span style=\"color: #000000\"> your_table<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">WHERE<\/span><span style=\"color: #000000\"> some_condition<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">-- The main query using the CTE<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">SELECT<\/span><span style=\"color: #000000\"> *<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">FROM<\/span><span style=\"color: #000000\"> cte_name;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some points to consider:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>WITH Clause:<\/strong><br>In most SQL dialects, you use the WITH keyword to introduce the CTE. Some databases require the RECURSIVE keyword for recursive CTEs.<\/li>\n\n\n\n<li><strong>CTE Name and Columns:<\/strong><br>You can optionally provide a name for your CTE (cte_name in the example) and explicitly list the columns. This is optional in some databases.<\/li>\n\n\n\n<li><strong>AS Clause:<\/strong><br>The AS keyword is used to define the CTE.<\/li>\n\n\n\n<li><strong>Main Query:<\/strong><br>After the CTE definition, you can use it in the main query. The CTE is referenced by its name.<\/li>\n<\/ol>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Here&#8217;s an example of a simple CTE:<\/h5>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">SQL<\/span><span role=\"button\" tabindex=\"0\" data-code=\"WITH cte_example AS (\n    SELECT id, name\n    FROM your_table\n    WHERE some_condition\n)\nSELECT *\nFROM cte_example;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">WITH<\/span><span style=\"color: #000000\"> cte_example <\/span><span style=\"color: #0000FF\">AS<\/span><span style=\"color: #000000\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">SELECT<\/span><span style=\"color: #000000\"> id, <\/span><span style=\"color: #0000FF\">name<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">FROM<\/span><span style=\"color: #000000\"> your_table<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">WHERE<\/span><span style=\"color: #000000\"> some_condition<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">SELECT<\/span><span style=\"color: #000000\"> *<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">FROM<\/span><span style=\"color: #000000\"> cte_example;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Remember that while the basic structure is similar, there might be differences in more advanced features or options, especially when dealing with recursive CTEs, materialized views, or optimizer hints. Always refer to the documentation of the specific database you are working with for precise details and potential variations. Common SQL databases include PostgreSQL, MySQL, SQL Server, and Oracle, each with its own nuances.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Links to documentations<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/\">SQL Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.postgresql.org\/docs\/\">PostgreSQL<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.oracle.com\/en\/database\/\">Oracle<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/\">MySQL<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These links will take you to the official documentation websites where you can find detailed information about the syntax, features, and best practices for each database. Keep in mind that the information available in documentation may vary, so always refer to the documentation version that corresponds to the version of the database you are using.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CTEs to organize query structure, making code cleaner and easier to read.<\/p>\n","protected":false},"author":1,"featured_media":209,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,17],"tags":[],"class_list":["post-207","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pattern","category-sql"],"_links":{"self":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":5,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions\/214"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/media\/209"}],"wp:attachment":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}