YouTube2Text

CTE in sql server Part 49 — Transcript

by kudvenkat · 2,050 words · 297 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hello welcome to preim Technologies I'm
  2. 0:02wenet this is part 49 of SQL server in
  3. 0:05this session we'll learn about common
  4. 0:07table Expressions before continuing with
  5. 0:09the session I strongly recommend to
  6. 0:11watch part 48 of this video
  7. 0:14series Common Table expressions are
  8. 0:16introduced in SQL Server 2005 so what is
  9. 0:20s CTE CTE is a temporary result set that
  10. 0:24can be referenced within a select insert
  11. 0:27update or delete statement that immedi
  12. 0:29Med follows the CTE let's understand
  13. 0:32what we mean by this definition with an
  14. 0:34example so here you can find the Syntax
  15. 0:37for creating a CTE so to create a CTE we
  16. 0:41use the withth keyword for example to
  17. 0:43create a stor procedure we use create
  18. 0:46procedure command similarly to create a
  19. 0:48table we use create table command but to
  20. 0:51create a CTE we use the width keyword so
  21. 0:55width keyword followed by the name of
  22. 0:58the CTE
  23. 1:00and then the columns that would make up
  24. 1:02your CTE so what is a CTE a CTE is a
  25. 1:06temporary result set so obviously a
  26. 1:09result set will have columns so what are
  27. 1:11the columns that would make up your
  28. 1:13result set that is what you specify here
  29. 1:17within a pair of parenthesis then you
  30. 1:19use the ask keyword then another set of
  31. 1:23PA you know parenthesis and then your
  32. 1:25CTE query there let's look at this with
  33. 1:28an example
  34. 1:30we have two tables here TBL employee and
  35. 1:32TBL Department tables TBL employee has
  36. 1:35got ID name gender and Department ID
  37. 1:37columns and TBL department has got
  38. 1:39Department ID and Department name
  39. 1:41columns now let's say I want you to
  40. 1:43write a query which gives me an output
  41. 1:46that you can see here I want the total
  42. 1:49number of employees by Department
  43. 1:52obviously to achieve this output I don't
  44. 1:55have to use a CTA I can simply achieve
  45. 1:57this by using joins and group bu but
  46. 2:00since we are talking about CTE here
  47. 2:02let's see how to achieve this using a
  48. 2:05CTE so I'm creating a c here called
  49. 2:08employee count I'm using the width
  50. 2:11keyword with employee count so employee
  51. 2:14count is the name of the CTE and
  52. 2:16Department ID and total employees are
  53. 2:18the columns that form you know the
  54. 2:22CD as and then this is your query so if
  55. 2:25you look at this query it's pretty
  56. 2:27straightforward what is this query doing
  57. 2:29it select two columns Department ID and
  58. 2:32total employees okay count of Star as
  59. 2:35total employees from TBL employee Group
  60. 2:38by Department ID so this query will
  61. 2:40return me the total number of employees
  62. 2:43by Department ID but if you look in the
  63. 2:45output I want total number of employees
  64. 2:47by Department name okay so this select
  65. 2:52query is nothing but your CTE query and
  66. 2:54if you look at the select query it's
  67. 2:55returning these two columns and these
  68. 2:58two columns will map to these two
  69. 3:00columns that you can see here you know
  70. 3:03this CTE has got Department ID and total
  71. 3:05employees your select query has got
  72. 3:07Department ID and total employees so
  73. 3:10these columns map to this CTE columns
  74. 3:13okay and then look at the way we are
  75. 3:15using the CTE this
  76. 3:18CTE is now being used in the select
  77. 3:21query so you're joining the employee
  78. 3:23count CTE with TBL Department table and
  79. 3:26why are we doing that because the
  80. 3:28employee count CTE has got Department ID
  81. 3:31and tottal employees Department table
  82. 3:33has got Department ID and Department
  83. 3:35name so we join the employee account
  84. 3:37with TBL department on the department ID
  85. 3:42column and we retrieve Department name
  86. 3:45from TBL
  87. 3:46department and total employees from
  88. 3:49employee count CTE and finally we order
  89. 3:52by total employees so if you execute
  90. 3:54this entire query we get the output that
  91. 3:56you see here let's look at the CT CTA in
  92. 4:01action so we are creating this CTA with
  93. 4:04employee count these are the columns
  94. 4:06that would make this
  95. 4:08CTA as and this is the CTA query the
  96. 4:12select query that would actually serve
  97. 4:14the data for the CTE so now when I
  98. 4:16actually execute this select query Alone
  99. 4:19look at this I get this this is nothing
  100. 4:21but a temporary result set a result set
  101. 4:24and for this result set you are giving a
  102. 4:27name called employee count
  103. 4:31and this column Department ID and total
  104. 4:33employees returned by your select query
  105. 4:35is mapping up to the columns you know in
  106. 4:38the
  107. 4:40CTE okay and then what you're doing
  108. 4:42you're joining this employee account
  109. 4:44with TBL Department table and the rest
  110. 4:47of the stuff is is pretty much a simple
  111. 4:49join here and finally you're ordering by
  112. 4:52total employees so if we execute this
  113. 4:55entire staff we should get the output
  114. 4:57that we are expecting all right right
  115. 5:00now the columns that you specify for the
  116. 5:02CTE here are actually optional if you
  117. 5:05don't specify I mean look at this
  118. 5:08obviously when you execute this
  119. 5:10query you get these two columns now if
  120. 5:13you don't specify the column names for
  121. 5:16your CTE
  122. 5:17here what is going to happen the same
  123. 5:20column names are retained and then used
  124. 5:24as the columns for this
  125. 5:26CTE so let's execute this and we get the
  126. 5:30same amount output still okay so it is
  127. 5:35also possible that you can just say Okay
  128. 5:37instead of saying Department ID I can
  129. 5:40just say
  130. 5:41d ID and instead of saying total
  131. 5:44employees I can say total but then the
  132. 5:47moment you do that the columns you know
  133. 5:50though this query is returning them as
  134. 5:52Department ID and total employees you
  135. 5:54know for the CTE they will be mapped to
  136. 5:57DP ID and total employees will be mapped
  137. 6:00to total so when you refer to that CTE
  138. 6:03in the select statement you will have to
  139. 6:05change those column names accordingly
  140. 6:08okay because if I try to execute this
  141. 6:10now we get an error invalid column name
  142. 6:13Department ID because on employee count
  143. 6:16CTE I don't find a column called
  144. 6:18Department ID because you have redefined
  145. 6:21you have renamed that to be DP DP ID so
  146. 6:25you should use DP
  147. 6:28ID
  148. 6:30and similarly here you have to specify
  149. 6:33that as
  150. 6:37total so now when we execute this we
  151. 6:40should succeed and get the same
  152. 6:43output all right um you know if you
  153. 6:47don't
  154. 6:48specify I mean if you do specify the
  155. 6:50columns the number of columns that you
  156. 6:52specify here should match with the
  157. 6:55number of the columns that your select
  158. 6:57query is returning if you say for
  159. 6:59example let's say I have another column
  160. 7:01XY Z for example okay look at this your
  161. 7:04select query is actually returning two
  162. 7:06columns Department ID and total
  163. 7:07employees Department ID will be mapped
  164. 7:10to D ID total employees will be mapped
  165. 7:12to Total but there is no you know third
  166. 7:15column in the select query to map to XY
  167. 7:17Z so obviously when I try to execute
  168. 7:20this it throws an error stating that
  169. 7:22employee count has fewer columns than
  170. 7:26were specified in the column list
  171. 7:29obviously that makes sense okay so the
  172. 7:32number of columns that you specify here
  173. 7:34should match the number of the columns
  174. 7:36returned by the CTE
  175. 7:38query all
  176. 7:42right now this is important to keep in
  177. 7:45mind you know if you look at the
  178. 7:47definition of the CTA a CT is a
  179. 7:49temporary result set we know what we
  180. 7:51mean by what we mean by that and then a
  181. 7:54CTE is a temporary result set that can
  182. 7:56be referenced within a select insert
  183. 7:59update or delete statement that
  184. 8:01immediately follows the CTA so if you
  185. 8:04look at the way we are using the CTA we
  186. 8:07are using the CTE within the select
  187. 8:08statement if you look at the select
  188. 8:10statement it follows the CTE there is
  189. 8:13nothing in between the CTE and the
  190. 8:15select statement that's why we are able
  191. 8:17to use that but on the other hand it's
  192. 8:20the same CTE except that you know I have
  193. 8:23select hello here okay we are saying we
  194. 8:26are using another select statement and
  195. 8:28within this select statement I am not
  196. 8:30referring to the CTA but then there is
  197. 8:33another select statement here where I am
  198. 8:35referring to the CTA so obviously what's
  199. 8:37going to happen by the time you know we
  200. 8:40execute this query gets executed the CTE
  201. 8:43is gone because this CTA will be
  202. 8:46available to a select insert update or
  203. 8:50delete that immediately follows the CTE
  204. 8:53if you try to use it anywhere else then
  205. 8:56you know if you try to do something else
  206. 8:58in between we get an error let's look at
  207. 9:00this in
  208. 9:04action so here we have the employee
  209. 9:07count CTE you know it's the same thing
  210. 9:09and then we have the select hello in
  211. 9:11between now look at this if I comment
  212. 9:13that and then if I try to execute this
  213. 9:16entire thing you know we succeed we get
  214. 9:19the output that we were getting before
  215. 9:21but on the other hand look at this the
  216. 9:23moment I uncomment
  217. 9:27this we are not using using the CTE in
  218. 9:30the select insert update or delete
  219. 9:32statement that immediately follows the
  220. 9:33CTE so obviously you cannot access this
  221. 9:37employee count in this select statement
  222. 9:39because this is not the immediate select
  223. 9:41statement for this CTE so let's see what
  224. 9:44happens if I try to execute this we get
  225. 9:47an error stating Common Table expression
  226. 9:49defined but not
  227. 9:52used okay so that's why in the
  228. 9:54definition it's very clear you know a
  229. 9:57CTE is a temporary result set that can
  230. 9:59be referenced from within a select
  231. 10:01insert update or delete statement that
  232. 10:03immediately follows the CTE and I hope
  233. 10:06that makes sense
  234. 10:09now all right it's also possible to
  235. 10:13create multiple ctes using a single
  236. 10:16width Clause now we know that to create
  237. 10:18a CTA we use the width
  238. 10:21Clause so you
  239. 10:23specify with keyword and then the name
  240. 10:26of the CTA and then the columns that
  241. 10:28would make pick up that CTE as your CTE
  242. 10:32query so this entire thing is one CTE
  243. 10:36but if you want an additional CD using
  244. 10:39the same width keyword what you can
  245. 10:41basically do is put a
  246. 10:44comma another name of the CT another CT
  247. 10:48and then the columns that would make up
  248. 10:49that CT as you know select query that
  249. 10:53can you know that makes up this CTE so
  250. 10:56it's possible to create more than one
  251. 10:58CTE using the withd keyword just by
  252. 11:01separating them with commas so what we
  253. 11:03are doing here it's pretty simple now if
  254. 11:05you look at this select query all we are
  255. 11:08doing is we are getting the total you
  256. 11:10know the total number of employees by
  257. 11:13department but we're filtering you know
  258. 11:15we are only doing it for it and payroll
  259. 11:18and here we are doing it for HR and
  260. 11:20admin departments so obviously when I
  261. 11:22execute this query you should see we get
  262. 11:25the total number of employees for it and
  263. 11:28payroll
  264. 11:29departments and when we execute this
  265. 11:32query you should see that we get the
  266. 11:36total number of employees for admin and
  267. 11:38HR department and we have named our CS
  268. 11:42accordingly so this gives us employee
  269. 11:44count by HR and admin Department this
  270. 11:47gives us the employee count by payroll
  271. 11:49and IT department and the way we are
  272. 11:52using that there is a select statement
  273. 11:54look at this now you might be wondering
  274. 11:56okay I am using this city within the
  275. 11:59select statement this is another select
  276. 12:01statement where I'm using you know maybe
  277. 12:04this CT so I'm not using them within a
  278. 12:08select statement that immediately
  279. 12:09follows CD actually this entire thing is
  280. 12:12treated as one statement because you're
  281. 12:15using the union keyword and we have
  282. 12:17spoken about Union keyword in the
  283. 12:19previous sessions of this video series
  284. 12:21which basically unions the result of
  285. 12:24this query with this query so this
  286. 12:26entire thing is treated as one a
  287. 12:29statement so obviously now if I execute
  288. 12:31this entire thing you should see total
  289. 12:33employees by department for all the four
  290. 12:37departments okay so this example
  291. 12:39illustrates that it's possible to create
  292. 12:41multiple CTS using a single with
  293. 12:46clause on this slide you can find
  294. 12:48resources for asp.net and cop and SQL
  295. 12:51server interview questions that's it for
  296. 12:53today thank you for listening have a
  297. 12:55great day

About this transcript

This page contains the full transcript of CTE in sql server Part 49 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,050 words across 297 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.