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

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

Ankit Bansal

2 года назад

86,448 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Muaz malik
Muaz malik - 17.08.2023 09:23

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

Ответить
Kiran Kapruwan
Kiran Kapruwan - 16.08.2023 10:01

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.

Ответить
Bhavesh Bharti
Bhavesh Bharti - 03.08.2023 12:53

Thanks for so clear explanation. Understood

Ответить
Hello to happiness
Hello to happiness - 02.07.2023 11:21

please increase ur voice ..just now i subscribed

Ответить
Arpit Mishra
Arpit Mishra - 22.06.2023 06:13

nicely explained!

Ответить
pavan gsk
pavan gsk - 19.06.2023 17:09

lovely explaining

Ответить
Mohit Malviya
Mohit Malviya - 17.06.2023 19:48

grt work sir

Ответить
Akash Watar
Akash Watar - 11.06.2023 00:21

1

Ответить
shubham sri
shubham sri - 20.05.2023 07:38

Your video is very helpful, sir. The way you explain complex concepts is very understandable

Ответить
🌠SpaceSpectacle🚀
🌠SpaceSpectacle🚀 - 02.05.2023 17:11

please upload data set as well or provide drive link whenever u upload practical

Ответить
Divyagoud A
Divyagoud A - 30.03.2023 18:18

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

Ответить
Umesh Roy
Umesh Roy - 30.03.2023 08:44

when I run the query say table does not exit

Ответить
anchit gupta
anchit gupta - 28.03.2023 20:11

I have solved this question same way in leetcode

Ответить
LazyGeek
LazyGeek - 23.03.2023 13:51

great, thanks for the explanation

Ответить
Yatin Shekhar
Yatin Shekhar - 20.03.2023 14:10

3/122

Ответить
vin
vin - 09.03.2023 14:49

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

Ответить
Rohith S
Rohith S - 04.03.2023 16:37

Thanks for this Ankit. It was really helpful for me in understanding the concept of self joins. Really appreciated!

Ответить
Praveen Kumar Rai
Praveen Kumar Rai - 03.03.2023 20:45

create table emp_manager(emp_id int,emp_name varchar(50),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 );

Ответить
Suman Acharya
Suman Acharya - 14.02.2023 07:16

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

Ответить
Mahesh
Mahesh - 27.01.2023 11:13

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?

Ответить
aniket raut
aniket raut - 23.01.2023 21:03

Thank you for these amazing videos its helping me to learn the sql in simplest way.

Ответить
Rishabh Kesarwani
Rishabh Kesarwani - 21.01.2023 13:16

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

Ответить
Vènom
Vènom - 20.01.2023 12:03

the most lucid explanation on whole utube without any complication.. amazing bro !!

Ответить
Manju Mohan
Manju Mohan - 15.01.2023 22:01

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

Ответить
MD Shuaib Saqib B
MD Shuaib Saqib B - 10.01.2023 12:55

Hi Ankit,
Kindly post the Table schema queries in the description for reference and solution query too..

Ответить
Saurabh Shrivastava
Saurabh Shrivastava - 05.01.2023 20:21

Beautifully explained

Ответить
Ajinkya Adhotre
Ajinkya Adhotre - 25.12.2022 18:29

DAMN ! the two table creation on excel was soo good way of explaining ! you earned a subscriber ! 🤩

Ответить
Priyanka Patil
Priyanka Patil - 10.12.2022 08:42

Thank you sir..

Ответить
HITESH JANUSKAR
HITESH JANUSKAR - 20.11.2022 11:47

Very helpful concept 👍🏻

Ответить
Sai Rajesh
Sai Rajesh - 26.10.2022 17:12

IT'S ALWAYS THE UNDERRATED VID THAT'S LEGIT! THANK YOU! <3

Ответить
Tech Talk
Tech Talk - 18.10.2022 13:44

god bless u xdd

Ответить
NIshant Bodke
NIshant Bodke - 18.10.2022 12:39

thank you so much dude you're a god

Ответить
Raju Naik
Raju Naik - 16.10.2022 13:40

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

Ответить
Aditya Kaushik
Aditya Kaushik - 11.10.2022 20:57

Thanks for this example !
what would be the solution to find out those employees who have highest salary under each manager.

Ответить
amrita sinha
amrita sinha - 02.10.2022 04:29

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.

Ответить
amrita sinha
amrita sinha - 26.09.2022 14:13

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.

Ответить
Lalit Singh Jeena
Lalit Singh Jeena - 19.09.2022 10:59

Thanks for making it so easy

Ответить
BOTD34TH
BOTD34TH - 29.08.2022 16:42

select e.emp_id, e.emp_name, e.salary as emp_salary, f.emp_name as manager_nm, f.salary as manager_sal from emp_manager e
inner join emp_manager f
on e.manager_id = f.emp_id
where e.salary > f.salary
order by f.emp_name;

Ответить
Milind Zuge
Milind Zuge - 14.08.2022 12:38

Thanks Ankit, Great Content❣

Ответить
Amrut Deshpande
Amrut Deshpande - 05.08.2022 23:15

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!

Ответить
Moyeen Shaikh
Moyeen Shaikh - 27.07.2022 17:40

Sir, if we inner join emp id of emp with manager id of manager table, will it work same?

Ответить
Ash
Ash - 27.07.2022 10:12

Awesome Ankit got your channel from Linkeidin and its best thing happened.

Ответить
Abhi_goodFella
Abhi_goodFella - 23.07.2022 16:43

Well Ankit's salary is greater than his manager's salary at Amazon 😀. Nice tutorial btw. Great work.

Ответить
Sir Amar
Sir Amar - 20.07.2022 15:13

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 .

Ответить
S PD
S PD - 19.07.2022 15:39

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 );

Ответить
Vishnu Jatav
Vishnu Jatav - 17.07.2022 15:19

Very nice explanation

Ответить
Mohammad Abdullah Ansari
Mohammad Abdullah Ansari - 07.07.2022 19:13

Please share the script to make the table.

Ответить
Rajiv jain
Rajiv jain - 30.06.2022 15:48

Hi Ankit
Could you make video on performance tunning topic.

Ответить
Nivedita Singh
Nivedita Singh - 13.06.2022 17:47

Hey Ankit, if we write query as " on e.emp_id = m.mgr_id" and rest query remains same will it yield same result?

Ответить
Neel shah
Neel shah - 31.05.2022 12:59

Hi Ankit,
Can you please make videos on Python and ML please would be really helpful.
Thanks

Ответить