SlideShare a Scribd company logo
1 of 22
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

Similar to Joins and Views.pptx

Similar to Joins and Views.pptx (20)

Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tables
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Sql joins
Sql joinsSql joins
Sql joins
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
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
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
2..basic queries.pptx
2..basic queries.pptx2..basic queries.pptx
2..basic queries.pptx
 
Sql joins final
Sql joins finalSql joins final
Sql joins final
 
Advance database system(part 8)
Advance database system(part 8)Advance database system(part 8)
Advance database system(part 8)
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Sql Tutorials
Sql TutorialsSql Tutorials
Sql Tutorials
 
joins and subqueries in big data analysis
joins and subqueries in big data analysisjoins and subqueries in big data analysis
joins and subqueries in big data analysis
 
Joins SQL Server
Joins SQL ServerJoins SQL Server
Joins SQL Server
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
SQL JOIN.pptx
SQL JOIN.pptxSQL JOIN.pptx
SQL JOIN.pptx
 
Joins & constraints
Joins & constraintsJoins & constraints
Joins & constraints
 
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
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
Joins and unions
Joins and unionsJoins and unions
Joins and unions
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Recently uploaded (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
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
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

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.