SlideShare a Scribd company logo
JOIN , SET OPERATIONS
STD-XII
INFORMATICS PRACTICES
LESSON-8
I) INTRODUCTION pg(395)
II) JOINING TABLES-
A join is a query that combines rows from two or more tables.
In a join query, more than one table is listed in the from clause of select statement.
The function of combining data from multiple tables is called joining.
The joining of 2 tables can be restricted(based on some condition) or unrestricted(nobinding condition)
UNRESTRICTED JOIN(CartesianProduct)(Cross Join)-When two tables are joined without any binding condition,
such a type of join is called unrestricted join and the result is called the CartesianProduct.
All possible concatenations are formed of all rows of both the tables emp and dept. That is , when no particular
rows(using where clause) and columns(using select list) are selected. Such an operation is known as Unrestricted
Join.
Def-A cross join (cartesian product) is a very basic type of join that simply matches each rowfromone table to
every rowfromanother table.
Def-A Cartesian Product-shows all possible concatenations formedby all the rows of both the tables.
A cartesian product is formedwhen-
-A join condition is omitted
-A join condition is invalid
-All rows in the firsttable are joined to all rows in the second table.
(RESTRICTED JOIN) (JOIN)
In a selectquery fetching rows fromtwo tables, if we put a restrictionby adding a condition(where clause), it
will no longer be the Cartesian product. It will now be called a restrictedjoinor simply a join.
Def- Join is a query which combines rows fromtwo or more tables based on a condition.
Fromthe tables below ,can u tell me the locationof Smith????(needfor a join)
WE come back to the earlier query-What is the locationof Smith.
Select ename,loc
from dept,emp
where dept.deptno=emp.deptno
and ename=‘smith’;
If we don’t write the condition ename=‘smith’, then the result will look like
This is the join statement
The 2 tables have to be joinedon a common column.
This col in both the tables –
1.Should be of similar dts
2.May not have the same name and in that case the
query will look like-
Select ename,loc
From emp,dept
Where deptno=dno;
Joins can be created between views and tables also-
Eg-
So to avoidthis
duplicatecolumn→
or
or
Qualifiedfield name
III)Qualified Field Names-
Prog2-WAQtodisplay the ename, sal , deptno, dname from both the tables.
Order this table in ascending order of deptno
Qualified field name-It is a column name written in a query preceded by its table name. If a column is commonto
both the tables SQL needs to know the table from where it should be shown . If this column is not proceded with
its table name SQL gives an error.
IV TABLE ALIAS-
It is a new (temporary)name givento the table in the fromclause.
Prog3-WAQtodisplay the deptno, deptname, empno,nameof all the employees.Order the rows 1st by deptno and
then by empno.
Qualifiedfield name
Additional Search conditions in JOINS-
Prog4-
Prog5-Displaydetails like deptno, dname, empno, ename, job and salary. Order the rows by employee number
with dept number. These details should be only for employee earnings at least 1500 and stores department.
Prog6-WAQtodisplay the deptno, dname, empno, ename, sal of all the employees who work in the ‘Acc’. Sort the
rows in descending order of deptno
V) JOINING MORE THAN 2 TABLES- an eg of a Non-equi join
Prog7-Create the following table salgrade
*increase all the salaries by 1000
Not an equi join
TYPES OF JOINS-
1.Equi-Join:-A join in which the columns are compared for equality.
In an equi join-Allthe columns from the joiningtable appearin the output even if they are identical.
Prog8 -WAQ to displayall the fields from both the tables(emp & dept)using an equi-join.
2.Non-Equi Join:- A non-equi join is a query which specifies some relation, other than equalitybetween the columns of
the joining table.
Prog9-WAQ to displaythe empname, their sal and grade for all the employees who belon to department 10
output
3.Natural Join-An equi-join minus one of the two-identical columns is called a Natural Join. The result of an
equi-join contains two identical columns,if one of the two identical columns is eliminated the result is called a
Natural Join.
A join in which only one of the identical columns coming fromthe Join tables exists is called a natural join.
Prog10-WAQto display all the fields from the emp and dept table using a natural join.
Joins covered so far-
1.Cross join
2.Equi join
3.Non-equi join
4.NaturalJoin
JOIN CLAUSE-Joiningtables using the join clause of the SQL statement:-
SYNTAX-
1. Cartesian Product/Unrestricted Join/Cross Join using the join clause
Prog11- Using the join clause create a cartesian product of the two tables emp and dept;
Cross join-It is a very basic type of a join which simply matches each row from one table to every row from
another table.
Select *
From <table1>
[Cross][Natural] join<table2>
[on(<join-condition>)|using(<join fields>)];
or
2. Equi-Join using the join clause
Prog12-Create an equi join for emp and dept table using the join clause
Equi-Join using Join clause with condition
Prog13-WAQto display the Equi-Join of tables emp and dept for employees having sal greater than 1300
3.Natural Join using the join clause
Prog14-Createa natural join of emp and dept table using the join clause[using sub clause]
If name deptno
is not same in
both the tables,
then this query
will give an error.
Prog15-A natural join of dept and emp tables for all emp with sal>1300(using the natural keyword)
Prog16-Createa natural join using the JOIN CLAUSE(using subclause) between emp and dept with employees of
sal>1300
DIFF BETWEEN USING AND ON SUBCLAUSES OF THE JOIN CLAUSE
LEFT AND RIGHT JOINS-
When you join tables based on some condition, u may find that only some, not all rows from either table match the
rows of other table. When u display an equi-join or natural join, it shows only the matched rows. What if u want to
know which all rows from a table did not match with other. In such a case, MYSQL left or right join can be very helpful
and it refers to the order in which the tables are put together and the results are displayed.
On subclause Using subclause
1) It creates an equi-join. 1)It creates a Naturaljoin
2) The on subclause requires a complete
Join condition.
2)The using clause requires just the name
of the join field.
3)Eg select * from emp
Join dept
on(dept.deptno=emp.deptno);
3)Eg-select * from emp
Join dept
Using(deptno);
LEFT JOIN-
SYNTAX select <select list>
From <table1>
LEFT JOIN<table2>
On<joining-condition>;
LEFT JOIN-
In Left Join all the rows from the first table will be returned whether there are matched in the second table or
not. For unmatched rows of first table, NULL is shown in columns of second table.
Prog17-Writethe output of the following query-
Prog18-Writethe output
Dept table is written first so it
will appear 1st in the output
Here emp is written first so it will
appearfirst on the left side.
RIGHT JOIN- All the rows from the 2nd table are going to be returned whether or not there are matches in the 1st
table.
Prog19-Writethe output
Prog20-Writethe output of
Emp is the 1st
Therefore emp will be
displayed1st on the left side
It is a right join, so all the values from the
right side table will appear, but emp will
come on the left side.
Def-
Cross join
Equi join
Non-equi join
Natural join
Left join
Right join
*Note
Select*
From
Where (join can come here)
Group by
Having
Order by
x--------x
end of joins
PERFORMING SET OPEARTIONS ON RELATION
Sql Joins tend to combine columns fromtwo or more tables(width wise) and SQL set operations tend to combine rows from
two or more tables(length wise)
We will be learningabout 3 SET operations-
UNION
Multiplequeries can be combinedin one by forming a unionof them.
The sql UNION operatorallowsmanipulationof results returned by 2 or more queries by combining the results of each query
into a single result set.
Eg-
UNION
INTERSECTION
MINUS
The Union of these two can be created as follows-
1)The duplicaterow of Kush has been automatically removed.
2)By defaultthe UNION operatorremoves the duplicaterows from the result
3)If the ALL option is used then all rows ,including duplicates,are included in the results.
The general form of the UNION operator is-
Select statement
UNION [ALL]
Select statement;
Prog-1
SQL UNION With WHERE returns the distinct
values from both the tables
SQL UNION ALL With WHERE returns duplicate
values from both the tables
Prog-2
Prog-3
Note: The column names in the result-set are usually equal to the column names in the first SELECT statement
Prog-4 Eg-11 pg(411)
Prog-5 Eg-12 pg(411)
Prog-6 Eg-13 pg(412)
MINUS OPERATOR
Prog 7
Prog-8 Eg 14 pg-413
INTERSECT Operator
In SQL, you can use an intersect operation(intersect) to return rows that are common between two tables.
INTERSECT operation returns common rows from both the left and right tables.
This is useful when u want to find results that are in common between two queries. Prog9-
Inner join is same
as join.
same
x------------------x
End of lesson
EXERCISE
x----------x
End of lesson

More Related Content

Similar to 3)12th_L8_Join-Set-Operations.pdf

Sql join
Sql  joinSql  join
Sql join
Vikas Gupta
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
Join sql
Join sqlJoin sql
Join sql
Vikas Gupta
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
Ishucs
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
Anusha sivakumar
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
Brian Foote
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
uzmasulthana3
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
Ritwik Das
 
Introduction to MySQL - Part 2
Introduction to MySQL - Part 2Introduction to MySQL - Part 2
Introduction to MySQL - Part 2webhostingguy
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
Joshi Vinay
 
SQL report
SQL reportSQL report
SQL report
Ahmad Zahid
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
Techglyphs
 
Sql Queries
Sql QueriesSql Queries
Sql Querieswebicon
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
ASHABOOPATHY
 
Ch7
Ch7Ch7
Ch7
muteddy
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Nitesh Singh
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 

Similar to 3)12th_L8_Join-Set-Operations.pdf (20)

Sql join
Sql  joinSql  join
Sql join
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
 
Join sql
Join sqlJoin sql
Join sql
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
Introduction to MySQL - Part 2
Introduction to MySQL - Part 2Introduction to MySQL - Part 2
Introduction to MySQL - Part 2
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
SQL report
SQL reportSQL report
SQL report
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
Ch7
Ch7Ch7
Ch7
 
Sql basics v2
Sql basics v2Sql basics v2
Sql basics v2
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Sql
SqlSql
Sql
 

Recently uploaded

openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 

Recently uploaded (20)

openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 

3)12th_L8_Join-Set-Operations.pdf

  • 1. JOIN , SET OPERATIONS STD-XII INFORMATICS PRACTICES LESSON-8
  • 2. I) INTRODUCTION pg(395) II) JOINING TABLES- A join is a query that combines rows from two or more tables. In a join query, more than one table is listed in the from clause of select statement. The function of combining data from multiple tables is called joining. The joining of 2 tables can be restricted(based on some condition) or unrestricted(nobinding condition) UNRESTRICTED JOIN(CartesianProduct)(Cross Join)-When two tables are joined without any binding condition, such a type of join is called unrestricted join and the result is called the CartesianProduct.
  • 3. All possible concatenations are formed of all rows of both the tables emp and dept. That is , when no particular rows(using where clause) and columns(using select list) are selected. Such an operation is known as Unrestricted Join. Def-A cross join (cartesian product) is a very basic type of join that simply matches each rowfromone table to every rowfromanother table. Def-A Cartesian Product-shows all possible concatenations formedby all the rows of both the tables. A cartesian product is formedwhen- -A join condition is omitted -A join condition is invalid -All rows in the firsttable are joined to all rows in the second table.
  • 4. (RESTRICTED JOIN) (JOIN) In a selectquery fetching rows fromtwo tables, if we put a restrictionby adding a condition(where clause), it will no longer be the Cartesian product. It will now be called a restrictedjoinor simply a join. Def- Join is a query which combines rows fromtwo or more tables based on a condition. Fromthe tables below ,can u tell me the locationof Smith????(needfor a join)
  • 5. WE come back to the earlier query-What is the locationof Smith. Select ename,loc from dept,emp where dept.deptno=emp.deptno and ename=‘smith’; If we don’t write the condition ename=‘smith’, then the result will look like This is the join statement The 2 tables have to be joinedon a common column. This col in both the tables – 1.Should be of similar dts 2.May not have the same name and in that case the query will look like- Select ename,loc From emp,dept Where deptno=dno;
  • 6. Joins can be created between views and tables also- Eg- So to avoidthis duplicatecolumn→ or or Qualifiedfield name III)Qualified Field Names-
  • 7. Prog2-WAQtodisplay the ename, sal , deptno, dname from both the tables. Order this table in ascending order of deptno Qualified field name-It is a column name written in a query preceded by its table name. If a column is commonto both the tables SQL needs to know the table from where it should be shown . If this column is not proceded with its table name SQL gives an error. IV TABLE ALIAS- It is a new (temporary)name givento the table in the fromclause. Prog3-WAQtodisplay the deptno, deptname, empno,nameof all the employees.Order the rows 1st by deptno and then by empno. Qualifiedfield name
  • 8. Additional Search conditions in JOINS- Prog4- Prog5-Displaydetails like deptno, dname, empno, ename, job and salary. Order the rows by employee number with dept number. These details should be only for employee earnings at least 1500 and stores department.
  • 9. Prog6-WAQtodisplay the deptno, dname, empno, ename, sal of all the employees who work in the ‘Acc’. Sort the rows in descending order of deptno V) JOINING MORE THAN 2 TABLES- an eg of a Non-equi join Prog7-Create the following table salgrade *increase all the salaries by 1000 Not an equi join
  • 10. TYPES OF JOINS- 1.Equi-Join:-A join in which the columns are compared for equality. In an equi join-Allthe columns from the joiningtable appearin the output even if they are identical. Prog8 -WAQ to displayall the fields from both the tables(emp & dept)using an equi-join. 2.Non-Equi Join:- A non-equi join is a query which specifies some relation, other than equalitybetween the columns of the joining table. Prog9-WAQ to displaythe empname, their sal and grade for all the employees who belon to department 10 output
  • 11. 3.Natural Join-An equi-join minus one of the two-identical columns is called a Natural Join. The result of an equi-join contains two identical columns,if one of the two identical columns is eliminated the result is called a Natural Join. A join in which only one of the identical columns coming fromthe Join tables exists is called a natural join. Prog10-WAQto display all the fields from the emp and dept table using a natural join. Joins covered so far- 1.Cross join 2.Equi join 3.Non-equi join 4.NaturalJoin
  • 12. JOIN CLAUSE-Joiningtables using the join clause of the SQL statement:- SYNTAX- 1. Cartesian Product/Unrestricted Join/Cross Join using the join clause Prog11- Using the join clause create a cartesian product of the two tables emp and dept; Cross join-It is a very basic type of a join which simply matches each row from one table to every row from another table. Select * From <table1> [Cross][Natural] join<table2> [on(<join-condition>)|using(<join fields>)]; or
  • 13. 2. Equi-Join using the join clause Prog12-Create an equi join for emp and dept table using the join clause Equi-Join using Join clause with condition Prog13-WAQto display the Equi-Join of tables emp and dept for employees having sal greater than 1300 3.Natural Join using the join clause Prog14-Createa natural join of emp and dept table using the join clause[using sub clause] If name deptno is not same in both the tables, then this query will give an error.
  • 14. Prog15-A natural join of dept and emp tables for all emp with sal>1300(using the natural keyword) Prog16-Createa natural join using the JOIN CLAUSE(using subclause) between emp and dept with employees of sal>1300
  • 15. DIFF BETWEEN USING AND ON SUBCLAUSES OF THE JOIN CLAUSE LEFT AND RIGHT JOINS- When you join tables based on some condition, u may find that only some, not all rows from either table match the rows of other table. When u display an equi-join or natural join, it shows only the matched rows. What if u want to know which all rows from a table did not match with other. In such a case, MYSQL left or right join can be very helpful and it refers to the order in which the tables are put together and the results are displayed. On subclause Using subclause 1) It creates an equi-join. 1)It creates a Naturaljoin 2) The on subclause requires a complete Join condition. 2)The using clause requires just the name of the join field. 3)Eg select * from emp Join dept on(dept.deptno=emp.deptno); 3)Eg-select * from emp Join dept Using(deptno); LEFT JOIN- SYNTAX select <select list> From <table1> LEFT JOIN<table2> On<joining-condition>; LEFT JOIN-
  • 16. In Left Join all the rows from the first table will be returned whether there are matched in the second table or not. For unmatched rows of first table, NULL is shown in columns of second table. Prog17-Writethe output of the following query- Prog18-Writethe output Dept table is written first so it will appear 1st in the output Here emp is written first so it will appearfirst on the left side.
  • 17. RIGHT JOIN- All the rows from the 2nd table are going to be returned whether or not there are matches in the 1st table. Prog19-Writethe output Prog20-Writethe output of Emp is the 1st Therefore emp will be displayed1st on the left side It is a right join, so all the values from the right side table will appear, but emp will come on the left side.
  • 18. Def- Cross join Equi join Non-equi join Natural join Left join Right join *Note Select* From Where (join can come here) Group by Having Order by
  • 20. PERFORMING SET OPEARTIONS ON RELATION Sql Joins tend to combine columns fromtwo or more tables(width wise) and SQL set operations tend to combine rows from two or more tables(length wise) We will be learningabout 3 SET operations- UNION Multiplequeries can be combinedin one by forming a unionof them. The sql UNION operatorallowsmanipulationof results returned by 2 or more queries by combining the results of each query into a single result set. Eg- UNION INTERSECTION MINUS The Union of these two can be created as follows- 1)The duplicaterow of Kush has been automatically removed. 2)By defaultthe UNION operatorremoves the duplicaterows from the result 3)If the ALL option is used then all rows ,including duplicates,are included in the results.
  • 21. The general form of the UNION operator is- Select statement UNION [ALL] Select statement; Prog-1
  • 22. SQL UNION With WHERE returns the distinct values from both the tables SQL UNION ALL With WHERE returns duplicate values from both the tables Prog-2 Prog-3 Note: The column names in the result-set are usually equal to the column names in the first SELECT statement
  • 27. Prog-8 Eg 14 pg-413
  • 28. INTERSECT Operator In SQL, you can use an intersect operation(intersect) to return rows that are common between two tables. INTERSECT operation returns common rows from both the left and right tables. This is useful when u want to find results that are in common between two queries. Prog9- Inner join is same as join. same
  • 31.
  • 32.
  • 33.
  • 34.