SlideShare uma empresa Scribd logo
1 de 27
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
JOINS AND UNION IN MySQL
SHAMEEM AT
atshameem89@hotmail.com
www.facebook.com/Shamee
m At
twitter.com/ Shameem_at
in.linkedin.com/in/ shameem
at
+91-9446-986-108
JOINS AND UNION
IN
MySQL
JOIN
• To query data from two or more tables, based on a relationship between
certain column in these tables.
• Tables in a database are often related to each other with keys.
• Primary key is a column (or a combination of columns) with a unique
value for each row.
• Foreign Key in one table points to a Primary Key in another table.
Different JOINs in MySQL
INNER JOIN(JOIN):
Return rows when there is at least one match in both tables.
LEFT JOIN:
Return all rows from the left table, even if there are no matches
in the right table.
RIGHT JOIN:
Return all rows from the right table, even if there are no matches in the left
table.
FULL JOIN:
Return rows when there is a match in one of the tables.
But It does not support in MySQL.
Tbl_dept
pk_dept_id dept_name
1 CSE
2 ECE
3 EEE
4 CE
tbl_student
pk_student_id student_name student_place fk_dept_id
1 baabtra1 Calicut 1
2 baabtra2 cochin 2
3 baabtra3 Kannur 3
4 baabtra4 ooty 3
5 baabtra5 munnar 1
6 baabtra6 mlp NULL
INNER JOIN (JOIN)
• The INNER JOIN keyword returns rows when there is at least one match in
both tables.
Syntax:
SELECT
column name(s)
FROM
table_name1
INNER JOIN
table_name2
ON
table_name1.column_name=table_name2.column_name
Tbl_dept Tbl_student
INNER JOIN Example
student_name dept_name
baabtra1 CSE
baabtra5 CSE
baabtra2 ECE
baabtra3 EEE
baabtra4 EEE
SELECT tbl_student.student_name,tbl_dept.dept_name
FROM tbl_dept
JOIN tbl_student
ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;
LEFT JOIN
• The LEFT JOIN keyword returns all rows from the left table (table_name1),
even if there are no matches in the right table (table_name2).
Syntax:
SELECT
column_name(s)
FROM
table_name1
LEFT JOIN
table_name2
ON
table_name1.column_name=table_name2.column_name.
Tbl_dept Tbl_student
LEFT JOIN Example
SELECT tbl_student.student_name,tbl_dept.dept_name
FROM tbl_dept
LEFT JOIN tbl_student
ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;
student_name dept_name
Baabtra1 CSE
Baabtra5 CSE
Baabtra2 ECE
Baabtra3 EEE
Baabtra4 EEE
NULL CE
RIGHT JOIN
• The RIGHT JOIN keyword returns all the rows from the right table
(table_name2), even if there are no matches in the left table (table_name1).
Syntax:
SELECT
column_name(s)
FROM
table_name1
RIGHT JOIN
table_name2
ON
table_name1.column_name=table_name2.column_name.
Tbl_dep Tbl_student
RIGHT JOIN Example
SELECT tbl_student.student_name,tbl_dept.dept_name
FROM tbl_dept
RIGHT JOIN tbl_student
ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;
Student_name Dept_name
Baabtra1 CSE
Baabtra2 ECE
Baabtra3 EEE
Baabtra4 EEE
Baabtra5 CSE
Baabtra6 NULL
FULL JOIN
• There is no FULL JOIN in MySQL.
• We can possible this by using both LEFT and RIGHT Joins together with
UNION key word.
Tbl_dep Tbl_student
UNION
• The UNION operator combines two or more SELECT statements.
• The UNION operator is used to combine the result-set of two or more
SELECT statements.
• Each SELECT statement within the UNION must have the same number of
columns.
• The columns must also have similar data types.
• The columns in each SELECT statement must be in the same order.
UNION Syntax
SELECT column _ name(s)
FROM Table_name1
UNION
SELECT column _ name(s)
FROM table_name2
• The column names in the result-set of a UNION are always equal to the
column names in the first SELECT statement in the UNION.
UNION ALL
• The UNION operator selects only distinct values by default. If you need to
allow duplicate values, use UNION ALL.
Syntax
SELECT column_name (s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
UNION Example
SELECT tbl_student.student_name,tbl_dept.dept_name
FROM tbl_dept
LEFT JOIN tbl_student
ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id
UNION
SELECT tbl_student.student_name,tbl_dept.dept_name
FROM tbl_dept
RIGHT JOIN tbl_student
ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;
Student_name Dept_name
Baabtra1 CSE
Baabtra5 CSE
Baabtra2 ECE
Baabtra3 EEE
Baabtra4 EEE
NULL CE
Baabtra6 NULL
UNION ALL Example
SELECT tbl_student.student_name,tbl_dept.dept_name
FROM tbl_dept
LEFT JOIN tbl_student
ON tbl_dept.pk_dept_id=tbl_student.fk_dept_id
UNION ALL
SELECT tbl_student.student_name,tbl_dept.dept_name
FROM tbl_dept
RIGHT JOIN tbl_student
ON tbl_dept.pk_dept_id=tbl_student.fk_dept_id;
Student_name Dept_name
Baabtra1 CSE
baabtra5 CSE
baabtra2 ECE
baabtra3 EEE
baabtra4 EEE
NULL CE
baabtra1 CSE
baabtra2 ECE
baabtra3 EEE
baabtra4 EEE
baabtra5 CSE
baabtra6 NULL
SELECT tbl_dept.dept_name
FROM tbl_dept
UNION
SELECT tbl_student.student_name
FROM tbl_student;
dept_name
CSE
ECE
EEE
CE
Baabtra1
Baabtra2
Baabtra3
Baabtra4
Baabtra5
baabtra6
THANKS
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us

Mais conteúdo relacionado

Mais procurados (20)

Types Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql ServerTypes Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql Server
 
SQL
SQLSQL
SQL
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
joins in database
 joins in database joins in database
joins in database
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
Join query
Join queryJoin query
Join query
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
SQL commands
SQL commandsSQL commands
SQL commands
 
SQL Join Basic
SQL Join BasicSQL Join Basic
SQL Join Basic
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Data Manipulation Language
Data Manipulation LanguageData Manipulation Language
Data Manipulation Language
 
Group By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQLGroup By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQL
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
 
Sql join
Sql  joinSql  join
Sql join
 
Sql joins
Sql joinsSql joins
Sql joins
 
Join
JoinJoin
Join
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
Create table
Create tableCreate table
Create table
 

Destaque (8)

crystal report
crystal reportcrystal report
crystal report
 
Travel agencies and working of travel agencies
Travel agencies and working of travel agenciesTravel agencies and working of travel agencies
Travel agencies and working of travel agencies
 
3tier architecture
3tier architecture3tier architecture
3tier architecture
 
Joins SQL Server
Joins SQL ServerJoins SQL Server
Joins SQL Server
 
Database
DatabaseDatabase
Database
 
Server side scripting
Server side scriptingServer side scripting
Server side scripting
 
Ado.net in Asp.net
Ado.net in Asp.netAdo.net in Asp.net
Ado.net in Asp.net
 
GDS (Global Distribution System)
GDS (Global Distribution System)GDS (Global Distribution System)
GDS (Global Distribution System)
 

Semelhante a Joins and unions (20)

Joins and unions
Joins and unionsJoins and unions
Joins and unions
 
Unions and joins in mysql
Unions and joins in mysqlUnions and joins in mysql
Unions and joins in mysql
 
Join sql
Join sqlJoin sql
Join sql
 
Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tables
 
SQL Joins and View.pptx
SQL Joins and View.pptxSQL Joins and View.pptx
SQL Joins and View.pptx
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
SQL Join's
SQL Join'sSQL Join's
SQL Join's
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Views, Triggers, Functions, Stored Procedures,  Indexing and JoinsViews, Triggers, Functions, Stored Procedures,  Indexing and Joins
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
 
Join in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer JoinJoin in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer Join
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
 
SQL
SQLSQL
SQL
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
Joins and different types of joins dani
Joins and different types of joins daniJoins and different types of joins dani
Joins and different types of joins dani
 
Joins.ppt
Joins.pptJoins.ppt
Joins.ppt
 
Lesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfLesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdf
 

Mais de baabtra.com - No. 1 supplier of quality freshers

Mais de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Último

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Último (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Joins and unions

  • 1.
  • 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3. JOINS AND UNION IN MySQL SHAMEEM AT atshameem89@hotmail.com www.facebook.com/Shamee m At twitter.com/ Shameem_at in.linkedin.com/in/ shameem at +91-9446-986-108
  • 5. JOIN • To query data from two or more tables, based on a relationship between certain column in these tables. • Tables in a database are often related to each other with keys. • Primary key is a column (or a combination of columns) with a unique value for each row. • Foreign Key in one table points to a Primary Key in another table.
  • 6. Different JOINs in MySQL INNER JOIN(JOIN): Return rows when there is at least one match in both tables. LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table. RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table. FULL JOIN: Return rows when there is a match in one of the tables. But It does not support in MySQL.
  • 8. tbl_student pk_student_id student_name student_place fk_dept_id 1 baabtra1 Calicut 1 2 baabtra2 cochin 2 3 baabtra3 Kannur 3 4 baabtra4 ooty 3 5 baabtra5 munnar 1 6 baabtra6 mlp NULL
  • 9. INNER JOIN (JOIN) • The INNER JOIN keyword returns rows when there is at least one match in both tables. Syntax: SELECT column name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name Tbl_dept Tbl_student
  • 10. INNER JOIN Example student_name dept_name baabtra1 CSE baabtra5 CSE baabtra2 ECE baabtra3 EEE baabtra4 EEE SELECT tbl_student.student_name,tbl_dept.dept_name FROM tbl_dept JOIN tbl_student ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;
  • 11. LEFT JOIN • The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2). Syntax: SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name. Tbl_dept Tbl_student
  • 12. LEFT JOIN Example SELECT tbl_student.student_name,tbl_dept.dept_name FROM tbl_dept LEFT JOIN tbl_student ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id; student_name dept_name Baabtra1 CSE Baabtra5 CSE Baabtra2 ECE Baabtra3 EEE Baabtra4 EEE NULL CE
  • 13. RIGHT JOIN • The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if there are no matches in the left table (table_name1). Syntax: SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name. Tbl_dep Tbl_student
  • 14. RIGHT JOIN Example SELECT tbl_student.student_name,tbl_dept.dept_name FROM tbl_dept RIGHT JOIN tbl_student ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id; Student_name Dept_name Baabtra1 CSE Baabtra2 ECE Baabtra3 EEE Baabtra4 EEE Baabtra5 CSE Baabtra6 NULL
  • 15. FULL JOIN • There is no FULL JOIN in MySQL. • We can possible this by using both LEFT and RIGHT Joins together with UNION key word. Tbl_dep Tbl_student
  • 16. UNION • The UNION operator combines two or more SELECT statements. • The UNION operator is used to combine the result-set of two or more SELECT statements. • Each SELECT statement within the UNION must have the same number of columns. • The columns must also have similar data types. • The columns in each SELECT statement must be in the same order.
  • 17. UNION Syntax SELECT column _ name(s) FROM Table_name1 UNION SELECT column _ name(s) FROM table_name2 • The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.
  • 18. UNION ALL • The UNION operator selects only distinct values by default. If you need to allow duplicate values, use UNION ALL. Syntax SELECT column_name (s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2
  • 19. UNION Example SELECT tbl_student.student_name,tbl_dept.dept_name FROM tbl_dept LEFT JOIN tbl_student ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id UNION SELECT tbl_student.student_name,tbl_dept.dept_name FROM tbl_dept RIGHT JOIN tbl_student ON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;
  • 20. Student_name Dept_name Baabtra1 CSE Baabtra5 CSE Baabtra2 ECE Baabtra3 EEE Baabtra4 EEE NULL CE Baabtra6 NULL
  • 21. UNION ALL Example SELECT tbl_student.student_name,tbl_dept.dept_name FROM tbl_dept LEFT JOIN tbl_student ON tbl_dept.pk_dept_id=tbl_student.fk_dept_id UNION ALL SELECT tbl_student.student_name,tbl_dept.dept_name FROM tbl_dept RIGHT JOIN tbl_student ON tbl_dept.pk_dept_id=tbl_student.fk_dept_id;
  • 22. Student_name Dept_name Baabtra1 CSE baabtra5 CSE baabtra2 ECE baabtra3 EEE baabtra4 EEE NULL CE baabtra1 CSE baabtra2 ECE baabtra3 EEE baabtra4 EEE baabtra5 CSE baabtra6 NULL
  • 23. SELECT tbl_dept.dept_name FROM tbl_dept UNION SELECT tbl_student.student_name FROM tbl_student; dept_name CSE ECE EEE CE Baabtra1 Baabtra2 Baabtra3 Baabtra4 Baabtra5 baabtra6
  • 25.
  • 26. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com