Self Join in SQL

Поділитися
Вставка
  • Опубліковано 19 січ 2025

КОМЕНТАРІ • 122

  • @DatabaseStar
    @DatabaseStar  Місяць тому +1

    Want to easily remember the SQL commands for your database? Get my free SQL Cheat Sheets here: databasestar.mykajabi.com/lpkj-sql-cheat-sheets/?

  • @yawenxue2963
    @yawenxue2963 3 роки тому +28

    I like how you talk :quiet and clear

  • @preciouschinenyenwa8490
    @preciouschinenyenwa8490 10 місяців тому +1

    Thank you, I was struggling to understand self join, but after watching your video, I realised how simple it is.

  • @joel-rg8xm
    @joel-rg8xm Рік тому +3

    Man! That CATEGORIES example saved me just in time before creating many tables. Thanks

  • @mdimranhussain171
    @mdimranhussain171 2 роки тому +2

    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

  • @Moon22m
    @Moon22m 7 місяців тому +2

    Thank you so much! I was trying to understand the ON statement in SELF JOINS and you've explained it perfectly.

  • @elioko3639
    @elioko3639 3 роки тому +17

    Definitely needed this and you explained it perfectly

  • @kim-ex5cn
    @kim-ex5cn Рік тому +1

    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  Рік тому +1

      Thanks for the kind words! I’m glad this video helped you understand it.

  • @kpamiose
    @kpamiose 2 роки тому +4

    Thanks for breaking this down so simply.

  • @Kartik-vj5os
    @Kartik-vj5os 2 роки тому +1

    Even after a year you are helping guys like me! Thanks for clearing my doubt

  • @Kornackifs
    @Kornackifs Рік тому +1

    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  Рік тому

      Oh is that a good thing or a bad thing?

    • @Kornackifs
      @Kornackifs Рік тому

      @@DatabaseStar
      I don't maybe I should've solved it on my own 😥

  • @astorgadexterpelaez3502
    @astorgadexterpelaez3502 Рік тому

    thank you! been lurking in your channel for a while now, great content sir!

  • @metaman8662
    @metaman8662 Рік тому +1

    I'm thankful I found this video your explanation is what I needed!

  • @dhruvitmodi3043
    @dhruvitmodi3043 11 місяців тому

    Nicely explain in simple terms...checked many of your videos too... I am being SQL developer, liked your work 👍

    • @DatabaseStar
      @DatabaseStar  11 місяців тому

      Thanks! Glad you like the videos.

  • @TK-ko3fg
    @TK-ko3fg Рік тому

    King of SQL explanation

  • @thanzeeljalaldeen
    @thanzeeljalaldeen 3 роки тому +1

    somethething that i needed currently for my project. thanks a lot sir

  • @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.

  • @quynhanhnguyenthi1686
    @quynhanhnguyenthi1686 2 роки тому +2

    Thank you so much for this clear explanation

  • @ExcelTutorials1
    @ExcelTutorials1 2 роки тому +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!!!

  • @slimandahra4294
    @slimandahra4294 2 роки тому +1

    your explaining is great thank u..and good luck

  • @gauravsrivastava17
    @gauravsrivastava17 7 місяців тому

    9:05 writing query and making er diagram is so much helpful

  • @Maria-n5g1l
    @Maria-n5g1l 2 роки тому

    Huge thanks for such a clear explanation! Helped a lot!

  • @chesterliu1929
    @chesterliu1929 2 роки тому +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 роки тому +2

      Ah yes you're right! The arrow should be the other way around. Thanks for letting me know.

    • @offlinedie
      @offlinedie 2 роки тому +1

      @@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?

  • @aiasaiascon3894
    @aiasaiascon3894 Місяць тому +1

    I have a question again - Using the Aliases is really flexible and clever - when you deal with a big database - do you use any method to keep a list of the aliases that you have created ? In case they are quite many ?

    • @DatabaseStar
      @DatabaseStar  Місяць тому +1

      Good question. I don't have a method to keep a list, but the aliases only exist for a specific query so we can see that they refer to right next to where they are defined.

  • @shriefabdelaziz4280
    @shriefabdelaziz4280 7 місяців тому +1

    wow amazing explanation thanks!!

  • @RoysIdea
    @RoysIdea Рік тому +1

    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  Рік тому

      Good question. It depends on the database vendor, and I've written more about it here: www.databasestar.com/hierarchical-data-sql/

  • @BronProject
    @BronProject 2 роки тому +1

    Thank you for this!

  • @aakash1763
    @aakash1763 2 роки тому

    Great explanation just one doubt in the ER diagram for both examples of self join why did you show 1:many relationship?

    • @DatabaseStar
      @DatabaseStar  2 роки тому

      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!

  • @Eyenn_n
    @Eyenn_n 2 роки тому

    Great content! Awesome channel!

  • @Mahde
    @Mahde 4 місяці тому +1

    Brilliant explanation

  • @ZenonMcRae
    @ZenonMcRae 3 роки тому +1

    Nice & clear

  • @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.

  • @matze489
    @matze489 7 місяців тому +1

    really good video

  • @hyperborean72
    @hyperborean72 8 місяців тому

    lovely example

  • @oscarpalomares8282
    @oscarpalomares8282 2 роки тому

    Great explanation!

  • @kumruorkun3947
    @kumruorkun3947 Рік тому

    Very clean, thank you

  • @balaji2035
    @balaji2035 Рік тому

    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  Рік тому

      Yes, if you match on e.manager_id = m.id it will show employers and their managers, which is correct.

  • @SonAyoD
    @SonAyoD 7 місяців тому +1

    Great video

  • @bouslamafathi3581
    @bouslamafathi3581 2 роки тому

    Perfect explanation thanks a lot

  • @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.

  • @AmanRaj-zo7bx
    @AmanRaj-zo7bx Рік тому

    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  Рік тому +1

      Good point, the employee can only have one manager but the manager can have many employees.

    • @AmanRaj-zo7bx
      @AmanRaj-zo7bx Рік тому

      @@DatabaseStar Thanks for clarification.

  • @jaymo2024
    @jaymo2024 2 роки тому

    04:45 the ON clause is what you need to know the rest is bloatware

  • @osoriomatucurane9511
    @osoriomatucurane9511 6 місяців тому

    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  6 місяців тому

      Thanks! That is a good point.

  • @humbertojesustoledovazquez5891
    @humbertojesustoledovazquez5891 3 роки тому

    What a video!!! Thanks :)

  • @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 😁😁🔥🔥🔥🔥🔥🔥🙏🏻🙏🏻

  • @lehadypierrengangue4149
    @lehadypierrengangue4149 3 роки тому

    thanks for the content !!

  • @michaelchiz8055
    @michaelchiz8055 2 роки тому

    Good tutorial

  • @newbeginning3089
    @newbeginning3089 2 роки тому

    Thanks. It was 👍

  • @spaaaaace8952
    @spaaaaace8952 Рік тому

    Thank you.

  • @olufemiolawale3848
    @olufemiolawale3848 Рік тому

    Thank you sir

  • @youmightdontknow450
    @youmightdontknow450 5 місяців тому

    Thank you sir

  • @rural_world
    @rural_world 11 місяців тому +1

    Perfect

  • @trytofindareasonwhy
    @trytofindareasonwhy 7 місяців тому

    self joins make my brain melt

    • @DatabaseStar
      @DatabaseStar  7 місяців тому

      Yes they are a hard concept to understand!

  • @souronion3822
    @souronion3822 Рік тому

    It is okay

  • @Dobby_zuul
    @Dobby_zuul 2 місяці тому

    I think your ERD should be the other way around

    • @DatabaseStar
      @DatabaseStar  2 місяці тому

      Which one, the employee and manager? I think it's correct - an employee ID can be related to many other employee manager ID fields, which indicates that an employee can manage multiple other employees. Unless I am missing something?

  • @haarkee9366
    @haarkee9366 5 місяців тому

    I love you

  • @gauravsrivastava17
    @gauravsrivastava17 7 місяців тому

  • @poojamore056
    @poojamore056 Рік тому

    Hey, the employee example was good! I waited till end for another example tho.. 🥲

    • @DatabaseStar
      @DatabaseStar  Рік тому

      Thanks! Good point, I should have included more examples.

  • @arnosolo2008
    @arnosolo2008 11 місяців тому

    Thank you.