PageSourceSearch

https://kysely.dev/assets/js/a26f8d69.4b2c73e7.js

js kysely.dev collected 2026-10-02 02:57:43 UTC 2,707 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkkysely_site=globalThis.webpackChunkkysely_site||[]).push([[3381],{6650(e,s,t){t.r(s),t.d(s,{assets:()=>c,contentTitle:()=>o,default:()=>p,frontMatter:()=>a,metadata:()=>n,toc:()=>d});const n=JSON.parse('{"id":"examples/cte/simple-selects","title":"Simple selects","description":"Common table expressions (CTE) are a great way to modularize complex queries.","source":"@site/docs/examples/cte/0010-simple-selects.mdx","sourceDirName":"examples/cte","slug":"/examples/cte/simple-selects","permalink":"/docs/examples/cte/simple-selects","draft":false,"unlisted":false,"editUrl":"https://github.com/kysely-org/kysely/tree/master/site/docs/examples/cte/0010-simple-selects.mdx","tags":[],"version":"current","sidebarPosition":10,"frontMatter":{"title":"Simple selects"},"sidebar":"tutorialSidebar","previous":{"title":"CTE","permalink":"/docs/category/cte"},"next":{"title":"Inserts, updates and deletions","permalink":"/docs/examples/cte/inserts-updates-and-deletions"}}');var i=t(1058),l=t(4801),r=t(2360);const a={title:"Simple selects"},o="Simple selects",c={},d=[];function m(e){const s={h1:"h1",header:"header",p:"p",...(0,l.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(s.header,{children:(0,i.jsx)(s.h1,{id:"simple-selects",children:"Simple selects"})}),"\n",(0,i.jsx)(s.p,{children:"Common table expressions (CTE) are a great way to modularize complex queries.\nEssentially they allow you to run multiple separate queries within a\nsingle roundtrip to the DB."}),"\n",(0,i.jsx)(s.p,{children:"Since CTEs are a part of the main query, query optimizers inside DB\nengines are able to optimize the overall query. For example, postgres\nis able to inline the CTEs inside the using queries if it decides it's\nfaster."}),"\n","\n","\n",(0,i.jsx)("div",{style:{marginBottom:"1em"},children:(0,i.jsx)(r.Q,{code:"const result = await db\n  // Create a CTE called `jennifers` that selects all\n  // persons named 'Jennifer'.\n  .with(\n    'jennifers',\n    db\n      .selectFrom('person')\n      .where('first_name', '=', 'Jennifer')\n      .select(['id', 'age']),\n  )\n  // Select all rows from the `jennifers` CTE and\n  // further filter it.\n  // To refer to a CTE in another CTE, use the callback variant of `with`.\n  .with('adult_jennifers', (db) =>\n    db.selectFrom('jennifers').where('age', '>', 18).select(['id', 'age']),\n  )\n  // Finally select all adult jennifers that are\n  // also younger than 60.\n  .selectFrom('adult_jennifers')\n  .where('age', '<', 60)\n  .selectAll()\n  .execute()"})})]})}function p(e={}){const{wrapper:s}={...(0,l.R)(),...e.components};return s?(0,i.jsx)(s,{...e,children:(0,i.jsx)(m,{...e})}):m(e)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.