YouTube2Text

Introduction to SQL/2 — Transcript

by Data Base Management System - IITKGP · 4,528 words · 843 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:15welcome to module 7 of database
  3. 0:18management systems
  4. 0:20ah this is a second
  5. 0:22part
  6. 0:23out of total three of introduction to
  7. 0:26sql
  8. 0:27in the
  9. 0:28last module we have discussed about
  10. 0:31the evolution of sql
  11. 0:34the data definition language part of it
  12. 0:37and the basic structure of queries
  13. 0:40in the
  14. 0:41current module we will
  15. 0:43complete the understanding of the basic
  16. 0:45query structure and will see how
  17. 0:48common
  18. 0:49set theoretic operations can be
  19. 0:51performed in terms of queries
  20. 0:54we will familiarize ourselves with the
  21. 0:57handling of null values and aggregation
  22. 1:01operation that will be frequently
  23. 1:03required for forming queries
  24. 1:05so this is a module outline these are
  25. 1:08the topics that we will discuss so we
  26. 1:10start with the ah discussion of some
  27. 1:12more basic operations in the query
  28. 1:16we
  29. 1:17have already discussed this that
  30. 1:20if we do select
  31. 1:21star from two tables then it results in
  32. 1:24a cartesian product we have seen this
  33. 1:27ah result earlier
  34. 1:29the
  35. 1:30now by itself as we had said that by
  36. 1:32itself the cartesian product may not
  37. 1:34really make a lot of
  38. 1:38you may not be very useful
  39. 1:40but suppose we want to ah answer this
  40. 1:44kind of a query that find
  41. 1:46all names of all instructors who have
  42. 1:49taught some course
  43. 1:51and
  44. 1:52ah for those
  45. 1:55you also write the course id
  46. 1:57so what we are interested in is the name
  47. 1:59of the instructor and course id so we
  48. 2:02put that on the select class
  49. 2:04ah these are the two tables that are
  50. 2:06required because ah the name of the
  51. 2:08instructor is there in the instructor
  52. 2:11table relation and
  53. 2:13then the relationship between
  54. 2:16which instructor teach which course is
  55. 2:20in the teachers table so in the
  56. 2:23instructor table we have the name
  57. 2:25in the
  58. 2:26teachers table here the course id and
  59. 2:28also this relationship as to
  60. 2:31which
  61. 2:32course is taught by which teacher and
  62. 2:36here we have
  63. 2:37the
  64. 2:38relationship between
  65. 2:40which id of the instructor has which
  66. 2:43name
  67. 2:44so
  68. 2:46what we do is when we
  69. 2:48do this cartesian product we will get
  70. 2:50something like this as we have already
  71. 2:52seen
  72. 2:52but we want to qualify it by with a
  73. 2:55predicate we say that we will
  74. 2:58out of all these combination will choose
  75. 3:00only those where the instructor id
  76. 3:02equals the teacher's id
  77. 3:05so if we look at
  78. 3:07say
  79. 3:08in a in a in the first row the
  80. 3:11instructor id is this
  81. 3:15teaches id
  82. 3:17is this
  83. 3:18which is id of the teachers table they
  84. 3:20are same so it says that this instructor
  85. 3:23srinivasan
  86. 3:24actually teaches the course cs101
  87. 3:28whereas if you look into this row
  88. 3:33it says that instructor id is this and
  89. 3:36teaches id is this
  90. 3:37and we are really not interested in this
  91. 3:39combination because this combination
  92. 3:41does not convey anything meaningful
  93. 3:44so
  94. 3:45by the use of this where clause we will
  95. 3:47try to choose only those records where
  96. 3:51these two ids are same which will tell
  97. 3:53us that this particular instructor
  98. 3:55actually teaches that particular course
  99. 3:58so
  100. 3:59if we do that
  101. 4:01ah then
  102. 4:05you will find that
  103. 4:06majority of the records that
  104. 4:09actually
  105. 4:10came about in the cartesian product are
  106. 4:15eliminated from this result so i have
  107. 4:17struck them out
  108. 4:19so you can currently see in this part
  109. 4:21you can see only four records the three
  110. 4:24courses taught by srinivasan
  111. 4:27so and the one core stored by you so in
  112. 4:31the output you will have
  113. 4:37in the output
  114. 4:39you will
  115. 4:41have this name
  116. 4:43this name part
  117. 4:46this
  118. 4:48name part
  119. 4:49and
  120. 4:50this course id part
  121. 4:52because you are projecting on these two
  122. 4:54so this will be the output table which
  123. 4:56will get generated
  124. 4:58and just to remind you this is the
  125. 5:01notion of natural join that we had
  126. 5:04discussed in relational algebra ah in
  127. 5:06this case we will actually call it
  128. 5:08equijoin because we are using a equality
  129. 5:11condition
  130. 5:12after the cartesian product to join
  131. 5:15these two relationships
  132. 5:17so this is a very critical
  133. 5:20ah operation in many of cases in our
  134. 5:23database query system
  135. 5:26there is and this is another extension
  136. 5:27of this ah
  137. 5:29similar ah example
  138. 5:31so here we have added another
  139. 5:34predicate in the where clause specifying
  140. 5:37that instructor dot department name
  141. 5:40is art so which means that this will now
  142. 5:43give the names of all instructors
  143. 5:46in the art department only who have
  144. 5:48taught some course and specify their
  145. 5:51course id so in different such ways you
  146. 5:53can manipulate and create
  147. 5:55queries
  148. 5:57it is possible to read them we have
  149. 5:59already seen examples you can rename a
  150. 6:01relation you can rename an attribute and
  151. 6:04the style is to use as so here you can
  152. 6:07see that in the select
  153. 6:10query we have said from instructor as t
  154. 6:13so
  155. 6:14the name of this relation can be treated
  156. 6:16as t and we again see that as instructor
  157. 6:20instructor as s so actually what we are
  158. 6:23doing we are doing a
  159. 6:25join between
  160. 6:27the same relation instructor and
  161. 6:30instructor
  162. 6:32so and we are trying to find out all
  163. 6:34instructors who have higher salary than
  164. 6:36some instructor in computer science so
  165. 6:39the sum instructor in computer science
  166. 6:41is specified by this condition because
  167. 6:43department has to be computer science
  168. 6:45and the fact that salary is higher so as
  169. 6:48if you treat that though it is actually
  170. 6:52a join between
  171. 6:54a
  172. 6:55product between
  173. 6:57instructor and instructor the same
  174. 6:59relation but you by renaming you treat
  175. 7:01them as if they are two different
  176. 7:04ah tables having name t and s and then
  177. 7:07it becomes easier to write this kind of
  178. 7:09query so ah otherwise it is it is quite
  179. 7:11difficult to write
  180. 7:13this query to find out because you need
  181. 7:16to actually create a product of one
  182. 7:19relation with itself
  183. 7:21keyword as is optional you can just
  184. 7:24write instructor and then the name the
  185. 7:27new name that you want to give and that
  186. 7:28itself will work
  187. 7:31here is another cartesian product
  188. 7:33example ah here given a relation ah
  189. 7:36which is which list a person and
  190. 7:40he is or her supervisor we want to find
  191. 7:42out
  192. 7:43all supervisors direct or indirect of
  193. 7:46that person so i leave this as an
  194. 7:49exercise to you to think over as to how
  195. 7:52we can actually compute this
  196. 7:55query
  197. 7:58supports several string operations and
  198. 8:00of particular interest are two specific
  199. 8:04symbols characters which allow
  200. 8:08us doing certain match percentage is
  201. 8:10used to match any substring and
  202. 8:13underscore is used to match any
  203. 8:14particular character and we use a
  204. 8:18keyword like to find out different
  205. 8:21string patterns that can be matched
  206. 8:24so
  207. 8:25here we want to find the names of all
  208. 8:27instructors whose name includes the
  209. 8:29substring d a r
  210. 8:31and ah by by writing this so you are
  211. 8:34saying the predicate is formed is name
  212. 8:37like this so
  213. 8:39what is there is a percentage before
  214. 8:41there is a percentage after so anywhere
  215. 8:44dar will feature in the name
  216. 8:46this predicate will turn out to be true
  217. 8:49otherwise if there is no dir in the name
  218. 8:52the predicate will turn out to be false
  219. 8:53and that particular record will not get
  220. 8:56selected
  221. 8:57so in this way we can using like we can
  222. 9:00actually
  223. 9:02do different kinds of string operations
  224. 9:04as conditions in the where clause or
  225. 9:07else so
  226. 9:09now naturally this
  227. 9:10brings in an issue of what if my string
  228. 9:13itself
  229. 9:15has a percentage or an underscore
  230. 9:17character so the rule followed is you
  231. 9:20will need to escape that with the
  232. 9:23escape character that you define this is
  233. 9:25a style which you have seen in c
  234. 9:27programming as well
  235. 9:30patterns are certainly case sensitive so
  236. 9:33it depends
  237. 9:34it will distinguish between uppercase as
  238. 9:37well as lowercase
  239. 9:38and these are different examples of
  240. 9:41string matching that you can do where
  241. 9:43you can match at this beginning of a
  242. 9:45string end of a string anywhere in the
  243. 9:47string specific number of characters in
  244. 9:50a string and so on
  245. 9:52ah sql supports ah
  246. 9:54concatenation
  247. 9:56conversion of lower to upper case and
  248. 9:58vice versa and different other common
  249. 10:01string operations those are available as
  250. 10:04functions in sql and can be used for
  251. 10:07convenience
  252. 10:10now let us address a different question
  253. 10:12let us say we have computed a query
  254. 10:15and then often we would want that the
  255. 10:18result
  256. 10:19be ordered
  257. 10:21in according to certain order
  258. 10:22particularly the value of certain field
  259. 10:25if you want the result to be ordered
  260. 10:27then sql allows you to do that by
  261. 10:29another clause that you add to the query
  262. 10:32which is called order by so what this
  263. 10:34will do we have already seen this query
  264. 10:36this will find out the names of all the
  265. 10:38instructors ah
  266. 10:40and
  267. 10:42the names will occur in a distinct
  268. 10:43manner because distinct is specified but
  269. 10:46then the output will be in terms ordered
  270. 10:49by the name
  271. 10:51and the ordering can be
  272. 10:53[Music]
  273. 10:55descending or ascending
  274. 10:57by you can control that by specifying
  275. 10:59whether you want descending or ascending
  276. 11:01by default the ordering is ascending
  277. 11:04so that makes the presentation of the
  278. 11:06result often very easy
  279. 11:08and you can certainly sort on multiple
  280. 11:11fields as well so it can be ordered
  281. 11:13based on combination of fields
  282. 11:16sql ah
  283. 11:17where clause also allows
  284. 11:19between as a comparison parameter so
  285. 11:22between can specify two values so that
  286. 11:25whenever the field value will be between
  287. 11:27these two
  288. 11:28ah given values the condition will be
  289. 11:31predicate will be taken to be true
  290. 11:33otherwise is taken to be false
  291. 11:35you can compare based on tuple as well
  292. 11:38so
  293. 11:39in this case you could have written
  294. 11:43you could have
  295. 11:45checked for equality of instructor id
  296. 11:47with teachers id and
  297. 11:50department name with
  298. 11:52the literal biology but you can compact
  299. 11:55it by writing a tuple notation as is
  300. 11:57shown here so these are common
  301. 11:59convenient ways of writing different ah
  302. 12:02where clauses
  303. 12:04now we have ah
  304. 12:05specified that
  305. 12:06sql does ah
  306. 12:09carry duplicates so
  307. 12:12unlike relational algebra
  308. 12:14which said theoretically specify that
  309. 12:17their duplicates should not be there a
  310. 12:20an sql there could be duplicate entries
  311. 12:22in the same relation
  312. 12:24so
  313. 12:25there is a
  314. 12:26this is called when duplicates are
  315. 12:28allowed in set theory then such sets
  316. 12:31where duplicates are allowed and known
  317. 12:32as multi sets
  318. 12:34so
  319. 12:35there are multiset versions of the sql
  320. 12:38queries or so to say the relational
  321. 12:41algebra operations so you have a
  322. 12:43selection um which
  323. 12:46can be multiset selection which means
  324. 12:48that
  325. 12:49if there are certain c one number of
  326. 12:52copies of a tuple in the relation which
  327. 12:54satisfy the condition theta then all of
  328. 12:56them will feature in the result
  329. 12:59and all those ah
  330. 13:02copies can be seen simultaneously
  331. 13:04because it is a multiset condition
  332. 13:06similar definitions are
  333. 13:08hold for projection as well as for
  334. 13:11cartesian product so i will leave it to
  335. 13:13you to go through the details and
  336. 13:15convince yourself that these multiset
  337. 13:18relations really extend the
  338. 13:21traditional single set distinct
  339. 13:24definition of the relational algebra
  340. 13:28so here is an example where there are
  341. 13:30two multiset relations as you can
  342. 13:33see
  343. 13:34particularly this one which has
  344. 13:37identical duplicate entries so using
  345. 13:40that
  346. 13:41you can define a
  347. 13:45cartesian you can define a projection ah
  348. 13:47of ah
  349. 13:49r one
  350. 13:51on b
  351. 13:56r one one b which will ah certainly
  352. 13:59ah give you its you are doing projection
  353. 14:01on b so it will give you a only
  354. 14:04so you will have this result itself will
  355. 14:06be a multiset because you will get two
  356. 14:08a's so this result will be like a
  357. 14:11a
  358. 14:14and then you have r two with which you
  359. 14:17are doing the cartesian product so you
  360. 14:19will have all possible
  361. 14:21combinations all these six are the
  362. 14:24result in the sql
  363. 14:26whereas in set theoretically
  364. 14:28the result should have been only
  365. 14:30these two tuples
  366. 14:39now we
  367. 14:39take a quick look into the common set
  368. 14:42operations
  369. 14:43so it is possible to ah do union ah
  370. 14:47intersection difference kind of
  371. 14:49operations very easily with sql queries
  372. 14:52so ah suppose we want to find
  373. 14:55all courses that
  374. 14:56ran in fall 2009
  375. 14:59or in spring 2010
  376. 15:02so certainly the first part of the query
  377. 15:04is simple this will give you all courses
  378. 15:06that
  379. 15:07ran so you are taking out the course id
  380. 15:10from section is where the course
  381. 15:14running information is provided and you
  382. 15:16are putting two conditions which say
  383. 15:17that they actually this courses ran in
  384. 15:20ah
  385. 15:21fall 2009
  386. 15:23so this is the first query the second
  387. 15:25query says the courses that ran in
  388. 15:27spring
  389. 15:28ah 2010
  390. 15:30and you are you have an or condition in
  391. 15:32the
  392. 15:33statement of what you are looking for so
  393. 15:35you do a union union is another keyword
  394. 15:38so this will simply give you a relation
  395. 15:41of ah the course id attribute as the
  396. 15:44only attribute which has records from
  397. 15:46the first as well as the second query
  398. 15:50similarly you can
  399. 15:53find out uh
  400. 15:56the courses that ran both in fall 2009
  401. 15:59and spring 2010 by using intersect
  402. 16:03which basically give you the
  403. 16:04intersection of the result of the first
  404. 16:06and the second query
  405. 16:09you could also do
  406. 16:11difference set difference by doing fine
  407. 16:13courses that ran in fall 2009 but not in
  408. 16:172010. so what will that mean that will
  409. 16:20mean that the result of the result of
  410. 16:22this first query
  411. 16:24from the result of the first query the
  412. 16:26results of the second query be
  413. 16:28subtracted
  414. 16:29be done a difference from so those
  415. 16:32that
  416. 16:34had run in the fall 2009
  417. 16:37and then was again run in spring 2010
  418. 16:40will get removed we so that is done
  419. 16:43through the accept
  420. 16:45keyword so in this way you can very
  421. 16:47easily do set operations whenever that
  422. 16:51is easy to conceive obviously you can
  423. 16:53write these queries in
  424. 16:54several other different forms but this
  425. 16:56is just to show you how set theoretic
  426. 16:58operations can be easily written
  427. 17:02ah you can do ah set operations
  428. 17:05like
  429. 17:05this in terms of
  430. 17:07ah find salaries of all instructions
  431. 17:09that are less than a largest salary so
  432. 17:12again we are using renaming
  433. 17:14to ah think of the same relation as 2
  434. 17:18and then as if
  435. 17:19from the
  436. 17:20relation t
  437. 17:22we are trying to look at relation s and
  438. 17:24finding out what are the salaries which
  439. 17:27are smaller than that and certainly
  440. 17:29whatever comes in
  441. 17:30out is
  442. 17:32[Music]
  443. 17:34the one which is not the largest because
  444. 17:36certainly the largest will not satisfy
  445. 17:38this particular condition because it
  446. 17:40will get compared with itself
  447. 17:46you can find salaries of all instructors
  448. 17:49and then you can find the largest salary
  449. 17:52so
  450. 17:53this is
  451. 17:55all salaries which are less than largest
  452. 17:58this is all salaries including the
  453. 18:00largest so what happens if you subtract
  454. 18:04that is from from this if you subtract
  455. 18:07this
  456. 18:07from all salaries if you remove the
  457. 18:10salaries that are not largest naturally
  458. 18:12what you get is the largest salary so
  459. 18:14this is a interesting way to find the
  460. 18:17largest salary we will see later on that
  461. 18:18there could be several other ways
  462. 18:20particularly the use of aggregate
  463. 18:22function which make these computations
  464. 18:24easier to perform
  465. 18:26but these are the typical ways to use
  466. 18:28set theoretic operations
  467. 18:30the set operations ah
  468. 18:32so we have seen three of them union
  469. 18:35intersect and accept
  470. 18:37ah they automatically these operations
  471. 18:39are set theoretics so each of them
  472. 18:41automatically eliminate the duplicate
  473. 18:44unlike what
  474. 18:45sql by default scale by default does
  475. 18:49what
  476. 18:50allows duplicates but set operations
  477. 18:52will eliminate duplicates because they
  478. 18:54are set operations so if you want the
  479. 18:56sql type of behavior if you want the
  480. 18:59duplicates to be preserved
  481. 19:01then you can have a multi set version of
  482. 19:03this set operations which are known as
  483. 19:05union all intersect all except all like
  484. 19:08that
  485. 19:09and naturally ah if you do
  486. 19:12these operations then here is the simple
  487. 19:16formula of the number of tuples that
  488. 19:18will get computed in different cases so
  489. 19:21you can study and convince yourself that
  490. 19:23these are the correct numbers
  491. 19:27ah let us go to ah the treatment of we
  492. 19:29we talked about null values that we said
  493. 19:32that it is possible that
  494. 19:34certain
  495. 19:37records
  496. 19:38in a relation may have one or more
  497. 19:41attributes where the value is not known
  498. 19:43and to represent that the value is not
  499. 19:45known
  500. 19:46we are putting a placeholder called null
  501. 19:50so let us see what is the consequence of
  502. 19:52that null value
  503. 19:54in terms of doing this query operations
  504. 19:57so the null signifies an unknown value
  505. 20:00so if i
  506. 20:00do 5 plus null then naturally the result
  507. 20:03is null
  508. 20:04so what you are saying that i am adding
  509. 20:06an unknown quantity to 5
  510. 20:09so then what would you say is the result
  511. 20:10is unknown so that is the basic
  512. 20:12semantics of adding null to a number
  513. 20:16so it is possible to check
  514. 20:19if
  515. 20:20particularly a field
  516. 20:22is null for a record and that is done by
  517. 20:25a predicate is null so
  518. 20:27ah in this particular query we are
  519. 20:30trying to find all instructors
  520. 20:33whose
  521. 20:34salary is null that is not not known so
  522. 20:38this is a predicate so for a particular
  523. 20:40record for which salary is null
  524. 20:42this will become true and that will get
  525. 20:44included in the result
  526. 20:46but for all records for which there is
  527. 20:48some value for the salary so salary is
  528. 20:50known it is not null those will not get
  529. 20:53included in the result
  530. 20:55so
  531. 20:58the basic ah
  532. 21:01semantics of null is then
  533. 21:04ah combined with the
  534. 21:06truth values
  535. 21:07because we know our basic predicate
  536. 21:10logic is two valued true and false but
  537. 21:12now you have a third value unknown that
  538. 21:14is you may not know the value of a
  539. 21:16predicate so
  540. 21:18how does it ah play around with the true
  541. 21:20and false values ah you can
  542. 21:23reason through that quite easily if you
  543. 21:25are comparing with the null in whatever
  544. 21:27way
  545. 21:28ah then naturally the result is unknown
  546. 21:30so it returns a null
  547. 21:32ah if you are doing any connectives for
  548. 21:34example if you are doing or of
  549. 21:37null or true
  550. 21:39then the result should be true because
  551. 21:41in or
  552. 21:42ah we say that if any of the components
  553. 21:45is true then the result is true so here
  554. 21:47you do not need to know what is that
  555. 21:49unknown value you can say it is true
  556. 21:51but if you do
  557. 21:54if you do
  558. 21:56or with false or of unknown with false
  559. 22:00the the second row or of
  560. 22:03unknown with false
  561. 22:05if you do this
  562. 22:07then naturally this is
  563. 22:09unknown because
  564. 22:11since this is false
  565. 22:13the result would be true only if unknown
  566. 22:16value is true
  567. 22:17and the result would be false if the
  568. 22:19unknown value is false you do not know
  569. 22:20what that unknown value is so you have
  570. 22:22to say that your result is unknown
  571. 22:24so using that same ah
  572. 22:26logic you could ah see verify i would ah
  573. 22:30ask you to verify offline
  574. 22:32at home you please verify that all these
  575. 22:36combinations of true false with unknown
  576. 22:38are valid so
  577. 22:40if p is unknown is ah evalu will is as a
  578. 22:44predicate will evaluate to true if p is
  579. 22:47not known
  580. 22:56now ah we come to the aggregate
  581. 22:58functions ah there are several aggregate
  582. 23:01functions they can be used for
  583. 23:04convenience
  584. 23:05and these are the
  585. 23:08common ones that
  586. 23:09operate on the multi set values
  587. 23:12naturally aggregate functions
  588. 23:14operate on a particular column they try
  589. 23:16to aggregate on a particular column
  590. 23:18and return a single value for example
  591. 23:21average would be meaning that you are
  592. 23:23trying to find average of the values of
  593. 23:26a particular column
  594. 23:29so here is an example
  595. 23:31so we are trying to find the
  596. 23:34average salary of instructors in
  597. 23:37computer science department
  598. 23:39so naturally what you output
  599. 23:42is average salary so mind you this will
  600. 23:45this output relation will have one
  601. 23:48attribute which is average salary
  602. 23:50and
  603. 23:51since average salary
  604. 23:54is a
  605. 23:55single quantity it will only have one
  606. 23:57record
  607. 23:59and here i have made use of this
  608. 24:02aggregate function average so it says
  609. 24:04you do average
  610. 24:05on the attribute salary
  611. 24:09and where do you get that attribute from
  612. 24:10you get fit from the
  613. 24:12table instructor
  614. 24:14and then we are saying that we are not
  615. 24:16interested to find average of salary of
  616. 24:18all instructors
  617. 24:19we are interested to find the average
  618. 24:22salary of those instructors who work for
  619. 24:25computer science
  620. 24:26so you put this where clause
  621. 24:28so this will ensure that you find the
  622. 24:30average salary of instructors in
  623. 24:32computer science department
  624. 24:35so
  625. 24:36in
  626. 24:37similar way you can use other
  627. 24:40[Music]
  628. 24:42aggregate functions like
  629. 24:44if you want to know the total number of
  630. 24:46instructors
  631. 24:48who teach a course in the semester
  632. 24:51so you
  633. 24:53first
  634. 24:54put the where clause naturally you where
  635. 24:56will you find this information you will
  636. 24:58find this information in
  637. 25:01teachers
  638. 25:02teachers is the relation
  639. 25:04which tells you which instructor is
  640. 25:06teaching what course so that comes in
  641. 25:09the from
  642. 25:11then you have to specify that
  643. 25:13teaching a course in spring 2010
  644. 25:15semester so the where clause specifies
  645. 25:18that the semester is spring and the year
  646. 25:20is 2010.
  647. 25:22so this will give you all records
  648. 25:25which show
  649. 25:26that the some instructor is teaching the
  650. 25:30course in spring 2010 semester
  651. 25:33now naturally there could be multiple
  652. 25:36the same instructor could happen
  653. 25:38multiple times because an instructor may
  654. 25:41be teaching more than one course
  655. 25:43so you make the
  656. 25:45instructor id instructor id that you
  657. 25:48have here you make that distinct
  658. 25:51so that you get only those instructors
  659. 25:55every instructor who is ah teaching one
  660. 25:59course
  661. 26:00or more than one course will feature
  662. 26:02only once in this total list
  663. 26:05and then you simply count it
  664. 26:07use aggregate function count on that so
  665. 26:09that will tell you how many instructors
  666. 26:11are
  667. 26:12teaching some course in spring 2010 mind
  668. 26:16you if this is this here is is critical
  669. 26:18to use this keyword
  670. 26:20distinct because unless you use that
  671. 26:23then all that you will eventually find
  672. 26:26out is not the number of instructors who
  673. 26:29are teaching the course you will find
  674. 26:30out the number of courses that are being
  675. 26:32offered in spring 2010 because there
  676. 26:35could be the same instructor teaching
  677. 26:37more than one course
  678. 26:44if you just want to count the number of
  679. 26:46ah tuples you can do
  680. 26:48count on star because what is star star
  681. 26:50is ah all the attributes
  682. 26:53so from
  683. 26:55if you want to find out the number of
  684. 26:56courses you have to count star on course
  685. 27:03so this is showing you the computation
  686. 27:05of ah average salary of instructors in
  687. 27:08each department so now what you want to
  688. 27:10do
  689. 27:11is earlier you try to find out the
  690. 27:14average salary
  691. 27:16in one department now you want that for
  692. 27:18all the departments for each department
  693. 27:20i want so
  694. 27:22my result now is not a single
  695. 27:26row its not a single
  696. 27:28value it is a pair where i show the
  697. 27:31department and the average salary in
  698. 27:34that department so this is what i want
  699. 27:36this
  700. 27:37and this is what i have
  701. 27:40so naturally
  702. 27:42the information comes from instructor
  703. 27:43that is from
  704. 27:45what i want is a department name and the
  705. 27:48average salary
  706. 27:50and i want to give it a nice name abj
  707. 27:52salary so i have done a rename so i get
  708. 27:55a avj salary here
  709. 27:57but then what i want is i do not want an
  710. 28:00average done over this whole set of
  711. 28:02fields
  712. 28:04i want separate average to be done here
  713. 28:07to be done here to be done on this to be
  714. 28:10this so these are these are basically
  715. 28:12groupings by the
  716. 28:14department as you can see that this
  717. 28:18particular
  718. 28:19relation has been sorted according to
  719. 28:21the department name
  720. 28:23so when i want to do
  721. 28:26apply an aggregate function on certain
  722. 28:29subgroups of records
  723. 28:32i use this
  724. 28:34particular
  725. 28:36clause group by
  726. 28:38and use a
  727. 28:40name of a field so what it does is
  728. 28:43if the values in the group by field in
  729. 28:45this case department name are identical
  730. 28:48those records are put together
  731. 28:50and
  732. 28:51over those records an average is
  733. 28:53completed so the average that is
  734. 28:55computed over these records are put in
  735. 28:57here
  736. 28:58average that is computed in terms of
  737. 29:00these records are
  738. 29:02put in here
  739. 29:03these only one records average that is
  740. 29:05computed in terms of that is put in here
  741. 29:08so group by is a very
  742. 29:10useful feature along with the
  743. 29:12aggregation functions and it allows you
  744. 29:15to club
  745. 29:17information
  746. 29:18based on certain attribute and then
  747. 29:20compute the
  748. 29:23aggregation on
  749. 29:25some other field
  750. 29:29mind you ah you will have to when you do
  751. 29:32group by and
  752. 29:34create the result id
  753. 29:36result table you have to make sure that
  754. 29:38all your resultable attributes are used
  755. 29:41in the group by which is not an
  756. 29:43aggregate function so here id is not
  757. 29:45used so this is not a
  758. 29:47ah query that sql would support
  759. 29:56you can further
  760. 29:58refine your result we are saying that
  761. 30:00find names and
  762. 30:02average salary of all departments
  763. 30:04this much you have already done
  764. 30:07now you are qualifying that whose
  765. 30:08average salary is greater than forty two
  766. 30:10thousand
  767. 30:11so of all that we have
  768. 30:14ah for example if we look in here
  769. 30:17ah for example in this music department
  770. 30:20the average salary is less than forty
  771. 30:21two thousand so you do not want that in
  772. 30:23the result
  773. 30:24you want ah only those
  774. 30:26where the average salary is ah greater
  775. 30:28than forty two thousand and the way to
  776. 30:30do that is to have add another clause
  777. 30:33called having
  778. 30:35we say that the average salary is
  779. 30:37greater than forty two thousand so you
  780. 30:40are adding another predicate
  781. 30:42for actually qualifying the aggregated
  782. 30:46value
  783. 30:48now
  784. 30:50the having clause ah actually applies
  785. 30:53after
  786. 30:54along with the group by because
  787. 30:56naturally the having
  788. 30:58relates to the grouping
  789. 31:00so
  790. 31:01once the grouping has happened groups
  791. 31:04have been formed then
  792. 31:06the having clause will be evaluated on
  793. 31:09that
  794. 31:10in contrast
  795. 31:12where clause also has a predicate but
  796. 31:15the where clause is
  797. 31:17applied before forming the groups so
  798. 31:20this point this note has to be
  799. 31:22understood carefully because ah if you
  800. 31:24have a wire clause to choose the records
  801. 31:26they will first apply
  802. 31:28then out of those records chosen the
  803. 31:31grouping will happen and once the
  804. 31:33grouping has happened then
  805. 31:35the aggregate function will evaluate and
  806. 31:38the having clause will get evaluated the
  807. 31:40predicate of having clause will get
  808. 31:42evaluated
  809. 31:45certainly if there are null values ah in
  810. 31:47terms of aggregates then ah there is a
  811. 31:50question of what will happen
  812. 31:51so
  813. 31:52the
  814. 31:54general strategy is that whenever you
  815. 31:57perform aggregation then the null values
  816. 32:00are all ignored
  817. 32:01so if on that field there is no value
  818. 32:05which is not null that is if all values
  819. 32:07are null then the result is null
  820. 32:09otherwise the result is computed by
  821. 32:11ignoring the null values
  822. 32:14so these are
  823. 32:15what you have of course
  824. 32:18if you count
  825. 32:19then
  826. 32:21if the collection has only null values
  827. 32:23the count will return you 0 but all
  828. 32:26other aggregates will return you simply
  829. 32:28null
  830. 32:30so to summarize ah we have we had
  831. 32:33started the basic
  832. 32:34understanding of the basics query
  833. 32:36structure in the last module now we have
  834. 32:38completed that with some more additional
  835. 32:41ah operations we have understood the set
  836. 32:44theoretic operations
  837. 32:46and very importantly we have
  838. 32:48familiarized with
  839. 32:49the treatment of null values and
  840. 32:52aggregation functions particularly the
  841. 32:55group by and having clauses and how do
  842. 32:58null values and aggregation interact in
  843. 33:01terms of an sql query

About this transcript

This page contains the full transcript of Introduction to SQL/2 by Data Base Management System - IITKGP, generated from the public captions YouTube serves with the video. The transcript has 4,528 words across 843 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.