No video

Self Join in SQL

  Рет қаралды 43,755

Database Star

Database Star

Күн бұрын

📝 Get my free SQL Cheat Sheets: www.databasest...
🎓 Learn and become confident in SQL: databasestar.m...
A self join in SQL is where you write a query on one table that joins to the same table a second time. It's used when you have a record in the table that refers to another record in the same table.
A common example is employees and managers, or product categories. Anything with a "hierarchical data" setup may use an SQL self join.
This video shows you what an SQL self join is, how to write one, and how to overcome issues with column names and missing data.
You can find out more about joins overall in this guide here: www.databasest...
If you want more database training, practice questions, and guides, enroll in Database Star Academy here: www.databasest...
If you have any suggestions for other videos or comments on this one, let me know in the comments below.
Timestamps:
00:00 Introduction
00:12 Self Join definition
01:38 Employee solution
06:03 Final query
06:45 Categories example
07:30 Left Join

Пікірлер: 115
@yawenxue2963
@yawenxue2963 3 жыл бұрын
I like how you talk :quiet and clear
@DatabaseStar
@DatabaseStar 11 ай бұрын
Thanks!
@preciouschinenyenwa8490
@preciouschinenyenwa8490 5 ай бұрын
Thank you, I was struggling to understand self join, but after watching your video, I realised how simple it is.
@DatabaseStar
@DatabaseStar 5 ай бұрын
Glad it was helpful!
@Moon22m
@Moon22m 2 ай бұрын
Thank you so much! I was trying to understand the ON statement in SELF JOINS and you've explained it perfectly.
@DatabaseStar
@DatabaseStar 2 ай бұрын
Glad it was helpful!
@Kornackifs
@Kornackifs 7 ай бұрын
I struggled with an intermediate leetcode sql problem I didn't want to see the solution right away so i started searchin on sql topics to full the knowledge gap but what i found is actually a video solving the problem itself fuck me
@DatabaseStar
@DatabaseStar 7 ай бұрын
Oh is that a good thing or a bad thing?
@Kornackifs
@Kornackifs 7 ай бұрын
@@DatabaseStar I don't maybe I should've solved it on my own 😥
@elioko3639
@elioko3639 2 жыл бұрын
Definitely needed this and you explained it perfectly
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Thanks!
@joel-rg8xm
@joel-rg8xm Жыл бұрын
Man! That CATEGORIES example saved me just in time before creating many tables. Thanks
@DatabaseStar
@DatabaseStar Жыл бұрын
No problem, glad it was helpful!
@kpamiose
@kpamiose 2 жыл бұрын
Thanks for breaking this down so simply.
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Glad you like it!
@mdimranhussain171
@mdimranhussain171 Жыл бұрын
got here from a course i couldnt understand this self join and Thanks to you it's all clear now. you explained it really well with the perfect examples. Keep up
@DatabaseStar
@DatabaseStar Жыл бұрын
Great to hear!
@kim-ex5cn
@kim-ex5cn Жыл бұрын
THANK YOU. i have read a hundred tutorials and was stuck on the alias. starting with the from and on clauses and explaining the AS is implied and then going back to the select cracked things open for me. much appreciated.
@DatabaseStar
@DatabaseStar Жыл бұрын
Thanks for the kind words! I’m glad this video helped you understand it.
@Kartik-vj5os
@Kartik-vj5os 2 жыл бұрын
Even after a year you are helping guys like me! Thanks for clearing my doubt
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Thanks! Glad you found it useful.
@shriefabdelaziz4280
@shriefabdelaziz4280 2 ай бұрын
wow amazing explanation thanks!!
@DatabaseStar
@DatabaseStar 2 ай бұрын
Glad it was helpful!
@metaman8662
@metaman8662 8 ай бұрын
I'm thankful I found this video your explanation is what I needed!
@DatabaseStar
@DatabaseStar 8 ай бұрын
I’m glad you liked it!
@quynhanhnguyenthi1686
@quynhanhnguyenthi1686 2 жыл бұрын
Thank you so much for this clear explanation
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Glad you liked it!
@thanzeeljalaldeen
@thanzeeljalaldeen 3 жыл бұрын
somethething that i needed currently for my project. thanks a lot sir
@DatabaseStar
@DatabaseStar 3 жыл бұрын
Glad it was helpful!
@astorgadexterpelaez3502
@astorgadexterpelaez3502 11 ай бұрын
thank you! been lurking in your channel for a while now, great content sir!
@DatabaseStar
@DatabaseStar 11 ай бұрын
Awesome, thank you!
@slimandahra4294
@slimandahra4294 2 жыл бұрын
your explaining is great thank u..and good luck
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Thanks!
@TK-ko3fg
@TK-ko3fg Жыл бұрын
King of SQL explanation
@DatabaseStar
@DatabaseStar Жыл бұрын
Thanks!
@hyperborean72
@hyperborean72 3 ай бұрын
lovely example
@DatabaseStar
@DatabaseStar 3 ай бұрын
Thanks!
@gauravsrivastava17
@gauravsrivastava17 2 ай бұрын
9:05 writing query and making er diagram is so much helpful
@DatabaseStar
@DatabaseStar 2 ай бұрын
Glad you found it helpful
@dhruvitmodi3043
@dhruvitmodi3043 6 ай бұрын
Nicely explain in simple terms...checked many of your videos too... I am being SQL developer, liked your work 👍
@DatabaseStar
@DatabaseStar 6 ай бұрын
Thanks! Glad you like the videos.
@BronProject
@BronProject 2 жыл бұрын
Thank you for this!
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Glad it helped!
@ExcelTutorials1
@ExcelTutorials1 2 жыл бұрын
This is super helpful! Your voice is almost like listen to ASMR, lol. Nice to listen to
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Heh thanks- I’ve been working to improve the audio on my recent videos so the older ones may sound softer.
@teenytinytoons
@teenytinytoons 2 жыл бұрын
It's not almost like, IT IS!!!
@OberynPinkViper
@OberynPinkViper Жыл бұрын
Thank you for the clear and simple explanation. I remembered seeing the Employee - Manager example somewhere a few years ago, but I couldn't recall where exactly I saw it.
@DatabaseStar
@DatabaseStar Жыл бұрын
Glad it was helpful!
@matze489
@matze489 2 ай бұрын
really good video
@DatabaseStar
@DatabaseStar 2 ай бұрын
Thanks!
@ZenonMcRae
@ZenonMcRae 2 жыл бұрын
Nice & clear
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Thank you
@SonAyoD
@SonAyoD 2 ай бұрын
Great video
@DatabaseStar
@DatabaseStar 2 ай бұрын
Thanks a lot!
@user-xs3kf9tx1m
@user-xs3kf9tx1m Жыл бұрын
Huge thanks for such a clear explanation! Helped a lot!
@DatabaseStar
@DatabaseStar Жыл бұрын
Glad it helped!
@aakash1763
@aakash1763 Жыл бұрын
Great explanation just one doubt in the ER diagram for both examples of self join why did you show 1:many relationship?
@DatabaseStar
@DatabaseStar Жыл бұрын
Good question! It's because of the way the two concepts or records are related. For example with employees and managers, an employee has one manager, but a manager can have many employees. This is a one-to-many relationship, so I've drawn it that way on the diagram. Hope that helps!
@kumruorkun3947
@kumruorkun3947 Жыл бұрын
Very clean, thank you
@DatabaseStar
@DatabaseStar Жыл бұрын
Thanks!
@Eyenn_n
@Eyenn_n Жыл бұрын
Great content! Awesome channel!
@DatabaseStar
@DatabaseStar Жыл бұрын
Thanks so much!
@michaelchiz8055
@michaelchiz8055 Жыл бұрын
Good tutorial
@DatabaseStar
@DatabaseStar Жыл бұрын
Glad you think so!
@oscarpalomares8282
@oscarpalomares8282 Жыл бұрын
Great explanation!
@DatabaseStar
@DatabaseStar Жыл бұрын
Thanks!
@arnosolo2008
@arnosolo2008 6 ай бұрын
Thank you.
@DatabaseStar
@DatabaseStar 6 ай бұрын
You're welcome!
@RoysIdea
@RoysIdea 9 ай бұрын
So how would you expand these queries if you want to order them from top to bottom, with a level or additional spaces? So start with Clair (who has no manager), followed by a manager and his/her team, etc. And include a level column.
@DatabaseStar
@DatabaseStar 9 ай бұрын
Good question. It depends on the database vendor, and I've written more about it here: www.databasestar.com/hierarchical-data-sql/
@rural_world
@rural_world 5 ай бұрын
Perfect
@DatabaseStar
@DatabaseStar 5 ай бұрын
Thanks!
@bouslamafathi3581
@bouslamafathi3581 Жыл бұрын
Perfect explanation thanks a lot
@DatabaseStar
@DatabaseStar Жыл бұрын
You are welcome!
@olufemiolawale3848
@olufemiolawale3848 Жыл бұрын
Thank you sir
@DatabaseStar
@DatabaseStar Жыл бұрын
You're welcome
@osoriomatucurane9511
@osoriomatucurane9511 Ай бұрын
The concept is easy and straitforwad, but times come you face a question with no clue that the solution comes with self join, unless you are given the schema
@DatabaseStar
@DatabaseStar Ай бұрын
Thanks! That is a good point.
@newbeginning3089
@newbeginning3089 2 жыл бұрын
Thanks. It was 👍
@DatabaseStar
@DatabaseStar 2 жыл бұрын
You're welcome!
@humbertojesustoledovazquez5891
@humbertojesustoledovazquez5891 2 жыл бұрын
What a video!!! Thanks :)
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Glad you liked it!
@chesterliu1929
@chesterliu1929 2 жыл бұрын
At 6:38, relationship between id and manager_id is one to many which means one manager_id can have multiple employee_id to manage. However the multiple arrow sign is pointing to manager_id which should mean many manager_id can manage one employee_id. I think the arrow should be the opposite way, right?
@DatabaseStar
@DatabaseStar 2 жыл бұрын
Ah yes you're right! The arrow should be the other way around. Thanks for letting me know.
@offlinedie
@offlinedie 2 жыл бұрын
@@DatabaseStar Hi! Sry I think you were correct in the video, Since the foreign key is always on the many side in a EDR. Right?
@lehadypierrengangue4149
@lehadypierrengangue4149 3 жыл бұрын
thanks for the content !!
@DatabaseStar
@DatabaseStar 3 жыл бұрын
No worries!
@enricoroselino7557
@enricoroselino7557 Жыл бұрын
and what if there is a period validity of the structure (include year and month column) ? should i put it on the same table ? is it okay to have duplicate rows of ppl name?
@DatabaseStar
@DatabaseStar Жыл бұрын
Good question, I think it's OK to have on the same table.It would then be an additional condition on your join, or in the WHERE clause, to check if a record falls in a certain date range.
@MDARUN-ph1dw
@MDARUN-ph1dw 3 жыл бұрын
No wonder why you are calling yourself a database star 🔥🔥🔥🔥😁
@DatabaseStar
@DatabaseStar 3 жыл бұрын
Thanks! The name is more for my readers and viewers to become "database stars" by learning and improving :)
@MDARUN-ph1dw
@MDARUN-ph1dw 3 жыл бұрын
@@DatabaseStar 😁😁🔥🔥🔥🔥🔥🔥🙏🏻🙏🏻
@luizetulio4888
@luizetulio4888 Жыл бұрын
Great video. Is it possible to use self join and a many to many relationship simultaneously or it would work only for one to many? For example if an employee has 2 or more managers...
@DatabaseStar
@DatabaseStar Жыл бұрын
Good question! Yes, that is possible. Another example of using that could be if you want to capture the parents of a person, where each person could have more than one parent.
@balaji2035
@balaji2035 8 ай бұрын
hi sir I have one doubt if we specify e.manager_id =m.id it will give the result only the both columns having same values right example manager_id =1 and emp_id also 1 or how does it works can you please clarify my doubt.
@DatabaseStar
@DatabaseStar 8 ай бұрын
Yes, if you match on e.manager_id = m.id it will show employers and their managers, which is correct.
@AmanRaj-zo7bx
@AmanRaj-zo7bx 7 ай бұрын
in ERD Diagram, id to manager_id relationship is One to Many, which means an employee can have multiple managers but one manager can not have multiple employees !! But it's not the case here, please clarify it.
@DatabaseStar
@DatabaseStar 7 ай бұрын
Good point, the employee can only have one manager but the manager can have many employees.
@AmanRaj-zo7bx
@AmanRaj-zo7bx 7 ай бұрын
@@DatabaseStar Thanks for clarification.
@trytofindareasonwhy
@trytofindareasonwhy 2 ай бұрын
self joins make my brain melt
@DatabaseStar
@DatabaseStar 2 ай бұрын
Yes they are a hard concept to understand!
@jaymo2024
@jaymo2024 Жыл бұрын
04:45 the ON clause is what you need to know the rest is bloatware
@DatabaseStar
@DatabaseStar Жыл бұрын
Thanks for the comment!
@haarkee9366
@haarkee9366 5 күн бұрын
I love you
@DatabaseStar
@DatabaseStar 4 күн бұрын
Haha thanks!
@souronion3822
@souronion3822 7 ай бұрын
It is okay
@DatabaseStar
@DatabaseStar 7 ай бұрын
Thanks!
@gauravsrivastava17
@gauravsrivastava17 2 ай бұрын
@DatabaseStar
@DatabaseStar 2 ай бұрын
Thanks
@poojamore056
@poojamore056 7 ай бұрын
Hey, the employee example was good! I waited till end for another example tho.. 🥲
@DatabaseStar
@DatabaseStar 7 ай бұрын
Thanks! Good point, I should have included more examples.
@spaaaaace8952
@spaaaaace8952 Жыл бұрын
Thank you.
@DatabaseStar
@DatabaseStar Жыл бұрын
You're welcome!
@youmightdontknow450
@youmightdontknow450 13 күн бұрын
Thank you sir
@DatabaseStar
@DatabaseStar 13 күн бұрын
Glad it helped!
Database Design for Custom Fields
18:39
Database Star
Рет қаралды 26 М.
7 Database Design Mistakes to Avoid (With Solutions)
11:29
Database Star
Рет қаралды 71 М.
CHOCKY MILK.. 🤣 #shorts
00:20
Savage Vlogs
Рет қаралды 28 МЛН
Пройди игру и получи 5 чупа-чупсов (2024)
00:49
Екатерина Ковалева
Рет қаралды 2,3 МЛН
Get 10 Mega Boxes OR 60 Starr Drops!!
01:39
Brawl Stars
Рет қаралды 14 МЛН
6 SQL Joins you MUST know! (Animated + Practice)
9:47
Anton Putra
Рет қаралды 139 М.
SQL Self Join | Quick Tips Ep58
10:57
Joey Blue
Рет қаралды 10 М.
When to Use a Subquery in SQL
8:50
Database Star
Рет қаралды 32 М.
SQL CTEs (Common Table Expressions) - Why and How to Use Them
7:56
Database Star
Рет қаралды 29 М.
SQL Joins: Difference Between Inner/Left/Right/Outer Joins
10:52
How to Design a Database
10:57
Database Star
Рет қаралды 53 М.
SQL Joins - Beginner to PRO Masterclass with 10 Examples
27:08