YouTube2Text

Introduction to SQL/3 — Transcript

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

Full transcript

  1. 0:00[Music]
  2. 0:16welcome to module 8 of database
  3. 0:19management systems
  4. 0:20we have been discussing
  5. 0:22basic sql queries and this is the third
  6. 0:26and
  7. 0:27closing part of that introductory
  8. 0:29discussion that we started in the sixth
  9. 0:31module
  10. 0:33so just to quickly recap this is ah
  11. 0:36these are the things that we did in the
  12. 0:38last module completing the understanding
  13. 0:41of basic operations the null values and
  14. 0:43aggregate functions
  15. 0:45in the current module we want to
  16. 0:47understand a feature which is very very
  17. 0:50important in sql query forming is called
  18. 0:53the
  19. 0:54nested query or more
  20. 0:56formally nested sub query in sql
  21. 1:00and
  22. 1:00we would like to understand the process
  23. 1:02of data modification
  24. 1:05and those are the two things that are
  25. 1:07outlined here so lets start with nested
  26. 1:10sub queries
  27. 1:15a sub query is necessarily
  28. 1:18a select from where expression
  29. 1:21that is nested within another query
  30. 1:24this is these are the key things
  31. 1:28where expression
  32. 1:30so its nothing new
  33. 1:32over what we have already learnt
  34. 1:35but
  35. 1:36it is a part of another query it is
  36. 1:38nested within another query and that is
  37. 1:40the reason is called a sub query
  38. 1:43so it by itself is not the result
  39. 1:46this will be used
  40. 1:47this is a select from where expression
  41. 1:50that will be used in a nested form in
  42. 1:52another
  43. 1:53some other query
  44. 1:55to actually generate the result
  45. 1:58so thats a nested sub query
  46. 2:04so
  47. 2:05the nesting can be done in
  48. 2:09one or more of the three clauses that
  49. 2:12a
  50. 2:13select from where
  51. 2:15sql query has
  52. 2:18an attribute can be replaced
  53. 2:20a relation ah can be
  54. 2:23any valid sub query
  55. 2:24or
  56. 2:25a sub query could form the part of a
  57. 2:28predicate in the where clause
  58. 2:30all of these are possible so we will
  59. 2:32discuss them one by one
  60. 2:35so first we will start by discussing how
  61. 2:38sub queries
  62. 2:40work in the where clause
  63. 2:44the common use for you having sub
  64. 2:47queries in the where clause
  65. 2:49is to perform different kind of tests
  66. 2:52for membership comparison
  67. 2:55cardinality and so on
  68. 3:00so let us look at this you have already
  69. 3:02seen this query before find courses
  70. 3:04offered in fall 2009
  71. 3:07and in fall in spring 2010 earlier we
  72. 3:11have shown ah different ways of coding
  73. 3:13this now we are showing yet another way
  74. 3:15to be able to code this in sql
  75. 3:18so first
  76. 3:20from the from the beginning certainly we
  77. 3:22need the courses so the select clause is
  78. 3:25trivial
  79. 3:26it has to be the distinct course ids
  80. 3:28certainly the information will come from
  81. 3:30section which has a
  82. 3:33information about offering of courses as
  83. 3:35we have also seen already
  84. 3:38ah
  85. 3:40so those two are
  86. 3:42no brainer
  87. 3:44now let us see
  88. 3:45how do i find courses that are offered
  89. 3:47in fall 2009 and in spring 2010. so
  90. 3:51again the first part the courses offered
  91. 3:54in fall 2009 is coded in this part
  92. 3:58in part of the where where clause
  93. 4:00predicate where is the semester has to
  94. 4:01be fault and year is two thousand nine
  95. 4:04so this part is also done
  96. 4:07now i need to ensure that ah whatever
  97. 4:10i mean if i assume that ah
  98. 4:14after this this part were not there
  99. 4:17then this will only give me courses
  100. 4:20which are offered in fall 2009
  101. 4:24but we want the courses that are in fall
  102. 4:252009
  103. 4:27and in
  104. 4:28spring 2010
  105. 4:30so we do something interesting what i do
  106. 4:32is we write a separate query here
  107. 4:37which is courses that are offered in
  108. 4:40spring 2010 select course id just in the
  109. 4:43same way select course id section
  110. 4:45semester ah year
  111. 4:47so this particular query will give me
  112. 4:51the
  113. 4:52courses offered in spring two thousand
  114. 4:54nine so what do we have in one part
  115. 4:58i have so if you look at this part
  116. 5:01this courses ah that happened in fall
  117. 5:03two thousand nine if you look at this
  118. 5:05part
  119. 5:07courses that happened
  120. 5:08in spring 2010
  121. 5:11good
  122. 5:12now what i want i need that
  123. 5:15the it i am interested in the courses
  124. 5:18that happen in both
  125. 5:20so
  126. 5:21for a course that exists here
  127. 5:24i want to specify that that course id
  128. 5:28that course id
  129. 5:30must be present here
  130. 5:34so what i am checking for i am checking
  131. 5:36for it
  132. 5:39set membership
  133. 5:40this is a set right
  134. 5:43so i am trying to check whether that
  135. 5:45course id which is
  136. 5:47being selected in the first part
  137. 5:49exist in in is a is another keyword in
  138. 5:54this particular
  139. 5:56this particular relation
  140. 5:57that is specified by the second
  141. 6:00part of this query which is courses
  142. 6:03offered in spring 2010
  143. 6:05if it is
  144. 6:07if the core side is present then that
  145. 6:08course must have been offered in both
  146. 6:10the semesters
  147. 6:12if it is not present then it is offered
  148. 6:14only in fall 2009 and not in spring 2010
  149. 6:18and certainly the courses that were not
  150. 6:20offered in fall 2009 and only offered in
  151. 6:23spring 2010 will feature here
  152. 6:25but they do not exist here so they will
  153. 6:27never come up for test
  154. 6:29so as a result what i get finally is the
  155. 6:32effect of computing courses that are
  156. 6:35offered in fall 2009 and in spring 2010
  157. 6:40this part of the query which i used
  158. 6:43as a part of the where clause
  159. 6:45is my nested sub query and in this case
  160. 6:48as you have seen it is used for
  161. 6:50set membership
  162. 6:52and this is a basic idea of ah
  163. 6:56using nested sub queries that is a
  164. 6:58nested sub query will always give you a
  165. 7:00relation
  166. 7:01so you try to put that relation in the
  167. 7:03right context of the where clause from
  168. 7:06clause or select clause and then make
  169. 7:08use of it in building up your logic
  170. 7:11so let us run through some examples
  171. 7:14ah this is what you are saying is is ah
  172. 7:17earlier one was the courses offered in
  173. 7:19both here we are trying to do kind of
  174. 7:21the difference saying the course is
  175. 7:23offered in fall two thousand nine but
  176. 7:25not in spring two thousand ten certainly
  177. 7:27we easily get that by changing the
  178. 7:30membership to negation of the membership
  179. 7:33earlier it was in now you do not in you
  180. 7:35will simply get that it is ah up to you
  181. 7:38to take some examples and convince
  182. 7:40yourself that this kind of a nesting
  183. 7:42will work
  184. 7:44ah we find the set of the find the total
  185. 7:47number of
  186. 7:48distinct students we have taken the
  187. 7:49course
  188. 7:51section taught by the instructor id some
  189. 7:54id is given
  190. 7:55so again we form a nested query
  191. 7:58this is a nested query which
  192. 8:01ah tells me the courses taught by
  193. 8:04this particular teacher one zero one
  194. 8:05zero one
  195. 8:07and then we check
  196. 8:11set membership in terms of
  197. 8:13this course id section id that is fields
  198. 8:16of the
  199. 8:16takes ah relation
  200. 8:19to see that whether that particular
  201. 8:23tuple can exist
  202. 8:25in the course offered by the specific
  203. 8:28teacher
  204. 8:29if it does
  205. 8:30then take out
  206. 8:32that id
  207. 8:33which is which will turn out to be
  208. 8:36the student id in this case
  209. 8:39because that is the text relation has
  210. 8:41the student id take out the student id
  211. 8:43and count it as distinct so this can
  212. 8:45simply give you the answer to this query
  213. 8:52obviously we we agree that this can be
  214. 8:55written in a simpler form also but we
  215. 8:58are including it here just for the
  216. 9:00sake of illustrating the feature
  217. 9:04ah
  218. 9:06there is another
  219. 9:08clause
  220. 9:10called the sum clause ah look at this
  221. 9:13find names of instructor salary is
  222. 9:14greater than
  223. 9:15that of sum which means at least one
  224. 9:18instructor in biology department and we
  225. 9:21have already seen this coding before
  226. 9:24now we can do this in terms of the
  227. 9:26nested query by using
  228. 9:29again
  229. 9:31this is
  230. 9:32certainly the salary of instructors in
  231. 9:35biology department
  232. 9:37and we are doing greater than sum
  233. 9:39that means that the salary here
  234. 9:43being checked
  235. 9:45must find at least one record here
  236. 9:48so that it is greater than that salary
  237. 9:50value
  238. 9:51so its greater than sum
  239. 9:53is a nice way to find existential
  240. 9:56records
  241. 9:57using the nested sub query
  242. 10:01the logic of some clause ah i have
  243. 10:04detailed out here so we will not go
  244. 10:06through each one of them
  245. 10:08ah in this discussion i leave it on to
  246. 10:10you to study and convince yourself that
  247. 10:12you understand
  248. 10:13the semantics of some
  249. 10:16so similarly we have an all clause
  250. 10:18which say that ah if we want to say the
  251. 10:21find the names of all instructors whose
  252. 10:23salary is greater than the salary of all
  253. 10:25instructors in the biology department in
  254. 10:27place of sum we can write
  255. 10:29we will write all and it will check
  256. 10:32every salary will check with
  257. 10:34the whole
  258. 10:36set of salaries in this sub query and
  259. 10:38only if that is greater
  260. 10:41then
  261. 10:42that particular record that particular
  262. 10:44name will be included in the result
  263. 10:46otherwise it will be excluded from the
  264. 10:48result
  265. 10:52similar to sum there is a
  266. 10:55basic semantics of all which is also
  267. 10:57worked out here and i leave that to your
  268. 11:02study at home
  269. 11:06you can test for empty relations by
  270. 11:08using the exists
  271. 11:10construct
  272. 11:12so if you say exists r
  273. 11:14then that is a predicate
  274. 11:16which
  275. 11:17mean that r is not empty if r is empty
  276. 11:22then
  277. 11:23exist is false
  278. 11:26and not exist is the
  279. 11:29negation of exist
  280. 11:30so it can be used to
  281. 11:34specify
  282. 11:36the query like find all courses taught
  283. 11:38both in
  284. 11:39fall 2009 and spring 2010
  285. 11:43so all that you have to do earlier you
  286. 11:46did it by set membership
  287. 11:48so
  288. 11:49now you are trying to do this by this
  289. 11:51exist
  290. 11:52so you are saying that this is again the
  291. 11:54same query which gives you the courses
  292. 11:58that
  293. 11:58are in spring
  294. 12:002010
  295. 12:01and
  296. 12:02also in
  297. 12:04this
  298. 12:06fall fall 2009
  299. 12:09and
  300. 12:11you check whether
  301. 12:13this
  302. 12:15relation whether this particular
  303. 12:18nested query
  304. 12:20is an empty one or not if it is an empty
  305. 12:22one then exist will fail and the whole
  306. 12:25clause will fail it is not an empty one
  307. 12:28then you have found such an entry it was
  308. 12:30offered and therefore it will get
  309. 12:32included
  310. 12:33so these are just different ways of
  311. 12:35expressing similar things but what you
  312. 12:38should note is
  313. 12:40the nested sub query is a very
  314. 12:42convenient way to frame up the logic in
  315. 12:44multiple different ways
  316. 12:46that you would like to do
  317. 12:48so these are the different names given
  318. 12:50the correlation name and the correlated
  319. 12:52sub query ah incidentally
  320. 12:55the nested query is often referred to as
  321. 12:57the inner query
  322. 12:59and
  323. 13:00the query in which the nesting has
  324. 13:02happened is known as the outer query
  325. 13:05ah here is another example which
  326. 13:06illustrate the use of not exist
  327. 13:10so which i leave it for your own study
  328. 13:17we can check for uniqueness that is test
  329. 13:20for absence of duplicate tuples
  330. 13:23by using the unique keyword so we can
  331. 13:27you can see here that here is a nested
  332. 13:30query and
  333. 13:32using unique to find out all courses
  334. 13:34that were offered at most once in 2009
  335. 13:38so
  336. 13:39if it a course was offered more than
  337. 13:41once then naturally multiple records
  338. 13:43will feature
  339. 13:45and the result the unique will fail
  340. 13:48unique will be true only if there is
  341. 13:50only one entry which shows that it is
  342. 13:53offered at most once in that semester
  343. 14:00now we move on ah so we have been
  344. 14:01discussing about ah
  345. 14:04sub queries in the where clause now we
  346. 14:06move on to sub queries in the from
  347. 14:08clause
  348. 14:09so
  349. 14:11as we have already seen a nested sub
  350. 14:14query is a relation so it can
  351. 14:17naturally be used in the place of any
  352. 14:20relation that we have in the from clause
  353. 14:24so
  354. 14:25we are trying to find out
  355. 14:27average
  356. 14:29instructor salaries of those departments
  357. 14:31where the average salary is greater than
  358. 14:33forty two thousand
  359. 14:36so
  360. 14:36look at
  361. 14:41this is a nested sub query so where what
  362. 14:45is been found here this will compute
  363. 14:48the average salary department wise
  364. 14:51average salary which we have already
  365. 14:53seen group by department name and then
  366. 14:55you do average on the salary and you
  367. 14:57give it a give me that field a new name
  368. 15:00so which means that this is equivalent
  369. 15:03to having
  370. 15:04a
  371. 15:06ah relation which has two attributes
  372. 15:16depth name
  373. 15:17and avj salary
  374. 15:20so from that you are now trying to
  375. 15:23do the
  376. 15:24selection
  377. 15:25and what is the condition that the
  378. 15:27average salary has to be written so you
  379. 15:29already have that as a part of the field
  380. 15:31the average salary
  381. 15:32so or you just need to put that in the
  382. 15:34where clause
  383. 15:36and you have
  384. 15:37only those
  385. 15:39coming out of this particular relation
  386. 15:41where average salary is greater than 42
  387. 15:43000
  388. 15:44to be selected in this select query so
  389. 15:46these will
  390. 15:48feature in the output of this selection
  391. 15:52so that is how you can very easily use
  392. 15:54ah
  393. 15:57a nested sub query in the from clause
  394. 16:00and
  395. 16:01for this we did earlier we solve this
  396. 16:03problem using the having clause but here
  397. 16:06we will not
  398. 16:07need we did not need the having clause
  399. 16:09to do this
  400. 16:16there is ah this is another way
  401. 16:19so
  402. 16:20here what we have done is
  403. 16:22the same
  404. 16:23if you if you look into the
  405. 16:27nested sub query
  406. 16:29this is
  407. 16:32actually the same
  408. 16:34all that we have done we have given it a
  409. 16:36new name
  410. 16:38by
  411. 16:39the renaming feature and then this as if
  412. 16:42becomes a relation and on that the
  413. 16:44computation is done
  414. 16:46rest of it is similar
  415. 16:50there is a width clause
  416. 16:52that provides a way of
  417. 16:56computing a temporary relation and that
  418. 16:58can be subsequently used so let us look
  419. 17:00at example
  420. 17:02so
  421. 17:03we are trying to find
  422. 17:04all departments with maximum budget
  423. 17:08so this
  424. 17:10is my basic query
  425. 17:13we want to find department name
  426. 17:16department dot name that is the
  427. 17:18department's name
  428. 17:20from
  429. 17:21the department table
  430. 17:25and
  431. 17:27the
  432. 17:30budget must be same as a
  433. 17:32maximum budget
  434. 17:35so for that
  435. 17:37i need to know
  436. 17:39the maximum budget
  437. 17:42that
  438. 17:43exist across the department
  439. 17:46so look at what has been done here
  440. 17:49we have a nested query
  441. 17:52which aggregates
  442. 17:56the maximum
  443. 17:57budget from the departments so this
  444. 18:00gives you the value of the
  445. 18:03maximum budget
  446. 18:05we make that
  447. 18:07into a temporary table max budget
  448. 18:12with an attribute value so this is
  449. 18:14renaming
  450. 18:16so you can see the renaming is being
  451. 18:17used in in very interesting ways so this
  452. 18:20is my nested query that gives me a t
  453. 18:22relation
  454. 18:23and this is my
  455. 18:25definition of the relation so max budget
  456. 18:28now is a temporary relation a relation
  457. 18:31that i use subsequently in my from
  458. 18:33clause
  459. 18:34and width allows me to do that
  460. 18:37this relation will not be available
  461. 18:39otherwise after this query this relation
  462. 18:41will not exist this is just a temporary
  463. 18:43one computed for this query
  464. 18:45so this gives me the budget value this
  465. 18:47gives me the department and department
  466. 18:49specific budget
  467. 18:51and
  468. 18:53this condition tells me that
  469. 18:56i can choose all the departments which
  470. 18:58has the maximum budget
  471. 19:01very nice way of
  472. 19:02using this
  473. 19:04so
  474. 19:05with clause ah can be used in ah even
  475. 19:08more involved way again this is an
  476. 19:10example which is
  477. 19:12ah more complex use and i leave it to
  478. 19:14you to
  479. 19:15practice
  480. 19:16study and understand
  481. 19:20we move on to sub queries in the select
  482. 19:23clause finally
  483. 19:27a scalar sub query is one where
  484. 19:30there is a single value is expected
  485. 19:32so
  486. 19:33we can very easily use that
  487. 19:36in the select so
  488. 19:38what if you look at
  489. 19:41this part which is the sub query
  490. 19:43so you are saying list all departments
  491. 19:45along with number of instructors each
  492. 19:47department has
  493. 19:49so
  494. 19:54this condition
  495. 19:56tells that
  496. 19:58the
  497. 19:59from the instructor we are taking out
  498. 20:02those
  499. 20:03that department name where
  500. 20:07the instructor works
  501. 20:10so you count them
  502. 20:12and then you form that in as a
  503. 20:15new attribute mind you in while we were
  504. 20:18using this in the from clause
  505. 20:21we were treating that as a relation
  506. 20:23because nested query will give a
  507. 20:24relation
  508. 20:26but here in the select clause
  509. 20:28the entities are attributes
  510. 20:31so
  511. 20:32this as
  512. 20:35is renaming of attribute which mean but
  513. 20:37this is a relation
  514. 20:39that is why this notion of scalar sub
  515. 20:41query is required
  516. 20:43that is
  517. 20:44the though this is a relation
  518. 20:47what does the relation compute it
  519. 20:48computes a single value
  520. 20:51so that
  521. 20:52value
  522. 20:53we are putting as
  523. 20:55an
  524. 20:56attribute name
  525. 20:58num instructors
  526. 21:00so we have department name and the
  527. 21:03number of instructor there in
  528. 21:07for each and every department that we
  529. 21:10actually have from the department list
  530. 21:13so it is a very
  531. 21:15interesting way of using
  532. 21:17this ah nested sub query in terms of the
  533. 21:22select loss
  534. 21:26naturally since this in the select loss
  535. 21:28i cannot have
  536. 21:30ah
  537. 21:31i mean every entry in the select clause
  538. 21:34has to be an attribute
  539. 21:36pure relations are not possible
  540. 21:38so
  541. 21:39if if
  542. 21:40the sub query returns more than one
  543. 21:42tuple which cannot be conceived as a
  544. 21:45as a
  545. 21:47as one or more attributes
  546. 21:49then it will be a runtime error that
  547. 21:52will not be allowed
  548. 21:54because we do not know how to handle
  549. 21:56multiple
  550. 21:57tuples in terms of a select clause
  551. 22:01ok
  552. 22:03ah next we move on to ah discussing the
  553. 22:07modifications
  554. 22:09to the database how do we modify the
  555. 22:12database
  556. 22:13so we will look into some of the ways
  557. 22:16of
  558. 22:18changing the records or removing records
  559. 22:21from that earlier we saw a delete of
  560. 22:24record which removed all records from a
  561. 22:26relation
  562. 22:27but now we will see selective deletion
  563. 22:30insertion and update of values
  564. 22:34now deleting all uh instructors are easy
  565. 22:36delete from instructor all records are
  566. 22:39released and this becomes an empty table
  567. 22:41but
  568. 22:42suppose we want to delete all
  569. 22:44instructors from the finance department
  570. 22:46then
  571. 22:48like we do in the select from where
  572. 22:51we again
  573. 22:52use the where clause as a predicate and
  574. 22:55say that delete from instructor but you
  575. 22:57do the deletion provided this condition
  576. 22:59is satisfied that is department name is
  577. 23:02same as finance
  578. 23:04so it is very similar to the select from
  579. 23:06where but the effect is unlike select
  580. 23:08from where
  581. 23:10where no tables change in the database
  582. 23:13here the table is actually changing
  583. 23:16because these instructor records are
  584. 23:18deleted whose department name was
  585. 23:20financed
  586. 23:22the third example shows delete all
  587. 23:23tuples in the instructed relation for
  588. 23:26those instructor associated with the
  589. 23:28department
  590. 23:29located in the watson building
  591. 23:32so a dwetson building may have multiple
  592. 23:34departments so all ah
  593. 23:37instructors
  594. 23:39who worked on those departments which
  595. 23:41are located in the watson building that
  596. 23:43you remove
  597. 23:44so you do
  598. 23:46this is again you are using nested query
  599. 23:49now you know how to use an estate query
  600. 23:51so
  601. 23:53you use nested query which will give you
  602. 23:55the names of departments
  603. 23:58which it gives you a relation with the
  604. 24:00single attribute with names of
  605. 24:02departments housed in the watson
  606. 24:04building
  607. 24:05then you use
  608. 24:07the set membership to check whether a
  609. 24:09particular department
  610. 24:11belongs to that set
  611. 24:12if it does then it is in watson building
  612. 24:14otherwise it is not in watson building
  613. 24:17if it does belong to the watson building
  614. 24:19then this where clause becomes true and
  615. 24:21the corresponding instructor record is
  616. 24:23deleted
  617. 24:24and that is how this whole
  618. 24:27ah different kinds of selective deletion
  619. 24:29can happen
  620. 24:32delete all the instructors so salary is
  621. 24:34less than the average salary of
  622. 24:36instructor
  623. 24:37again this is
  624. 24:38so you compute
  625. 24:40the selection
  626. 24:44sub query which computes the average
  627. 24:46salary
  628. 24:47and check if the salary is less than
  629. 24:50the average salary and delete that
  630. 24:53sounds ah straight forward but just just
  631. 24:56wait a while just just wait just wait i
  632. 24:58mean did we do it do a right thing
  633. 25:02an average salary is
  634. 25:04computed
  635. 25:05by
  636. 25:06taking
  637. 25:08the sum of all salaries in the relation
  638. 25:11and then dividing it by the number of
  639. 25:13relations this has to be the average
  640. 25:14salary
  641. 25:16certainly if i remove a record
  642. 25:19then the average itself will change
  643. 25:24so if i write the query in this manner
  644. 25:29then what i am saying
  645. 25:31on the face of it looks correct but then
  646. 25:34actually can it be correct because the
  647. 25:36moment a condition is satisfied
  648. 25:38and that record is deleted
  649. 25:41this average value itself has changed
  650. 25:45so that is not so that will depend
  651. 25:47then the result will depend on the order
  652. 25:49in which the deletion is happening but
  653. 25:52that is not what was meant what is meant
  654. 25:53is
  655. 25:54take all the records for the present
  656. 25:56find out the average find out all
  657. 26:00records which have a salary less than
  658. 26:02that average and remove them
  659. 26:04so this
  660. 26:06initially you know easy trivial looking
  661. 26:09solution is not actually correct
  662. 26:12so
  663. 26:13you will have to do the solution in two
  664. 26:16stages first compute the average salary
  665. 26:19find all tuples to delete next delete
  666. 26:22all tuples found above
  667. 26:24without recomputing the average
  668. 26:27in the present solution the average is
  669. 26:29recomputed which is the wrong thing
  670. 26:33so again i will leave that for you to
  671. 26:36solve
  672. 26:37we move on ah to looking at
  673. 26:39modifications in terms of insertion
  674. 26:42so
  675. 26:43we had seen ah this earlier we can add a
  676. 26:45new tuple by insert into
  677. 26:48ah then the relation name then you say
  678. 26:50values and the tuple of values
  679. 26:52we can
  680. 26:54specify the
  681. 26:56if we
  682. 26:57if we do not remember the order of
  683. 27:00attributes in the relation
  684. 27:03then we can also specify
  685. 27:05the order in which we are speci
  686. 27:07actually giving the information so you
  687. 27:09are saying insert into course and what
  688. 27:11we have done here is we have actually
  689. 27:13specified the order in which the
  690. 27:15attributes occur and that order and the
  691. 27:17order of values must be the same
  692. 27:20in the first case
  693. 27:22this order of values is
  694. 27:24decided
  695. 27:25by the order of the attributes that
  696. 27:28exist
  697. 27:29in terms of the period table
  698. 27:34ah add a new tuple to student with total
  699. 27:37credits set to null
  700. 27:39that is i do not know if you are adding
  701. 27:41a student initially it does not have a
  702. 27:42credit right
  703. 27:43the credit is a nullable field the
  704. 27:45credit will be earned after the student
  705. 27:47has gone through the courses and all
  706. 27:49that
  707. 27:50so
  708. 27:51if i do not know the value of a field
  709. 27:52then i can write n u l l null which is a
  710. 27:55special value
  711. 27:56designating unknown for at the time of
  712. 27:59insertion
  713. 28:01add all instructors to the student
  714. 28:03relation with total credit set to 0.
  715. 28:06so
  716. 28:07i can also combine insert with select
  717. 28:12so we are taking the first part this
  718. 28:15part
  719. 28:16is selection
  720. 28:18which generates a whole lot of records
  721. 28:20having
  722. 28:21id name department name and the total
  723. 28:24credit set to 0
  724. 28:25from the instructor
  725. 28:27and
  726. 28:28ah insert them into the students
  727. 28:33so these will get instruct in inserted
  728. 28:36into the students
  729. 28:39select fire statement is evaluated fully
  730. 28:42so this first select form will be done
  731. 28:45before any of its results are inserted
  732. 28:47in the relation so that is the basic
  733. 28:49condition that sql
  734. 28:52guarantees
  735. 28:54because if that were not the case then
  736. 28:57such situations will become circular and
  737. 29:00will cause problem
  738. 29:05updates can be done
  739. 29:06based on particular values so you can
  740. 29:09update a table based
  741. 29:11and what it means that it you could
  742. 29:14update the values of specific fields
  743. 29:17so here in the in the first case we are
  744. 29:20giving trying to give a three percent
  745. 29:23salary raise
  746. 29:25for salaries which are more than hundred
  747. 29:27thousand and
  748. 29:29some five percent raise for salaries
  749. 29:30which are less than equal to hundred
  750. 29:32thousand
  751. 29:33and mind you
  752. 29:35this order
  753. 29:37in which you do the update is important
  754. 29:39because if you do the
  755. 29:40later update first then someone who was
  756. 29:44what qualified in the later part was
  757. 29:45less than hundred thousand
  758. 29:47with the increase will become more than
  759. 29:49hundred thousand and will also qualify
  760. 29:51for the second one so that will become
  761. 29:52wrong so update often
  762. 29:55is dependent on the order and therefore
  763. 29:58you have yet another
  764. 30:00ah
  765. 30:00[Music]
  766. 30:02feature to take care of this when you
  767. 30:04have a specific order to do things
  768. 30:07it is called the case
  769. 30:09so you say
  770. 30:10when salary case is a new keyword
  771. 30:13when is a keyword when salary is less
  772. 30:16than equal to hundred thousand then this
  773. 30:18is how you hike otherwise this is how
  774. 30:20you hike so
  775. 30:21it can its looks more like the if
  776. 30:24statement of c c plus plus
  777. 30:29you can do updates with
  778. 30:31scalar sub queries we have seen scalar
  779. 30:34sub queries ah already so you can use a
  780. 30:37scalar sub query again ah i would not go
  781. 30:41through the details ah please study and
  782. 30:44you will be able to understand
  783. 30:48so these are different examples
  784. 30:51so ah to summarize ah we have introduced
  785. 30:54a very powerful feature
  786. 30:57ah in sql query known as a nested sub
  787. 31:00query where we can write a
  788. 31:02select from where expression
  789. 31:05as a part of the where
  790. 31:08predicate
  791. 31:09or as a relation in the from clause
  792. 31:12or
  793. 31:13as
  794. 31:14one or more collection of attributes in
  795. 31:16the
  796. 31:17select clause and it can be used in
  797. 31:20several other places also we have seen
  798. 31:23the ways to perform data modification in
  799. 31:27terms of deleting inserting and updating
  800. 31:29records
  801. 31:30and we have also seen how nested sub
  802. 31:33query often may be very useful not only
  803. 31:36in terms of performing a query but also
  804. 31:39in terms of performing certain data
  805. 31:42modifications

About this transcript

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