YouTube2Text

Joins in sql server - Part 12 — Transcript

by kudvenkat · 2,828 words · 420 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hello, welcome to PR Technologies. I am
  2. 0:02Winkit. This is part 12 of SQL Server.
  3. 0:05In this session, we'll understand the
  4. 0:07different types of joins that are
  5. 0:09available in SQL Server. Specifically,
  6. 0:11cross join, inner join, and outer joins.
  7. 0:14Before continuing with this session, I
  8. 0:16strongly recommend watching parts three
  9. 0:18and five of this video series.
  10. 0:22Now, joins in SQL Server are used to
  11. 0:25retrieve data from two or more related
  12. 0:27tables. In general, tables are related
  13. 0:29to each other using foreign key
  14. 0:31constraints. So I have this employee
  15. 0:33table on the right here which has got ID
  16. 0:36name, gender, salary and department ID
  17. 0:38columns. And then I have the department
  18. 0:40table itself here which has got the
  19. 0:42department name the location that
  20. 0:45department is present at and the
  21. 0:46department head. Now if you look at
  22. 0:48these these two tables are related using
  23. 0:51this foreign key department ID. Now, if
  24. 0:54you're not sure what foreign key is,
  25. 0:56please watch part three of this video
  26. 0:58series. So, these two are related
  27. 1:00tables. Now, let's say I ask you to
  28. 1:02write a query which will give me an
  29. 1:04output as shown here on the left hand
  30. 1:06side. I want the name of the employee,
  31. 1:09gender, salary and the department name.
  32. 1:12And if you look at the output columns
  33. 1:13that I require the first three columns
  34. 1:16name, gender and salary they are present
  35. 1:19in the employee table. Whereas the
  36. 1:21department name is present in the
  37. 1:23department's table. So both you know
  38. 1:25these columns all these columns are not
  39. 1:28coming from a single table. They're
  40. 1:30spread across two different tables. So
  41. 1:32obviously if I have to retrieve this
  42. 1:34output then I will have to join these
  43. 1:36two tables. Okay. So to join these two
  44. 1:39tables in SQL Server, we have got
  45. 1:42different types of joins for example we
  46. 1:44have inner join, outer join and cross
  47. 1:46joins. And again the outer joins in SQL
  48. 1:49server which we'll be talking about in a
  49. 1:51bit are divided into three different
  50. 1:54categories. Left outer join, right outer
  51. 1:56join or full and full outer join. Okay,
  52. 1:59you can say left outer, right outer,
  53. 2:02full outer or you can just say left
  54. 2:03join, right join or full join.
  55. 2:08Now let's talk about inner join. Now
  56. 2:10let's say you know I want output like
  57. 2:13this. You know I want the name of the
  58. 2:14person, their gender, salary and the
  59. 2:17department name. Okay. So let's see how
  60. 2:20we do this using inner join. Okay. And
  61. 2:23look at this. If you have two tables you
  62. 2:26know in the inner join the matching rows
  63. 2:30between the two tables are retrieved.
  64. 2:31For example, if you look at the employee
  65. 2:33table here, you know, everybody has
  66. 2:36department ID except James and Russell.
  67. 2:40If you look at James and Russell's
  68. 2:41record, the department ids are null,
  69. 2:44which means these rows doesn't have a
  70. 2:48matching department ID from the
  71. 2:50department, you know, departments table.
  72. 2:52So when I join these two tables using
  73. 2:55inner join only the matching rows
  74. 2:57between these two tables are retrieved
  75. 3:00which means the matching rows between
  76. 3:02these two tables are you know these
  77. 3:04eight rows from 1 till 8 which is what
  78. 3:08I'm getting. And if you look at the
  79. 3:09output I don't get James and Russell's
  80. 3:12records. Why? Because their department
  81. 3:14ID does not match with the ID column in
  82. 3:18the department's table. So inner join
  83. 3:21will only give you matching rows between
  84. 3:24both the tables involved in a join. Now
  85. 3:26let's see how to write the inner join
  86. 3:28query itself.
  87. 3:30So if you look at the two tables that we
  88. 3:32have
  89. 3:35in employee table we have got 10 rows
  90. 3:37and in the departments table we have got
  91. 3:39four rows and what do we want? We want
  92. 3:42the name, gender, salary and department
  93. 3:44name columns in the output. from which
  94. 3:47table? From tbl employee table and you
  95. 3:51know the first three columns are present
  96. 3:53in tbl employee table but the department
  97. 3:56name column is present in the
  98. 3:57department's table. So I will have to
  99. 3:59join with that table and to join with
  100. 4:01that table you use the join keyword.
  101. 4:05So tbl
  102. 4:08department so we are saying I want to
  103. 4:10join tbl employee table with tbl
  104. 4:13department table. Okay. Now if you want
  105. 4:16to join these two tables, how do you
  106. 4:18want to join these two tables? What's
  107. 4:19the common column between them? The
  108. 4:22common column is the department ID
  109. 4:24column. And to specify the join
  110. 4:26condition, you use the on clause. So on
  111. 4:30tbl employee,
  112. 4:33so in the table employee, we have the
  113. 4:36department ID column. So use that to
  114. 4:39look up tbl department table
  115. 4:43and the ID column within that table.
  116. 4:45Okay. So it's pretty simple. Select
  117. 4:48whatever columns you want from you know
  118. 4:52tbl employee. Now I want to join this
  119. 4:56table with the tbl department. So we use
  120. 4:58the join keyword on how do you want to
  121. 5:01join those two tables. Now between these
  122. 5:03two tables the common column is the
  123. 5:05department ID. you know department ID in
  124. 5:07tbl employee table join that with ID
  125. 5:10column in tbl department table. So
  126. 5:14obviously if we execute this query
  127. 5:18we should get eight records. Okay now if
  128. 5:21we execute all of them together
  129. 5:25okay and if you look at the output here
  130. 5:27in the messages in TBL employees table
  131. 5:29we got eight rows in department we got
  132. 5:31four rows and in the join query we only
  133. 5:34got eight rows. Why? Because if you look
  134. 5:37at James and Russell, their records
  135. 5:41doesn't have a matching department ID.
  136. 5:43That's why inner join has not returned
  137. 5:46those two records. So if you look at the
  138. 5:49join output,
  139. 5:52we don't have James and Russell's
  140. 5:54records in the output. We only have the
  141. 5:56matching records between these two
  142. 5:58tables. Okay. And when you write uh an
  143. 6:02inner join query, you can say inner join
  144. 6:06or just join. I would say always write
  145. 6:08inner join because this will say your
  146. 6:11intentions explicitly. Okay. All right.
  147. 6:15So that's about inner join.
  148. 6:19So inner join returns only the matching
  149. 6:21rows between both the tables.
  150. 6:22Non-matching rows are eliminated. Okay.
  151. 6:26Now let's say my requirement is in such
  152. 6:28a way that I not only want the matching
  153. 6:30rows, I want the non-matching rows as
  154. 6:32well from the employees. So basically
  155. 6:34I'm saying I want all the employees
  156. 6:36irrespective of whether they are
  157. 6:38assigned to a department or not. I want
  158. 6:40all the employees. So how do we do that?
  159. 6:42So indirectly you are saying okay I want
  160. 6:45the matching rows plus the non-matching
  161. 6:49rows from the employees table which
  162. 6:51means you want everything from the left
  163. 6:53table matching and non-matching. Okay,
  164. 6:56that's when you actually go for left
  165. 6:59join. So if you look at left join
  166. 7:01returns all the matching rows plus
  167. 7:03non-matching rows from the left table.
  168. 7:06So there are eight matching rows. So
  169. 7:08this department id matches all for these
  170. 7:11eight rows the department ids match with
  171. 7:13the department ids but these two rows
  172. 7:16doesn't have a matching department ID.
  173. 7:18Okay. So but in department table we have
  174. 7:21total 10 rows. So all matching and
  175. 7:24non-matching rows will be retrieved when
  176. 7:26you use a left join. So when we write a
  177. 7:29left join query we should get 10 rows.
  178. 7:33So how do we write the left join query?
  179. 7:35You know everything remains exactly
  180. 7:37similar except that instead of inner
  181. 7:40join you will say left join. That's it.
  182. 7:44Look at this. Previously we have caught
  183. 7:45eight records when we executed that
  184. 7:47query. But let's now execute this and
  185. 7:50see how many records we get. So we first
  186. 7:53go to the messages. Look at this. In
  187. 7:55employees table we have got 10 rows. In
  188. 7:57departments four rows. And in the query
  189. 8:00left join query that we have written
  190. 8:02we've got 10 rows again. So if you look
  191. 8:04at the output now we should have got
  192. 8:07both James and Russell's records as
  193. 8:09well. But if you look at the department
  194. 8:11name column it's basically null and it
  195. 8:13makes sense why because they don't have
  196. 8:15a department ID. They are not assigned a
  197. 8:17department yet. So that's why department
  198. 8:19name will be null. Okay. So left join
  199. 8:22basically returns all the matching rows
  200. 8:24between both the tables plus
  201. 8:26non-matching rows from the left table.
  202. 8:29And this picture depicts exactly that
  203. 8:32scenario. And when you use a left join,
  204. 8:35you can say left just left join or you
  205. 8:38can say left outer join. Okay, the query
  206. 8:41results will not be affected. So the
  207. 8:43outer keyword is optional there.
  208. 8:47All right.
  209. 8:49Now let's say I want all the rows from
  210. 8:52the right table including if you if you
  211. 8:54look at the the two tables here you know
  212. 8:57you have a department called other
  213. 8:59department and that department's ID is
  214. 9:02four. Now this no employee in the
  215. 9:05employees table belongs to this other
  216. 9:07department.
  217. 9:09Okay. So
  218. 9:11if there is a requirement you know if
  219. 9:13somebody asks you okay give me all the
  220. 9:15employees.
  221. 9:17Okay. Which means I want all the
  222. 9:19matching records between both the tables
  223. 9:20plus
  224. 9:22you know non-matching records from the
  225. 9:24right table. Okay. Okay. So from the
  226. 9:26department's table I want you know other
  227. 9:29department record as well irrespective
  228. 9:31of whether there is an employee assigned
  229. 9:33to that department or not. So we want
  230. 9:36all the rows from the right table. Okay.
  231. 9:40Plus I mean all the rows including the
  232. 9:42non-matching ones. Okay. So basically
  233. 9:45right join will return you all the
  234. 9:47matching rows between both the tables
  235. 9:49plus non-matching rows from the right
  236. 9:51table. Okay. So let's see what happens
  237. 9:55you know if we convert this left outer
  238. 9:58join to right outer join. Okay. So
  239. 10:02between employees and department table
  240. 10:04there are eight matching rows and in the
  241. 10:07tbl department table there is this other
  242. 10:10department which you know no other
  243. 10:12employee I mean no employee belongs to
  244. 10:14this other department. Okay. So
  245. 10:17obviously this other department true
  246. 10:19does not have a matching record in
  247. 10:22employees table. Okay. And we want this
  248. 10:25record as well. Okay. So how do we do
  249. 10:28that? Using right outer join. So when we
  250. 10:31execute this we should get around nine
  251. 10:33records.
  252. 10:35So nine records. So look at this. All
  253. 10:37the matching rows until Sarah we have
  254. 10:39got and other department row. Okay. And
  255. 10:44if you look at the name, gender and
  256. 10:45salary columns are null. Why? Because no
  257. 10:48employee is assigned to this department.
  258. 10:50So obviously you will not have any name,
  259. 10:52gender or salary. Okay. So obviously
  260. 10:55whenever you want to return all the
  261. 10:57matching rows between both the tables
  262. 10:59that are involved in a join plus
  263. 11:01non-matching rows from the right table,
  264. 11:03that's when we go for right outer join.
  265. 11:06Okay. So you might have guessed by now
  266. 11:09if I want all the matching rows between
  267. 11:11both the tables plus non-matching rows
  268. 11:14from the left table and non-matching
  269. 11:15rows from the right table, then what
  270. 11:17type of join do I use? Full join. So
  271. 11:20full outer join gives me exactly that
  272. 11:22result. These are the matching rows from
  273. 11:25both the tables. These two are
  274. 11:27non-matching rows from the left table.
  275. 11:29And the last row here other department
  276. 11:31is the non-matching row in the right
  277. 11:33table. Full outer join will give you all
  278. 11:35the records from both the left and right
  279. 11:37tables including the non-matching rows.
  280. 11:41Okay. So obviously when we execute this
  281. 11:43query we should get around 11 records.
  282. 11:46eight matching records plus two
  283. 11:48non-matching from left and one
  284. 11:50non-matching from the right table. So
  285. 11:52how do we write a full outer join query?
  286. 11:54Just specify full outer join. So when we
  287. 11:57execute this query, you should see until
  288. 12:00here all these are matching records.
  289. 12:03These two are non-matching records in
  290. 12:05the employee table and this is a
  291. 12:07non-matching record in the department's
  292. 12:08table. So together we have 11 records.
  293. 12:13Again you can specify full outer join or
  294. 12:15just say full join.
  295. 12:20Okay. So so far we have seen inner join,
  296. 12:23left join, right join and outer join. Uh
  297. 12:26full outer join. Okay. But apart from
  298. 12:28all these types of joints, we also have
  299. 12:30another join called cross join. So what
  300. 12:33does a cross join do and how do I write
  301. 12:35that query? First let's write the query,
  302. 12:38you know, and we'll look at the output
  303. 12:39and then we'll decide what cross join is
  304. 12:41going to do. So if it's a cross join
  305. 12:44instead of saying full outer join all I
  306. 12:46say is cross join. Now remember a cross
  307. 12:50join will not have an on clause. Okay
  308. 12:54I'll I'll I'll tell you in a bit why a
  309. 12:56cross join shouldn't have an on clause.
  310. 12:59So when I delete that cross join and
  311. 13:01then when I execute this query
  312. 13:05look at this look at the number of rows
  313. 13:07that we are getting 40 rows. And if you
  314. 13:10remember how many rows are there in
  315. 13:12these tables 10 rows in the employees
  316. 13:15table and four rows in the department's
  317. 13:17table and when I when we execute the
  318. 13:19cross join query we are getting 40 rows.
  319. 13:23So basically what's happening the number
  320. 13:26of rows in the employees table is being
  321. 13:28multiplied by the number of rows in the
  322. 13:30department's table. So a cross join will
  323. 13:33give us the cartesian product of the
  324. 13:35tables that are involved in the cross
  325. 13:37join.
  326. 13:39Okay, so that's what cross join is all
  327. 13:41about. Cross join produces the cartition
  328. 13:43product of the two tables involved in
  329. 13:45the join. For example, in the employees
  330. 13:47table, if we have 10 records and the
  331. 13:49department's table, if we have four
  332. 13:51records, a cross join would produce 40
  333. 13:55rows between these two tables. And as I
  334. 13:57told you, cross join shouldn't have an
  335. 13:59on clause. Okay. So what basically a
  336. 14:02cross join does is it will take each
  337. 14:04record from the right table and then
  338. 14:07associate that with every record in the
  339. 14:09left table. So in the department's table
  340. 14:11we have got four rows you know it
  341. 14:14payroll
  342. 14:16HR and other department. So it will take
  343. 14:18it row from the department's table
  344. 14:20associate that to every one of the
  345. 14:23employee in the employees table and
  346. 14:25similarly it will take the next record
  347. 14:26which is payroll and associate that. So
  348. 14:29it basically gives us you know the
  349. 14:31number of rows in the employees table
  350. 14:34multiplied by the number of rows in the
  351. 14:36department's table. So it gives us a
  352. 14:38cartian product
  353. 14:42and since this gives us a cartian
  354. 14:44product it doesn't really make sense to
  355. 14:46have uh you know a non clause for cross
  356. 14:49join.
  357. 14:51Okay.
  358. 14:54And look at this. So far we have seen
  359. 14:56you know writing query for joins. You
  360. 14:59know we have seen how to write inner
  361. 15:00join, left outer, right outer, full
  362. 15:02outer and cross join. Now you know
  363. 15:06depending on what we have done until now
  364. 15:08it's pretty simple to to develop a
  365. 15:10generic formula for joins. So what is
  366. 15:13that? Select what columns you want or
  367. 15:17just say column list.
  368. 15:20Okay, select your column list. For
  369. 15:22example, I want name, gender, salary,
  370. 15:24department name, etc. Select column list
  371. 15:27from, you know, maybe your left table.
  372. 15:32And then you specify the join type,
  373. 15:36inner join, cross join, whatever.
  374. 15:40And once you specify the join type, you
  375. 15:42know, with which table you're joining.
  376. 15:45So, write table
  377. 15:47and on clause for specifying join
  378. 15:50condition.
  379. 15:54Okay. So if you compare this with what
  380. 15:57we have
  381. 16:00okay look at this uh if it's an inner
  382. 16:02join you know it would have been
  383. 16:04something like this. So inner join and
  384. 16:07on we will have
  385. 16:11tbl department
  386. 16:13dot id is equal to tbl employee dot
  387. 16:18department ID. So if you compare this
  388. 16:20generic formula with this inner join
  389. 16:22query you know it exactly fits in select
  390. 16:25whatever columns we want from clause the
  391. 16:29left table and then the join type here
  392. 16:31it's an inner join so we are specifying
  393. 16:33inner join otherwise if it's a cross you
  394. 16:36know left join we say left join right
  395. 16:38right join etc and then finally on
  396. 16:40clause and your join condition. So this
  397. 16:43is the generic formula you know if you
  398. 16:46have a hard time you know remembering
  399. 16:48the syntax for joins.
  400. 16:55All right so in summary cross join
  401. 16:58returns the cartian products of the
  402. 16:59tables involved in the join whereas
  403. 17:01inner join returns only the matching
  404. 17:03rows non-matching rows between the
  405. 17:04tables are eliminated. You know look at
  406. 17:06these pictures which will make the
  407. 17:08understanding of joins pretty much easy.
  408. 17:11Okay. So if you look at the inner join
  409. 17:13only matching rows left join matching
  410. 17:16rows plus non-matching rows from the
  411. 17:18left table and right join matching rows
  412. 17:21plus non-matching rows from the right
  413. 17:23table. Whereas full join matching rows
  414. 17:25between both the tables non-matching
  415. 17:27from the left and non-matching from the
  416. 17:30right. Okay.
  417. 17:34On this slide you can find resources for
  418. 17:36ASP.NET and C# interview questions.
  419. 17:38That's it for today. Thank you for
  420. 17:40listening. Have a great day.

About this transcript

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