SlideShare a Scribd company logo
1 of 35
Download to read offline
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

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

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 

Recently uploaded (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 

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.