SQL Self Join Concept | Most Asked Interview Question | Employee Salary More than Manager's Salary

  Рет қаралды 114,699

Ankit Bansal

Ankit Bansal

Күн бұрын

Пікірлер: 188
@amrutdeshpande05
@amrutdeshpande05 2 жыл бұрын
Using Excel and pasting the manager info next to employee info was a great way of explaining! Thank you. You make the life of a Data person so easy!
@ankitbansal6
@ankitbansal6 2 жыл бұрын
😊
@dhananjaymali6242
@dhananjaymali6242 Жыл бұрын
Correct thanks Ankit 🙌🙌... explaining same without excel was like tutor teaching to only class toppers, just imagine one who can imagine without excel can definitely solve this interview question 😅 it was just putting into syntax task for that kind of guy.
@spd3155
@spd3155 2 жыл бұрын
create table emp_manager(emp_id int,emp_name varchar(50),salary int(20),manager_id int(10)); insert into emp_manager values( 1 ,'Ankit', 10000 ,4 ); insert into emp_manager values( 2 ,'Mohit', 15000 ,5 ); insert into emp_manager values( 3 ,'Vikas', 10000 ,4 ); insert into emp_manager values( 4 ,'Rohit', 5000 ,2 ); insert into emp_manager values( 5 ,'Mudit', 12000 ,6 ); insert into emp_manager values( 6 ,'Agam', 12000 ,2 ); insert into emp_manager values( 7 ,'Sanjay', 9000 ,2 ); insert into emp_manager values( 8 ,'Ashish', 5000 ,2 );
@satyanathparvatham4406
@satyanathparvatham4406 2 жыл бұрын
Saviour
@devashishduklan8154
@devashishduklan8154 2 жыл бұрын
thnx
@alokgupta35
@alokgupta35 2 жыл бұрын
int(20) ? will it work ? i think size is not necessary
@rishabhkesarwani5761
@rishabhkesarwani5761 2 жыл бұрын
@@alokgupta35 yes
@NaveenKumar-fq4sb
@NaveenKumar-fq4sb Жыл бұрын
thank u
@kirankapruwan8892
@kirankapruwan8892 Жыл бұрын
I had been searching for self join videos. But I did not understand it's working then I came across ur video and you explained in such a easy way that any layman can understand. Thank u so much. Keep doing such great work.
@ankitbansal6
@ankitbansal6 Жыл бұрын
It's my pleasure😊
@vinipowerbi
@vinipowerbi Жыл бұрын
It became very easy to understand the concept the way you show joining manager id with the second table emp id..... Thank you so much for the clear understanding
@ankitbansal6
@ankitbansal6 Жыл бұрын
You are most welcome
@sanjanashuklaa2031
@sanjanashuklaa2031 6 ай бұрын
video was crystal clear😍😍 i don't know how effortlessly i understood this concept because it was you who was explaining thankyou plz make more video on other concepts
@ankitbansal6
@ankitbansal6 6 ай бұрын
Glad to know 🙂
@ajinkyaadhotre5336
@ajinkyaadhotre5336 2 жыл бұрын
DAMN ! the two table creation on excel was soo good way of explaining ! you earned a subscriber ! 🤩
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Thank you 😊
@Nikhilg-rs7iv
@Nikhilg-rs7iv 10 ай бұрын
Untill before watching this video I am just joining the tables withing itself and getting the empty table as result lol. Thank you Ankit bro for explaining clearly and simply.
@badalsamall
@badalsamall 3 күн бұрын
I agree the excel explanation and then heading to SQL and such detailed and simple explanation that I will never forget it I solved the problem I was stuck in by my own right after watching this video. Thank you so much for making concepts so easily understandable.
@ankitbansal6
@ankitbansal6 2 күн бұрын
Keep watching 👀
@Venom-yk3wu
@Venom-yk3wu 2 жыл бұрын
the most lucid explanation on whole utube without any complication.. amazing bro !!
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Glad you liked it!
@sanam6323
@sanam6323 7 ай бұрын
Great. Ager long years I am in the correct KZbin channel for SQL. Thanks Ankit.
@ankitbansal6
@ankitbansal6 7 ай бұрын
Keep going 💪
@sridharmurari3007
@sridharmurari3007 Жыл бұрын
Great Way to explain with Excel not many would explain in Detail like this !
@ankitbansal6
@ankitbansal6 Жыл бұрын
Glad you liked it!
@Aquatic_zone_betta
@Aquatic_zone_betta 3 ай бұрын
Hamesha ki tarah fir ek doubt solve ho gaya 😂😊 Thank You Sir 🙏💛🙏
@ayoushisingh5631
@ayoushisingh5631 6 ай бұрын
Great way of explaination by putting the same table next to the original table. Thanks!!
@spokenenglishworld1650
@spokenenglishworld1650 5 ай бұрын
Thank you so much ankit 100 times...I was not able to understand self join but your video made it possible and i have been searching this kinda content since a long time..thank you so much once again...i dont usually comment but it is worth to do it today☺
@ankitbansal6
@ankitbansal6 5 ай бұрын
Thank you 😊
@khaopiyovlogs
@khaopiyovlogs 2 ай бұрын
Thanks Ankit really appreciated this video help me so much to understand self join questions the excel table is game changer to understand this concept.
@dudechany
@dudechany 2 жыл бұрын
Well Ankit's salary is greater than his manager's salary at Amazon 😀. Nice tutorial btw. Great work.
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Haha 😃
@dfkgjdflkg
@dfkgjdflkg 11 ай бұрын
as usual, great content. You have been a guru to us. Thanks for your time. It is a selfless act. Thanks again
@ankitbansal6
@ankitbansal6 11 ай бұрын
My pleasure!
@HEALSTATION-k6h
@HEALSTATION-k6h 6 ай бұрын
Amazing Amazing seriously Amazing .... 👏 👏 👏 Thank you so mych
@naveennvnkumar4615
@naveennvnkumar4615 2 жыл бұрын
You make SQL look so easy, really good. Thanks mate
@ankitbansal6
@ankitbansal6 2 жыл бұрын
🙏
@shuaibsaqib5085
@shuaibsaqib5085 2 жыл бұрын
Hi Ankit, Kindly post the Table schema queries in the description for reference and solution query too..
@sairajesh6895
@sairajesh6895 2 жыл бұрын
IT'S ALWAYS THE UNDERRATED VID THAT'S LEGIT! THANK YOU!
@vinitpatidar5617
@vinitpatidar5617 6 ай бұрын
Most simple explanation. Thanks
@shubhamsri4561
@shubhamsri4561 Жыл бұрын
Your video is very helpful, sir. The way you explain complex concepts is very understandable
@ankitbansal6
@ankitbansal6 Жыл бұрын
Great 😊
@rahulranjan6740
@rahulranjan6740 Жыл бұрын
very crystal clear and detailed explanation
@ankitbansal6
@ankitbansal6 Жыл бұрын
Glad you liked it😊
@narendra742
@narendra742 2 ай бұрын
Helped me in interview, able to answer very confidently. thanks.
@ankitbansal6
@ankitbansal6 2 ай бұрын
Excellent!
@RohithS-ig4hl
@RohithS-ig4hl Жыл бұрын
Thanks for this Ankit. It was really helpful for me in understanding the concept of self joins. Really appreciated!
@ankitbansal6
@ankitbansal6 Жыл бұрын
Glad to hear that
@aniketraut6864
@aniketraut6864 2 жыл бұрын
Thank you for these amazing videos its helping me to learn the sql in simplest way.
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Great to hear!
@hemantsah8567
@hemantsah8567 5 ай бұрын
That's how manager have grudges against some employees, they use Ankit Bansal's query. :)
@prithak3263
@prithak3263 11 ай бұрын
great way of explaining. kudos!
@milindzuge906
@milindzuge906 2 жыл бұрын
Thanks Ankit, Great Content❣
@pspreetesh4166
@pspreetesh4166 7 ай бұрын
sir why did you give inner join instead of self join? inner join only gives common records b/w 2 tables and self join used for 1 table by itself. Please answer
@srikanthkalluri318
@srikanthkalluri318 26 күн бұрын
Inner, left, right,.. joins used not only for b/w 2 tables but also used while joining same table or itself
@anchitgupta4772
@anchitgupta4772 Жыл бұрын
I have solved this question same way in leetcode
@adityakaushik6417
@adityakaushik6417 2 жыл бұрын
Thanks for this example ! what would be the solution to find out those employees who have highest salary under each manager.
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Rank partition by manager and filter
@bhaveshbharti4782
@bhaveshbharti4782 Жыл бұрын
Thanks for so clear explanation. Understood
@ankitbansal6
@ankitbansal6 Жыл бұрын
Great to hear!
@aashishmalhotra
@aashishmalhotra 2 жыл бұрын
Awesome Ankit got your channel from Linkeidin and its best thing happened.
@debasmitabanerjee2835
@debasmitabanerjee2835 6 ай бұрын
Best explanation!!!!
@KumarNishant181286
@KumarNishant181286 2 ай бұрын
Garda
@hiteshjanuskar7319
@hiteshjanuskar7319 2 жыл бұрын
Very helpful concept 👍🏻
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Glad it was helpful!
@Travelling_With_Kamalu
@Travelling_With_Kamalu Жыл бұрын
Thank you sir. For the explanation.very clear
@ankitbansal6
@ankitbansal6 Жыл бұрын
You are welcome
@sumanacharya461
@sumanacharya461 2 жыл бұрын
If we just use join also it works select e.*,m.salary as sal from emp_manager as e join emp_manager as m on e.manager_id = m.emp_id where e.salary > m.salary this my query and it worked
@explorer_baba2750
@explorer_baba2750 2 жыл бұрын
please explain the reason for the join connection based on a.manager_id = b.id? why we cant use a.id = b.manager_id?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
You can do that also. Accordingly you will have to use alias for manager name and emp name and salary.
@explorer_baba2750
@explorer_baba2750 2 жыл бұрын
@@ankitbansal6 thanks
@vishnujatav6329
@vishnujatav6329 2 жыл бұрын
Very nice explanation
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Thank you
@nishantbodke
@nishantbodke 2 жыл бұрын
thank you so much dude you're a god
@DineshDinesh-vw3uj
@DineshDinesh-vw3uj 4 ай бұрын
Best
@techgalaxy100
@techgalaxy100 Жыл бұрын
Very well explained brother.
@ankitbansal6
@ankitbansal6 Жыл бұрын
Glad it was helpful!
@LS8636
@LS8636 2 жыл бұрын
Thanks for making it so easy
@enjoyfriends873
@enjoyfriends873 10 ай бұрын
Hi Ankit Thank you 🙂 If possible please share the create table and insert statements for every video Oracle create table emp_manager( emp_id number, emp_name varchar2(50), salary number, manager_id number ); insert into emp_manager values( 1 ,'Ankit', 10000 ,4 ); insert into emp_manager values( 2 ,'Mohit', 15000 ,5 ); insert into emp_manager values( 3 ,'Vikas', 10000 ,4 ); insert into emp_manager values( 4 ,'Rohit', 5000 ,2 ); insert into emp_manager values( 5 ,'Mudit', 12000 ,6 ); insert into emp_manager values( 6 ,'Agam', 12000 ,2 ); insert into emp_manager values( 7 ,'Sanjay', 9000 ,2 ); insert into emp_manager values( 8 ,'Ashish', 5000 ,2 );
@gauravthukral7042
@gauravthukral7042 3 жыл бұрын
Great explanation 👍
@ankitbansal6
@ankitbansal6 3 жыл бұрын
Thank you 😊
@AnjaliKumari-zm9zo
@AnjaliKumari-zm9zo Күн бұрын
I got the output with self join too: select e1.emp_id as emp_id,e1.name as Employee,e2.name as manager_name,e1.salary,e2.salary as manager_salary from Emp e1, Emp e2 where e1.managerId = e2.id and e1.salary > e2.salary
@mahesh6701
@mahesh6701 2 жыл бұрын
SELF JOIN concept explained in the best way. thanks a lot. Could you please explain INDEX and CLUSTER INDEX and NON-CLUSTER INDEX concepts please?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Noted
@lazygeek431
@lazygeek431 Жыл бұрын
great, thanks for the explanation
@ankitbansal6
@ankitbansal6 Жыл бұрын
You are welcome!
@mohitmalviya1928
@mohitmalviya1928 Жыл бұрын
grt work sir
@ankitbansal6
@ankitbansal6 Жыл бұрын
Thanks a lot
@mohiuddinansari9356
@mohiuddinansari9356 11 ай бұрын
Well explained, thank you so much. Subscribed !
@ankitbansal6
@ankitbansal6 11 ай бұрын
Awesome, thank you!
@vutv5742
@vutv5742 Жыл бұрын
Completed ❤
@ashish31416
@ashish31416 Жыл бұрын
Thank you so much
@sayansengupta8207
@sayansengupta8207 Жыл бұрын
Superb!!!!
@pavangsk8404
@pavangsk8404 Жыл бұрын
lovely explaining
@ankitbansal6
@ankitbansal6 Жыл бұрын
Thank you! 🙂
@sauravips
@sauravips 2 жыл бұрын
Beautifully explained
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Thank you so much 🙂
@demon-h5p
@demon-h5p Жыл бұрын
please upload data set as well or provide drive link whenever u upload practical
@surajkurle6962
@surajkurle6962 6 ай бұрын
thank u very easy
@NEHAKHANZODE-p8p
@NEHAKHANZODE-p8p 5 ай бұрын
Thank you bro:)
@meerasrinivasagopalan9662
@meerasrinivasagopalan9662 2 жыл бұрын
Clear explanation! thank u!
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Glad you liked it 😊
@rajivjain6165
@rajivjain6165 2 жыл бұрын
Hi Ankit Could you make video on performance tunning topic.
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Sure
@ChinthanaN-r4t
@ChinthanaN-r4t 10 ай бұрын
Can we combine different columns using inner join
@Vedantsinghvi1611
@Vedantsinghvi1611 Жыл бұрын
Hello Why is manager id taken from the employee table And not manager table? Thanks
@niveditasingh1176
@niveditasingh1176 2 жыл бұрын
Hey Ankit, if we write query as " on e.emp_id = m.mgr_id" and rest query remains same will it yield same result?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Nope
@Ramesh00300
@Ramesh00300 2 жыл бұрын
@@ankitbansal6 can you please explain why the queries are giving different results when we are changing the on condition?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
@@Ramesh00300 you need to watch the video again to understand
@mohitmotwani9256
@mohitmotwani9256 Жыл бұрын
When specifying a condition in the WHERE clause, it applies after the join has been performed. This means that the join conditions defined in the ON clause have already been applied, and the WHERE clause further filters the resulting data. In contrast, the ON clause specifies the conditions for joining tables. It dictates how the tables are connected, such as matching columns or other relationships. This condition is used during the join itself, influencing which rows from both tables are combined.
@pavan5208
@pavan5208 2 жыл бұрын
Hi Ankit, thanks for the clear explanation. Small question on the JOIN part. Any particular reason for using INNER JOIN there? LEFT JOIN would also work and produce similar result there right?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Yes but there is no need of left join.When inner join works then why to use left join.
@RSJ1947
@RSJ1947 2 жыл бұрын
INNER JOIN is optimized than OUTER JOIN
@uttamx2016
@uttamx2016 Жыл бұрын
@@ankitbansal6 I realized one thing how Emp_id = 1 is getting in output even though 1 is not present in manager_Id ? I am trying to match output but not matching .
@satyamtyagi3076
@satyamtyagi3076 2 жыл бұрын
Hi Ankit, Correct me if I'm wrong, Don't you think that 'emp_id: 1, emp_name: Ankit' should not come in the result as you have used inner join on the emp_id = manager_id.
@ankitbansal6
@ankitbansal6 2 жыл бұрын
But Ankit's manager id is present in table in emp I'd column.
@muazmalik601
@muazmalik601 Жыл бұрын
one more solution: with cte as(select A.* , B.Salary as manger_salary from emp_manager as A, emp_manager B where A.manager_id=B.emp_id) select emp_id, emp_name from cte where salary> manger_salary
@arpitmishra1783
@arpitmishra1783 Жыл бұрын
nicely explained!
@ankitbansal6
@ankitbansal6 Жыл бұрын
Glad you liked it
@raushansingh7530
@raushansingh7530 6 ай бұрын
select emp.emp_name as Employee_name from emp_manager as emp inner join emp_manager as manager on emp.manager_id = manager.emp_id where emp.salary > manager.salary Thanks Ankit sr
@rajeshgoud9619
@rajeshgoud9619 Жыл бұрын
hello ankit what if one of the manager id is null and how it will join in that case to get emp salary>mgr salary
@ankitbansal6
@ankitbansal6 Жыл бұрын
If an employee does not have a manager then you cannot compare.
@ganeshtaware9883
@ganeshtaware9883 2 жыл бұрын
Thanks
@divyagouda5285
@divyagouda5285 Жыл бұрын
Hi.. i have employee table in that we have ename and sal columns. From employee table we need employee name who is getting max sal without using analytical,order by and aggregate functions. How can we do any one help on this
@PriyankaPatil-nj2qb
@PriyankaPatil-nj2qb 2 жыл бұрын
Thank you sir..
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Most welcome
@arbazadam3407
@arbazadam3407 2 жыл бұрын
Hi, i have a slightly different question to ask. How can i put a check constraint on the manager_id column such that it shouldn't have any other values except for the ones present in emp_id in postgresql
@ankitbansal6
@ankitbansal6 2 жыл бұрын
We can put a forein key constraint
@arbazadam3407
@arbazadam3407 2 жыл бұрын
@@ankitbansal6 thanks dude. I wasn't aware that we can add a foreign key constraint to the same table.
@ankitbansal6
@ankitbansal6 2 жыл бұрын
@@arbazadam3407 we can 😊
@AnilKumar-oe1bb
@AnilKumar-oe1bb Жыл бұрын
Can you please give more real time scenarios where Self Join is used?
@sanilkumarbarik9151
@sanilkumarbarik9151 Жыл бұрын
Since it's 4 days and nobody replied to your comment, I would recommend you to ask the same question to ChatGPT.
@sass520
@sass520 9 ай бұрын
Hello Sir, You mentioned self join but in the answer you used Inner Join
@umeshroy6898
@umeshroy6898 Жыл бұрын
when I run the query say table does not exit
@neelshah8803
@neelshah8803 2 жыл бұрын
Hi Ankit, Can you please make videos on Python and ML please would be really helpful. Thanks
@moyeenshaikh9915
@moyeenshaikh9915 2 жыл бұрын
Sir, if we inner join emp id of emp with manager id of manager table, will it work same?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
No.
@moyeenshaikh9915
@moyeenshaikh9915 2 жыл бұрын
@@ankitbansal6 how to imagine sir which column to take on left table and which one on right
@ankitbansal6
@ankitbansal6 2 жыл бұрын
@@moyeenshaikh9915 watch video again
@amritasinha2784
@amritasinha2784 2 жыл бұрын
If any employee is not having any manager , I mean NULL. Then how to write that? I want NULL in the columns manager_name and manager_salary.
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Just do left join
@vishalsonawane.8905
@vishalsonawane.8905 9 ай бұрын
Done
@hellotohappiness1028
@hellotohappiness1028 Жыл бұрын
please increase ur voice ..just now i subscribed
@ankitbansal6
@ankitbansal6 Жыл бұрын
Sure
@amritasinha2784
@amritasinha2784 2 жыл бұрын
Tables: Employee: Employee_Id name salary 1 Ram 4500 2 Gopi 17500 3 shyam 9500 4 Nisha 13500 Salary_range: From_sal To_sal Grade 0 5000 A 5001 10000 B 10001 15000 C 15001 20000 D Output: Name Grade Ram A Gopi D shyam B Nisha C I need a query to get the output result.
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Just join with condition salary between from sal and to salary
@theraizadatalks14
@theraizadatalks14 2 жыл бұрын
Below Query will give the desired result: Select E.emp_name as 'Name',S.Grade from EmployeeT E Inner join Salary_range S on E.salary between S.From_Sal and S.To_Sal Here is the table scripts for the same: create table EmployeeT( emp_id int, emp_name varchar(20), salary int,) insert into EmployeeT values (1, 'Ram', 4500); insert into EmployeeT values (3, 'Gopi', 17500); insert into EmployeeT values (3, 'Shyam', 9500); insert into EmployeeT values (4, 'Nisha', 13500); create table Salary_range( From_sal int, To_sal int, Grade varchar(20), ) insert into Salary_range values (0,5000,'A'); insert into Salary_range values (5001,10000,'B'); insert into Salary_range values (10001,15000,'C'); insert into Salary_range values (15001,20000,'D');
@siramar2453
@siramar2453 2 жыл бұрын
can I write :- Table name - emp_manager select e.empid, e.salary, m.manager_id, m.salary from emp_manager as e, emp_manager as m where e.salary > m.salary; pls sir tell me .
@vanshgrover_19
@vanshgrover_19 9 ай бұрын
Why did he use INNER JOIN and not SELF JOIN? Please someone tell me!
@ankitbansal6
@ankitbansal6 9 ай бұрын
There is no keyword as self join. When you join a table with itself it's called self join.
@vanshgrover_19
@vanshgrover_19 9 ай бұрын
@@ankitbansal6 can't we do this question by joining this employee table with itself?
@ankitbansal6
@ankitbansal6 9 ай бұрын
​@@vanshgrover_19 that's what we have done
@techtalk9903
@techtalk9903 2 жыл бұрын
god bless u xdd
@LastCall-z3i
@LastCall-z3i 21 күн бұрын
What if any employe doesn't have manager, let's say it is null
@ankitbansal6
@ankitbansal6 21 күн бұрын
Then do left join instead of inner
@TechnoSparkBigData
@TechnoSparkBigData 2 жыл бұрын
could you please share script to create these tables?
@mohammadabdullahansari6314
@mohammadabdullahansari6314 2 жыл бұрын
create table emp_manager ( emp_id int, emp_name varchar(20), salary int, manager_id int); insert into emp_manager values (1,'Ankit',10000,4),(2,'Mohit',15000,5),(3,'Vikas',10000,4),(4,'Rohit',5000,2), (5,'Mudit',12000,6),(6,'Agam',12000,2),(7,'Sanjay',9000,2),(8,'Ashish',5000,2); select * from emp_manager;
@yatinshekhar787
@yatinshekhar787 Жыл бұрын
3/122
@mohammadabdullahansari6314
@mohammadabdullahansari6314 2 жыл бұрын
Please share the script to make the table.
@mohammadabdullahansari6314
@mohammadabdullahansari6314 2 жыл бұрын
Nvm, I created the script: create table emp_manager ( emp_id int, emp_name varchar(20), salary int, manager_id int); insert into emp_manager values (1,'Ankit',10000,4),(2,'Mohit',15000,5),(3,'Vikas',10000,4),(4,'Rohit',5000,2), (5,'Mudit',12000,6),(6,'Agam',12000,2),(7,'Sanjay',9000,2),(8,'Ashish',5000,2); select * from emp_manager;
@akashwatar6633
@akashwatar6633 Жыл бұрын
1
@vignesh9173
@vignesh9173 2 жыл бұрын
I solved a similar problem on Leetcode today! Used sub queries for it!
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Cool
@vignesh9173
@vignesh9173 2 жыл бұрын
This was a really good explanation! I’ll definitely be following your videos! :D
@dikshachourasiya5561
@dikshachourasiya5561 Жыл бұрын
can't we use this condition on e.emp_id=m.manager_id ? Using this giving different output
@rishabhkesarwani5761
@rishabhkesarwani5761 2 жыл бұрын
Solution Alert select emp_id,emp_name,emp_salary,manager_id, manager_salary from( select a.emp_id,a.emp_name , a.salary as emp_salary,a.manager_id ,b.salary as manager_salary from emp_manager a join emp_manager b on a.manager_id =b.emp_id ) where emp_salary > manager_salary
@rajunaik8803
@rajunaik8803 2 жыл бұрын
scripts: create table emp_manager(emp_id int,emp_name varchar(20),salary int,manager_id int); insert into emp_manager values(1,'Ankit',10000,4); insert into emp_manager values(2,'Mohit',15000,5); insert into emp_manager values(3,'Vikas',10000,4); insert into emp_manager values(4,'Rohit',5000,2); insert into emp_manager values(5,'Mudit',12000,6); insert into emp_manager values(6,'Agam',12000,2); insert into emp_manager values(7,'Sanjay',9000,2); insert into emp_manager values(8,'Ashish',5000,2); select * from emp_manager; with cte1 as( select e.emp_id,e.emp_name,m.emp_name as manager_name,e.salary as emp_salary,m.salary as manager_salary from emp_manager e join emp_manager m on e.manager_id=m.emp_id ) select * from cte1 where emp_salary>manager_salary
@raushansingh7530
@raushansingh7530 6 ай бұрын
SELECT emp_name FROM emp_manager e WHERE salary > ( SELECT salary FROM emp_manager WHERE emp_id = e.manager_id ); this is another approach
@angelnadar1209
@angelnadar1209 2 жыл бұрын
Thanks Ankit ! I understood this but under self join , I came across a different question from sqlzoo: stops:stops(id, name) route(num, company, pos, stop) Find the routes involving two buses that can go from Craiglockhart to Lochend. Show the bus no. and company for the first bus, the name of the stop for the transfer, and the bus no. and company for the second bus. I tried to find the solution but getting some error: Could you please take this type of example . I also saw some solutions but coudn't understand it .like below is an working soution for above question but I didn't understood on what basis they are joining on company and num fields ? Sol: SELECT a.num, a.company, stops.name, d.num, d.company FROM route a JOIN route b ON a.company = b.company AND a.num = b.num JOIN stops ON b.stop = stops.id JOIN route c ON c.stop = stops.id JOIN route d ON c.company = d.company AND c.num = d.num WHERE a.stop = (SELECT id FROM stops WHERE name = 'Craiglockhart') AND d.stop = (SELECT id FROM stops WHERE name = 'Lochend') ORDER BY a.num, stops.name, d.num It woud be helpful if you can help me to understand on what basis they are joining on company and num fields ?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Sure I will check that question.
@manjumohan7731
@manjumohan7731 2 жыл бұрын
@ankitbansal6 :I thought self join will have where clause and condition and no need of join cause. Can you plz clarify whether self join and inner join are one and the same as the syntax of both is different and whether can it be used interchangeably Using self join for Emp Salary >Manager Salary: select e1.emp_id as Emp_id, e1.emp_name as employee_name, e1.salary as emp_salary, m1.emp_id as Manager_id, m1.emp_name as manager_name, m1.salary as manager_salary from emp3 e1,emp3 m1 where m1.emp_id=e1.manager_id and e1.salary>m1.salary order by emp_id;
@ankitbansal6
@ankitbansal6 2 жыл бұрын
It's same thing
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
6 SQL Joins you MUST know! (Animated + Practice)
9:47
Anton Putra
Рет қаралды 250 М.
Self Join and Its Demonstration
10:21
Parteek Bhatia: Simplifying Computer Education
Рет қаралды 46 М.
Lec-40: Self Join operation with Example | Database Management System
14:37
Practice SQL Interview Query | Big 4 Interview Question
14:47
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН