YouTube2Text

Group by in sql server - Part 11 — Transcript

by kudvenkat · 2,863 words · 404 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hello welcome to preim Technologies I am
  2. 0:02wenet this is part 11 of SQL server in
  3. 0:05this session we'll talk about how to
  4. 0:07group Pros using Group by filtering
  5. 0:10groups and the difference between where
  6. 0:12and having
  7. 0:13Clauses now if you look at the
  8. 0:15definition the group by class is used to
  9. 0:18group a selected set of rows into a set
  10. 0:21of summary rows by the values of one or
  11. 0:23more columns or
  12. 0:24Expressions it is always used in
  13. 0:27conjunction with one or more aggregate
  14. 0:29functions now what are the different
  15. 0:30aggregate functions that are available
  16. 0:32in SQL Server there are many for example
  17. 0:35we have sum average count minimum
  18. 0:38maximum Etc let's look at them now now
  19. 0:41if you look at this table I've got the
  20. 0:42salary column and let's say I want the
  21. 0:45total salary that I'm paying within my
  22. 0:48organization so obviously what will you
  23. 0:50do if you have to do it manually you'll
  24. 0:52take each Ru and add them together but
  25. 0:55in SQL Server we have an aggregate
  26. 0:57function called sum which we can use so
  27. 1:00select sum so what do you want to sum I
  28. 1:03want to sum the salary so
  29. 1:08salary from TBL employee so all you're
  30. 1:13saying is okay total this salary for
  31. 1:16me so that gives us the total salary so
  32. 1:19we are using this aggregate function
  33. 1:21similarly if you want to say let's say
  34. 1:23for example I want to find out what is
  35. 1:24the minimum salary that I'm paying you
  36. 1:26know within my organization if you have
  37. 1:28to do it manually you'll have have to
  38. 1:30inspect each row and see okay 4,500 is
  39. 1:33less than 2ou greater than 2,800 okay I
  40. 1:35think 2,800 is the minimum salary but in
  41. 1:38SQL Server we have the aggregate
  42. 1:40function which we can use minimum okay I
  43. 1:43want the minimum salary from this table
  44. 1:46so when we execute this we should get
  45. 1:47the minimum salary and along the same
  46. 1:49lines we have the max function as well
  47. 1:52which gives us the maximum
  48. 1:58salary so max of salary so when we press
  49. 2:01that we get that okay so like this we
  50. 2:04have several aggregate functions in SQL
  51. 2:06Server so if you look at the definition
  52. 2:08you know it says Group by Clause is
  53. 2:10always used in conjunction with one or
  54. 2:13more aggregate functions okay now let's
  55. 2:16understand Group by so if you look at
  56. 2:18this table I've got a an employee table
  57. 2:21which has got their names genders salary
  58. 2:24and the city they are working in okay
  59. 2:28now I want you to write a query let's
  60. 2:30say and that query should give me the
  61. 2:32result that you see on the right hand
  62. 2:34side here I want the total salary that I
  63. 2:36paying to my employees by City okay how
  64. 2:40do we achieve this using groupby so if
  65. 2:43you look at this from a you know if you
  66. 2:45have to do it manually okay what will
  67. 2:47you do you will take each London record
  68. 2:50and then add their total salar similarly
  69. 2:52you take each New York record and add
  70. 2:55their salaries okay so you first group
  71. 2:58the records by City and then sum the
  72. 3:01salary column across that group so even
  73. 3:04the query has to do that so that's why
  74. 3:06we use Group by Cloud here so let's see
  75. 3:09how to use that so if you look at the
  76. 3:11query that we have written already we
  77. 3:13are using this sum aggregate function to
  78. 3:17Total the you know to sum up the entire
  79. 3:20salary that we are paying to our
  80. 3:22employees okay now this is the total
  81. 3:25salary of the organization but I don't
  82. 3:27want the total salary of the
  83. 3:28organization instead I want the total
  84. 3:31salaries paid by city so in our output
  85. 3:35we we not only want the total salary we
  86. 3:38also want the city okay so in our select
  87. 3:41list we need to have the
  88. 3:45city as well as the total salary now if
  89. 3:49you look at this for the total salary we
  90. 3:51didn't have a column name so let's give
  91. 3:53a column Name by using as and we will
  92. 3:56say total salary so this
  93. 4:00is an alas name for this
  94. 4:03column cool so now let's okay look at
  95. 4:07this select City sum of salary from TBL
  96. 4:11employee okay you're saying okay I want
  97. 4:13the city and the total salary but you
  98. 4:16should also tell I want to group okay by
  99. 4:22city so Group by City And then calculate
  100. 4:25the total salaries across that City okay
  101. 4:29so when I execute this
  102. 4:31query you should see the same output so
  103. 4:34the city names and the total salaries
  104. 4:37now let's see what's going to happen if
  105. 4:39I remove this group by clause and then
  106. 4:42execute this query Now by looking at
  107. 4:44itself you should you know it should
  108. 4:46make sense you
  109. 4:48know this is going to give an error
  110. 4:51because you're saying okay select City
  111. 4:53column okay I select the city column and
  112. 4:55select the sum of
  113. 4:57salaries okay so you're say you want
  114. 5:00City column and the total salary but
  115. 5:02you're not telling the query to group
  116. 5:05them by City And then total the salaries
  117. 5:08okay if I don't use the salary City
  118. 5:11column then it makes sense okay I want
  119. 5:13the total salaries in the entire table
  120. 5:16but when I use the city column you
  121. 5:18should tell it okay Group by city as
  122. 5:21well so obviously if I execute this
  123. 5:24query you will get an error stating that
  124. 5:27TBL employee. city is inv Val in the
  125. 5:30select list because it's not contained
  126. 5:33either in an aggregate function so we
  127. 5:35cannot apply an aggregate function to a
  128. 5:38text column you know what does it mean
  129. 5:40when you say sum cities you cannot sum
  130. 5:43cities London New York okay similarly
  131. 5:46you cannot find the average of city
  132. 5:48names itself okay it's only on numerical
  133. 5:51columns you can apply the aggregate
  134. 5:52functions like sum average
  135. 5:55Etc okay so we cannot use an aggregate
  136. 5:58function like sum average on this column
  137. 6:01and it's not part of group by as well
  138. 6:04that's why it's invalid in the select
  139. 6:06list okay so that error makes sense okay
  140. 6:09so the columns that you use in the
  141. 6:11select list okay they have to satisfy
  142. 6:14one of these two
  143. 6:15conditions you should either have an
  144. 6:18aggregate function applied on that
  145. 6:19column or that column should be part of
  146. 6:23group by Clause if none of these
  147. 6:26conditions are met then you get this
  148. 6:29error
  149. 6:30okay so the moment we put City in the
  150. 6:33group by Clause this condition is met
  151. 6:35and we don't get that error and that
  152. 6:37makes logical sense as well we don't
  153. 6:40have to really mug and remember that it
  154. 6:42makes sense you're asking okay give me
  155. 6:44the total salary and the city column but
  156. 6:47you're not telling it to group by City
  157. 6:49column so how will SQL Server know how
  158. 6:52it has to Total those
  159. 6:54salaries all
  160. 6:57right so obviously if you omit the group
  161. 6:59by clause and try to execute the query
  162. 7:01you get this error and we have seen this
  163. 7:03so if you look at in the output you know
  164. 7:05we are get we we are retrieving total
  165. 7:07salaries by city now let's take this one
  166. 7:10more level not only by city I want to
  167. 7:13break it down further by gender so if
  168. 7:16you look at the next slide look at this
  169. 7:17I want the output like this okay in the
  170. 7:20previous example we are we are showing
  171. 7:22the total salaries paid by city but now
  172. 7:27I want to break it down even further by
  173. 7:29by gender as well for example in Sydney
  174. 7:32how much is the total salary that I'm
  175. 7:34paying for male and female employees so
  176. 7:36if you look at the example here for male
  177. 7:38employees with and Sydney we are paying
  178. 7:402,800 for female it's 4,800 so I want
  179. 7:44this breakdown as well so we are
  180. 7:46grouping Now by multiple columns okay so
  181. 7:50we are grouping this total salary first
  182. 7:52by City And Then by
  183. 7:54gender okay so how do we do that it's
  184. 7:57pretty simple if you look at the output
  185. 7:59at s it should make sense okay in the
  186. 8:01select list we should now have three
  187. 8:03columns we already have City and total
  188. 8:06salaries columns we want the gender
  189. 8:08column as well so let's put that in the
  190. 8:10select list so I already have City I
  191. 8:13also want gender so select City Comm
  192. 8:16agenda now if I execute this query
  193. 8:19without any further modifications let's
  194. 8:21see what going what's going to happen
  195. 8:23you might you know you should have
  196. 8:24already expected we get the same error
  197. 8:27that we got before gender is invalid in
  198. 8:31the select list because you're not
  199. 8:33applying an aggregate function on top of
  200. 8:35it or and it's not part of the group by
  201. 8:40Clause so we we get the same error here
  202. 8:44so gender is invalid in the select list
  203. 8:46because it's not contained either in an
  204. 8:48aggregate function or the group by class
  205. 8:50so we need to make it part of the group
  206. 8:52by class to tell SQL Server engine all
  207. 8:55right I want you to total the salaries
  208. 8:57first by City And then by gender because
  209. 9:00we want that breakdown in that
  210. 9:03order so when we execute this query you
  211. 9:06should see the output that we have here
  212. 9:09you know it's slightly different here
  213. 9:12the cities are together for example if
  214. 9:14you look at Sydney Sydney they're next
  215. 9:15to each other New York New York they're
  216. 9:17next to each other but if you look at
  217. 9:18our output it's not so so obviously we
  218. 9:21use the order by Clause to order the
  219. 9:23result and if you're not sure on what is
  220. 9:25a by class I will strongly encourage you
  221. 9:28to watch the previous video in this
  222. 9:32series so order
  223. 9:36by city so when we execute this you know
  224. 9:40it should match the output on the
  225. 9:42presentation all right now you might be
  226. 9:45wondering okay I have by city by gender
  227. 9:49breakdown let us say I want by gender
  228. 9:52and byy City and know probably something
  229. 9:55like I want first maybe gender
  230. 10:00and then maybe City so I want first by
  231. 10:04gender and
  232. 10:09then by city in this order you just
  233. 10:12change the
  234. 10:17order and execute this
  235. 10:19query you get the same result but the
  236. 10:22columns have just switched okay first by
  237. 10:24gender and then by
  238. 10:26City cool
  239. 10:30okay now let's take this even more you
  240. 10:32know to the next step now we have seen
  241. 10:35we can group based on multiple columns
  242. 10:37now let us say if it's possible to use
  243. 10:39multiple aggregate functions absolutely
  244. 10:42you know here we are showing total
  245. 10:44salary by City And by gender let's say I
  246. 10:47want total number of employees as well
  247. 10:51now if you remember if you want to find
  248. 10:53out the total number of employees we can
  249. 10:55use let's say if you look at the TBL
  250. 10:57employee table we've got 10 rows there
  251. 10:59there and if I want to find the total
  252. 11:01number of employees within my
  253. 11:02organization I can say count of
  254. 11:05star from TBL employees when we press
  255. 11:08that we get the total count which is 10
  256. 11:11and for performance reasons instead of
  257. 11:12using star use a specific column name
  258. 11:15you know maybe
  259. 11:16ID so I get the same result okay so so
  260. 11:21what is it that we want we not only want
  261. 11:23Total salaries by City And by jenda we
  262. 11:26also want the total number of employees
  263. 11:28so
  264. 11:30if you want the total number of
  265. 11:31employees we use the count aggregate
  266. 11:33function so count of ID and if you want
  267. 11:38to give a meaningful name we will say
  268. 11:42total
  269. 11:45employees okay and when I execute
  270. 11:49this you should see we have total
  271. 11:51employees as well okay so total salary
  272. 11:55by gender by City And total number of
  273. 11:57employees and you might be wondering why
  274. 11:59did I use the square brackets here if
  275. 12:01you want to have a space in the column
  276. 12:03name then you have to use the square
  277. 12:05bracket otherwise you get an error so if
  278. 12:07I remove the bracket okay we get an
  279. 12:10error because it treats these two as two
  280. 12:12different
  281. 12:14words but you can you know if it's just
  282. 12:18one word you don't have to have a square
  283. 12:20bracket around it so if we execute this
  284. 12:22we end up with the same
  285. 12:26result all right so we have seen how to
  286. 12:29use multip multiple aggregate functions
  287. 12:31as well okay now let us see how to
  288. 12:33filter the groups okay now so far we
  289. 12:37have seen okay we got all the um result
  290. 12:41but let's say I want to see only male
  291. 12:44salaries I don't want to see female
  292. 12:46salaries so how do I filter that okay
  293. 12:50now usually to filter the rows we can
  294. 12:52use the wear Clause you know where you
  295. 12:56can say
  296. 12:58gender is equal to
  297. 13:02mail so when I execute this query what
  298. 13:05is going to happen it's going to show us
  299. 13:07only the male employees total salaries
  300. 13:10and and total number of male employees
  301. 13:12okay the other way to get the same
  302. 13:14result is instead of using the we
  303. 13:18Clause we can use having Clause but
  304. 13:21having Clause should come after the
  305. 13:23group by okay if you remember select
  306. 13:26star from table name where okay so after
  307. 13:31the from Clause we usually have the
  308. 13:32WHERE Clause but having Clause has to
  309. 13:34come after the group by Clause so now
  310. 13:37having gender is equal to male so when
  311. 13:39we execute this query we end up getting
  312. 13:41the same result now let's see what's the
  313. 13:44difference between these two queries you
  314. 13:45know here we said having where gender is
  315. 13:49equal to male and in this query we said
  316. 13:52where gender is equal to M and if we
  317. 13:54execute them together you should see we
  318. 13:57exactly have the same output but the way
  319. 13:59the query gets executed is a little
  320. 14:01different you know when you use wear
  321. 14:03Clause the T from the table only male
  322. 14:07records are retrieved and then they are
  323. 14:10grouped okay but whereas in this when
  324. 14:13you use the having
  325. 14:16Clause all the rows from the table TBL
  326. 14:18employee are retrieved they are grouped
  327. 14:21by gender and then only male groups are
  328. 14:25shown filtering female groups so here I
  329. 14:29aggregations are done for every Row in
  330. 14:31this table whereas here aggregations are
  331. 14:33not done they are they are filtered even
  332. 14:36before the aggregations are done only
  333. 14:38male set of records are retrieved from
  334. 14:40the table and then aggregated and then
  335. 14:43but remember it's possible to filter you
  336. 14:46know the output either using the wear
  337. 14:48clause or having Clause but it depends
  338. 14:50on how you know how you want the
  339. 14:57output so you can either use the wear
  340. 15:00clause or having Clause okay from a
  341. 15:03performance standpoint you cannot say
  342. 15:05that one method is less efficient than
  343. 15:07the other SQL Server Optimizer analyzes
  344. 15:10each statement and selects an efficient
  345. 15:13way of executing it and as a best
  346. 15:15practice use the syntax syntax that
  347. 15:18clearly describes the desired result try
  348. 15:21to eliminate rows that you wouldn't need
  349. 15:24as early as possible probably using the
  350. 15:26we clause
  351. 15:31okay so now we'll talk about the
  352. 15:33difference between we and having Clause
  353. 15:35now you might have already understood
  354. 15:37one difference by now where Clause
  355. 15:39filters rows before aggregations before
  356. 15:42groupings are performed whereas having
  357. 15:44Clause filters groups after the
  358. 15:46aggregations are performed okay and
  359. 15:49another difference is that you can use
  360. 15:51having Clause only with the select
  361. 15:53statement whereas we Clause can be used
  362. 15:57with other than select statements as
  363. 15:59well like insert and updates as well and
  364. 16:02aggregate functions cannot be used in
  365. 16:04the wear Clause let's see this okay what
  366. 16:07do we mean by aggregate functions cannot
  367. 16:08be used in the we clause examples of
  368. 16:11aggregate functions we already discussed
  369. 16:13count average sum minimum
  370. 16:15Etc okay so let's say select star from
  371. 16:18TBL
  372. 16:20employee where let's say sum
  373. 16:24of
  374. 16:26salary greater than 4 4,000 can I do
  375. 16:30this absolutely now I mean you can do
  376. 16:32that there is a way to do that you know
  377. 16:34using a subquery and having clause which
  378. 16:37we'll be talking about in a later
  379. 16:38session when we talk about
  380. 16:40subqueries okay but in general you know
  381. 16:43you cannot use uh aggregate functions
  382. 16:47with the we Clause whereas aggregate
  383. 16:49functions can be used in the having
  384. 16:51clause and that's what we have seen
  385. 16:53until now for example here you know when
  386. 16:56you're filtering
  387. 16:59you know you can say
  388. 17:01having maybe sum of
  389. 17:06salary greater
  390. 17:08than
  391. 17:115,000 so when we execute that we don't
  392. 17:14get the row where the sum of salary is
  393. 17:17less than U 5,000 less than or equal to
  394. 17:205,000 so you can use aggregate functions
  395. 17:22with the having Clause but you cannot do
  396. 17:24that with the wear
  397. 17:26Clause those are the differences between
  398. 17:29wear and having and this is one of the
  399. 17:30common interview question that's asked
  400. 17:32these days and it's very simple as
  401. 17:35well on this slide you can find
  402. 17:37resources for asp.net and cop interview
  403. 17:39questions that's it for today thank you
  404. 17:41for listening have a great day

About this transcript

This page contains the full transcript of Group by in sql server - Part 11 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,863 words across 404 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.