JOINS
INNER JOIN
LEFT JOIN
The LEFT JOIN command returns all
rows from the left table, and the
matching rows from the right table.
The result is NULL from the right side, if
there is no.
RIGHT JOIN
The RIGHT JOIN keyword returns all
records from the right table (table2),
and the matching records from the left
table (table1). The result is 0 records
from the left side, if there is no match.
1. create table emp(id integer unsigned not null auto_increment,name varchar(45) not null,des
varchar(45) not null ,doj date not null,primary key(id));
2. insert into emp(name,des,doj) values
('krish','manager','2019-09-10'),
('Moses','hr','2019-09-10'),
('Joyal','ACC','2019-09-11'),
('John','sales','2019-09-23'),
('daniel','sales','2019-10-02');
3. select *from emp;
4. create table salary(sid int not null auto_increment,id int not null,sdate date not null,amt int
not null,primary key(sid));
5. insert into salary(id,sdate,amt) values
(1,'2019-09-30',10000),
(2,'2019-09-30',7500),
(3,'2019-09-30',6000),
(4,'2019-09-30',4000);
In above able we have inserted details for 4-employees , the 5 th employee DOJ is 2019-10-02 .. so
we can filtered it using some joins commands
6. select emp.name,emp.des,salary.sdate,salary.amt from emp inner join salary on
emp.id=salary.id;
While using joins we want to mention dot operator as prefix in each fields of the table like
( Ex: emp.name it means “name” – field from “emp” table )
“from emp” it means like “A”- table
“emp inner join salary” here salary like “B”- table , here “inner join” like intersection of
two sets( means it extract common records from both tables)
“on emp.id=salary.id” here we have check unique records based on “id” of both “emp”
and “Salary” tables.
Result:
Table-1: emp Table name: salary
Id Id
Name DesSdate DOJ Amt
1 Krish Manager 2019-09-10
2 Moses HR 2019-09-10
3 Joyal ACC 2019-09-11
4 John Sales 2019-09-23
5 daneil sales 2019-10-02
Sid
1 1 2019-09-30 10000
2 2 2019-09-30 7500
3 3 2019-09-30 6000
4 4 2019-09-30 4000
Emp.id = salary.id
Id
Id
RESULT
FIND “sep” month updated salary details (LEFT join)
7. select emp.id,emp.name,emp.des,salary.sdate,salary.amt from emp left join salary on
emp.id=salary.id;
RIGHT JOIN
8. select emp.id,emp.name,emp.des,salary.sdate,salary.amt from emp right join salary on
emp.id=salary.id;
in above right join result and inner join result both are giving same result , just you can update
another salary details in “Salary” table , then you can view the different result .
9. insert into salary(id,sdate,amt) values (6,'2019-09-01',7000);
10. select *from salary;
But 6th record not exists in “emp” – table now you can use “right join” then view the different result
11. select emp.id,emp.name,emp.des,salary.sdate,salary.amt from emp inner join salary on
emp.id=salary.id;
( it give same result )
12. select emp.id,emp.name,emp.des,salary.sdate,salary.amt from emp right join salary on
emp.id=salary.id;
now we got 5 records but 5th record displayed without name
13. select emp.id,emp.name,emp.des,salary.sdate,salary.amt from emp left join salary on
emp.id=salary.id;
Now we got 5th record without having Sdate and Amt because he joined on 2019-10-02
14. select *from students;
15. select *from attendance;
16. select students.name,attendance.ADATE,attendance.ASTATUS from students inner join
attendance on students.id=attendance.id;
Provide bus fair based on city ?
17. select distinct city from students;
18. select name,city from students;
19. select name,city,
CASE
WHEN city='Salem' THEN 100
END
as Bus_Fair from students;
20. select name,city,
CASE
WHEN city='Salem' THEN 100
WHEN city='Namakkal' THEN 150
WHEN city='Hosur' THEN 350
ELSE 0
END
as Bus_Fair from students;
21. select name,city,
CASE
WHEN city='Salem' THEN 100
WHEN city='Namakkal' THEN 150
WHEN city='Hosur' THEN 350
ELSE 0
END
as Bus_Fair from students WHERE city='Hosur';
22. update students set city='Madurai' where city='Salem';
23. select name,city,
CASE
WHEN city='Salem' THEN 100
WHEN city='Namakkal' THEN 150
WHEN city='Hosur' THEN 350
ELSE 0
END
as Bus_Fair from students;