SlideShare a Scribd company logo
3
Most read
6
Most read
8
Most read
Lab Lecture: Joins, views
General SELECT statement
Select <List of Columns and expressions (usually involving
columns)>
From <List of Tables & Join Operators>
Where <List of Row conditions joined together by And, Or,
Not>
Group By <list of grouping columns>
Having <list of group conditions connected by And, Or, Not >
Order By <list of sorting specifications>
Joins
• We can refer to multiple tables using SELECT.
• There are 5 types of JOIN:
• INNER JOIN
• FULL OUTER JOIN
• LEFT OUTER JOIN
• RIGHT OUTER JOIN
• CROSS JOIN
Table: Student
Table: StudentCourse
INNER JOIN
• This is one of the more frequent types of joins
• Finds all rows which meet the join condition
• Create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be
same.
Syntax: INNER JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• We can also write JOIN instead of INNER JOIN. JOIN is same as INNER
JOIN.
Example: INNER JOIN
• Write an SQL query to show the names and age of students enrolled
in different courses.
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM
Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
LEFT OUTER JOIN
• Contains all records between T1 and T2 that meet the join condition,
and then any records in T1 that don’t meet the join condition
• Rows for which the join condition is not met, the columns of T2 are padded
with nulls
• Also known as Left Join
Syntax: LEFT OUTER JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• We can also write LEFT JOIN instead of LEFT OUTER JOIN.
Example: LEFT OUTER JOIN
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
RIGHT OUTER JOIN
• Contains all records between T1 and T2 that meet the join condition,
and then any records in T2 that don’t meet the join condition
• Rows for which the join condition is not met, the columns of T1 are padded
with nulls
• Also known as RIGHT JOIN
Syntax: RIGHT OUTER JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• We can also write RIGHT JOIN instead of RIGHT OUTER JOIN.
FULL OUTER JOIN
• Contains all records between T1 and T2 that meet the join condition,
and the combination of a left outer join and right outer join are
appended onto the results.
• The rows for which there is no matching, the result-set will
contain NULL values.
Syntax: FULL OUTER JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• We can also write FULL JOIN instead of FULL OUTER JOIN.
Cross join
• Cross joins have no join condition
• Cross joins literally return the Cartesian product of the rows.
Syntax: CROSS JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
CROSS JOIN table2;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• Note that there is no ON keyword.
• Try to add WHERE clause at the end and see results.
Explicit vs Implicit Joins
• Explicit Joins
SELECT * FROM AddressBook a
INNER JOIN CallList c on a.id = c.addressBookId;
• Implicit Join
SELECT * FROM AddressBook a, CallList c
WHERE a.id = c.addressBookId;
• You can use any one you like.
Views
•A view is kind of like a virtual table
• Views are defined as queries (SELECT statements)
• They can be queried like tables
Syntax: VIEWS
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
• Here:
• view_name: Name for the View
• table_name: Name of the table
• condition: Condition to select rows
View example
CREATE VIEW StudentNames AS
SELECT Roll_no, name FROM Student
WHERE Roll_no > 3
• This would create a view that contains names and roll
number from table: Student
• You can further query this view. For example:
SELECT * FROM StudentNames;
• Similarly views can be created from different tables.
Questions
1. Write a query that returns the account ID for each
nonbusiness customer (customer.cust_type_cd = 'I')
with the customer’s federal ID (customer.fed_id) and
the name of the product on which the account is
based (product.name).
2. Construct a query that finds all employees whose
supervisor is assigned to a different department.
Retrieve the employees’ ID, first name, and last
name.

More Related Content

PDF
Advance database system(part 8)
Abdullah Khosa
 
PPTX
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
talhahakeem295
 
PPT
joins IN DATA BASE MANAGEMENT SYSTEMSppt
Uma Kakarlapudi
 
PPTX
Joins
Mritunjay Sharma
 
PDF
SQL JOINS
PuNeEt KuMaR
 
PPTX
SQL Join's
Muhammad Noman Fazil
 
PPTX
Sql joins
LokeshGogia2
 
PDF
Tipos de Joins para consultas em banco de dados.pdf
PabloLpez168453
 
Advance database system(part 8)
Abdullah Khosa
 
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
talhahakeem295
 
joins IN DATA BASE MANAGEMENT SYSTEMSppt
Uma Kakarlapudi
 
SQL JOINS
PuNeEt KuMaR
 
Sql joins
LokeshGogia2
 
Tipos de Joins para consultas em banco de dados.pdf
PabloLpez168453
 

Similar to Joins and Views.pptx (20)

PDF
SQL joins for Database Testing easy .pdf
RohitSharma189763
 
PPTX
Inner join and outer join
Nargis Ehsan
 
PPTX
Structured Query Language (SQL)
Hammad Rasheed
 
PPTX
SQL Basics
Hammad Rasheed
 
PPTX
joins and subqueries in big data analysis
SanSan149
 
PPTX
askndmf,dskmlf,bvdmk,v nmdsvjkmc,dvjkcmsdcvjnkmd
talhahakeem295
 
PPT
Join sql
Vikas Gupta
 
PPTX
SQL JOIN
Ritwik Das
 
PPTX
DBMS: Week 08 - Joins and Views in MySQL
RashidFaridChishti
 
PPT
Sql join
Vikas Gupta
 
ODT
Sql joins
Ashok Kumar
 
PPTX
More Complex SQL and Concurrency ControlModule 4.pptx
bgscseise
 
PPTX
V19 join method-c
Dhirendra Chauhan
 
PPTX
Day-06-Joining presentation of SQL lecture form uni .pptx
joynulabeden2
 
PPTX
PRESENTATION........................pptx
ejazayesha485
 
PPTX
Sql server ___________session_11-12(joins)
Ehtisham Ali
 
PPTX
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
kailasmanoj
 
PPTX
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
kailasmanoj
 
PDF
SQL JOINS.pdf
fariaafrin11
 
PDF
SQL Joins.pdf
NiravPanchal50
 
SQL joins for Database Testing easy .pdf
RohitSharma189763
 
Inner join and outer join
Nargis Ehsan
 
Structured Query Language (SQL)
Hammad Rasheed
 
SQL Basics
Hammad Rasheed
 
joins and subqueries in big data analysis
SanSan149
 
askndmf,dskmlf,bvdmk,v nmdsvjkmc,dvjkcmsdcvjnkmd
talhahakeem295
 
Join sql
Vikas Gupta
 
SQL JOIN
Ritwik Das
 
DBMS: Week 08 - Joins and Views in MySQL
RashidFaridChishti
 
Sql join
Vikas Gupta
 
Sql joins
Ashok Kumar
 
More Complex SQL and Concurrency ControlModule 4.pptx
bgscseise
 
V19 join method-c
Dhirendra Chauhan
 
Day-06-Joining presentation of SQL lecture form uni .pptx
joynulabeden2
 
PRESENTATION........................pptx
ejazayesha485
 
Sql server ___________session_11-12(joins)
Ehtisham Ali
 
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
kailasmanoj
 
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
kailasmanoj
 
SQL JOINS.pdf
fariaafrin11
 
SQL Joins.pdf
NiravPanchal50
 
Ad

Recently uploaded (20)

PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
CDH. pptx
AneetaSharma15
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
CDH. pptx
AneetaSharma15
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Ad

Joins and Views.pptx

  • 2. General SELECT statement Select <List of Columns and expressions (usually involving columns)> From <List of Tables & Join Operators> Where <List of Row conditions joined together by And, Or, Not> Group By <list of grouping columns> Having <list of group conditions connected by And, Or, Not > Order By <list of sorting specifications>
  • 3. Joins • We can refer to multiple tables using SELECT. • There are 5 types of JOIN: • INNER JOIN • FULL OUTER JOIN • LEFT OUTER JOIN • RIGHT OUTER JOIN • CROSS JOIN
  • 6. INNER JOIN • This is one of the more frequent types of joins • Finds all rows which meet the join condition • Create the result-set by combining all rows from both the tables where the condition satisfies i.e value of the common field will be same.
  • 7. Syntax: INNER JOIN SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 INNER JOIN table2 ON table1.matching_column = table2.matching_column; • Here: • table1: First table. • table2: Second table • matching_column: Column common to both the tables. • We can also write JOIN instead of INNER JOIN. JOIN is same as INNER JOIN.
  • 8. Example: INNER JOIN • Write an SQL query to show the names and age of students enrolled in different courses. SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM Student INNER JOIN StudentCourse ON Student.ROLL_NO = StudentCourse.ROLL_NO;
  • 9. LEFT OUTER JOIN • Contains all records between T1 and T2 that meet the join condition, and then any records in T1 that don’t meet the join condition • Rows for which the join condition is not met, the columns of T2 are padded with nulls • Also known as Left Join
  • 10. Syntax: LEFT OUTER JOIN SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 LEFT OUTER JOIN table2 ON table1.matching_column = table2.matching_column; • Here: • table1: First table. • table2: Second table • matching_column: Column common to both the tables. • We can also write LEFT JOIN instead of LEFT OUTER JOIN.
  • 11. Example: LEFT OUTER JOIN SELECT Student.NAME,StudentCourse.COURSE_ID FROM Student LEFT JOIN StudentCourse ON StudentCourse.ROLL_NO = Student.ROLL_NO;
  • 12. RIGHT OUTER JOIN • Contains all records between T1 and T2 that meet the join condition, and then any records in T2 that don’t meet the join condition • Rows for which the join condition is not met, the columns of T1 are padded with nulls • Also known as RIGHT JOIN
  • 13. Syntax: RIGHT OUTER JOIN SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 RIGHT OUTER JOIN table2 ON table1.matching_column = table2.matching_column; • Here: • table1: First table. • table2: Second table • matching_column: Column common to both the tables. • We can also write RIGHT JOIN instead of RIGHT OUTER JOIN.
  • 14. FULL OUTER JOIN • Contains all records between T1 and T2 that meet the join condition, and the combination of a left outer join and right outer join are appended onto the results. • The rows for which there is no matching, the result-set will contain NULL values.
  • 15. Syntax: FULL OUTER JOIN SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 FULL OUTER JOIN table2 ON table1.matching_column = table2.matching_column; • Here: • table1: First table. • table2: Second table • matching_column: Column common to both the tables. • We can also write FULL JOIN instead of FULL OUTER JOIN.
  • 16. Cross join • Cross joins have no join condition • Cross joins literally return the Cartesian product of the rows.
  • 17. Syntax: CROSS JOIN SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 CROSS JOIN table2; • Here: • table1: First table. • table2: Second table • matching_column: Column common to both the tables. • Note that there is no ON keyword. • Try to add WHERE clause at the end and see results.
  • 18. Explicit vs Implicit Joins • Explicit Joins SELECT * FROM AddressBook a INNER JOIN CallList c on a.id = c.addressBookId; • Implicit Join SELECT * FROM AddressBook a, CallList c WHERE a.id = c.addressBookId; • You can use any one you like.
  • 19. Views •A view is kind of like a virtual table • Views are defined as queries (SELECT statements) • They can be queried like tables
  • 20. Syntax: VIEWS CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE condition; • Here: • view_name: Name for the View • table_name: Name of the table • condition: Condition to select rows
  • 21. View example CREATE VIEW StudentNames AS SELECT Roll_no, name FROM Student WHERE Roll_no > 3 • This would create a view that contains names and roll number from table: Student • You can further query this view. For example: SELECT * FROM StudentNames; • Similarly views can be created from different tables.
  • 22. Questions 1. Write a query that returns the account ID for each nonbusiness customer (customer.cust_type_cd = 'I') with the customer’s federal ID (customer.fed_id) and the name of the product on which the account is based (product.name). 2. Construct a query that finds all employees whose supervisor is assigned to a different department. Retrieve the employees’ ID, first name, and last name.