SlideShare una empresa de Scribd logo
1 de 8
Descargar para leer sin conexión
Home Contents




Joining tables

In this part of the SQLite tutorial, we will join tables in SQLite.


The real power and benefits from relational databases come from joining tables. The SQL JOIN clause

combines records from two or more tables in a database. There are basically two types of joins. INNER

and OUTER.


In this part of the tutorial, we will work with Customers and Reservations tables.




  sqlite> SELECT * FROM Customers;




  CustomerId      Name




  ----------      -----------




  1               Paul Novak




  2               Terry Neils




  3               Jack Fonda




  4               Tom Willis


Values from the Customers table.




  sqlite> SELECT * FROM Reservations;




  Id   CustomerId        Day
--     ----------      ----------




  1      1               2009-22-11




  2      2               2009-28-11




  3      2               2009-29-11




  4      1               2009-29-11




  5      3               2009-02-12


Values from the Reservations tables.




Inner joins

The inner join is the most common type of joins. It is the default join also. The inner join selects only

those records from database tables that have matching values. We have three types of INNER JOINS.

INNER JOIN, NATURAL INNER JOIN and CROSS INNER JOIN. The INNER keyword can be omitted.




INNER JOIN



  sqlite> SELECT Name, Day FROM Customers AS C JOIN Reservations




                                  AS R ON C.CustomerId=R.CustomerId;




  Name             Day




  -----------      ----------




  Paul Novak       2009-22-11
Terry Neils     2009-28-11




  Terry Neils     2009-29-11




  Paul Novak      2009-29-11




  Jack Fonda      2009-02-12




In this SELECT statement, we have selected all customers, that have made some reservations. Note,

that we have omitted the INNER keyword.


The statement is equivalent to the following one:




  sqlite> SELECT Name, Day FROM Customers, Reservations




                                WHERE Customers.CustomerId = Reservations.CustomerId;




  Name            Day




  -----------     ----------




  Paul Novak      2009-22-11




  Terry Neils     2009-28-11




  Terry Neils     2009-29-11




  Paul Novak      2009-29-11
Jack Fonda       2009-02-12


We get the same data.




NATURAL INNER JOIN

The NATURAL INNER JOIN automatically uses all the matching column names for the join. In our

tables, we have a column named CustomerId in both tables.




  sqlite> SELECT Name, Day FROM Customers NATURAL JOIN Reservations;




  Name             Day




  -----------      ----------




  Paul Novak       2009-22-11




  Terry Neils      2009-28-11




  Terry Neils      2009-29-11




  Paul Novak       2009-29-11




  Jack Fonda       2009-02-12


We get the same data. The SQL statement is less verbose.




CROSS INNER JOIN

The CROSS INNER JOIN combines all records from one table with all records from another table. This

type of join has little practical value. It is also called a cartesian product of records.
sqlite> SELECT Name, Day FROM Customers CROSS JOIN Reservations;




  Name           Day




  -----------    ----------




  Paul Novak     2009-22-11




  Paul Novak     2009-28-11




  Paul Novak     2009-29-11




  Paul Novak     2009-29-11




  Paul Novak     2009-02-12




  Terry Neils    2009-22-11




  Terry Neils    2009-28-11




  Terry Neils    2009-29-11




  Terry Neils    2009-29-11




  Terry Neils    2009-02-12




  ...


The same result can be achieved with the following SQL statement:
SELECT Name, Day FROM Customers, Reservations;




Outer joins

An outer join does not require each record in the two joined tables to have a matching record. There

are three types of outer joins. Left outer joins, right outer joins, and full outer joins. SQLite only

supports left outer joins.




LEFT OUTER JOIN

The LEFT OUTER JOIN returns all values from the left table, even if there is no match with the right

table. It such rows, there will be NULL values. In other words, left outer join returns all the values from

the left table, plus matched values from the right table. Note, that the OUTER keyword can be omitted.




  sqlite> SELECT Name, Day FROM Customers LEFT JOIN Reservations




                                   ON Customers.CustomerID=Reservations.CustomerId;




  Name             Day




  -----------      ----------




  Paul Novak       2009-22-11




  Paul Novak       2009-29-11




  Terry Neils      2009-28-11




  Terry Neils      2009-29-11




  Jack Fonda       2009-02-12
Tom Willis      NULL


Here we have all customers with their reservations, plus a customer, who has no reservation. There is

NULL value in his row.


We can use the USING keyword to achieve the same result. The SQL statement will be less verbose.




  sqlite> SELECT Name, Day FROM Customers LEFT JOIN Reservations USING (CustomerId);




  Name            Day




  -----------     ----------




  Paul Novak      2009-22-11




  Paul Novak      2009-29-11




  Terry Neils     2009-28-11




  Terry Neils     2009-29-11




  Jack Fonda      2009-02-12




  Tom Willis      NULL


Same result, with shorter SQL statement.




NATURAL LEFT OUTER JOIN

The NATURAL LEFT OUTER JOIN automatically uses all the matching column names for the join.




  sqlite> SELECT Name, Day FROM Customers NATURAL LEFT OUTER JOIN Reservations;
Name             Day




  -----------      ----------




  Paul Novak       2009-22-11




  Paul Novak       2009-29-11




  Terry Neils      2009-28-11




  Terry Neils      2009-29-11




  Jack Fonda       2009-02-12




  Tom Willis       NULL


Same result, but with fewer key strokes.


In this part of the SQLite tutorial, we were joining tables.

Más contenido relacionado

Similar a Joining tables

Sq lite functions
Sq lite functionsSq lite functions
Sq lite functions
punu_82
 
May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfolio
maywoo
 
The sqlite3 commnad line tool
The sqlite3 commnad line toolThe sqlite3 commnad line tool
The sqlite3 commnad line tool
punu_82
 
The select statement
The select statementThe select statement
The select statement
punu_82
 
Oracle 10g Performance: chapter 00 sampling
Oracle 10g Performance: chapter 00 samplingOracle 10g Performance: chapter 00 sampling
Oracle 10g Performance: chapter 00 sampling
Kyle Hailey
 
SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1
Umair Amjad
 

Similar a Joining tables (20)

Assignment#06
Assignment#06Assignment#06
Assignment#06
 
Sq lite functions
Sq lite functionsSq lite functions
Sq lite functions
 
May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfolio
 
SQL
SQLSQL
SQL
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
 
Sql Server 2000
Sql Server 2000Sql Server 2000
Sql Server 2000
 
COIS 420 - Practice01
COIS 420 - Practice01COIS 420 - Practice01
COIS 420 - Practice01
 
Oracle Database features every developer should know about
Oracle Database features every developer should know aboutOracle Database features every developer should know about
Oracle Database features every developer should know about
 
Les01.ppt
Les01.pptLes01.ppt
Les01.ppt
 
Les01.pptx
Les01.pptxLes01.pptx
Les01.pptx
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
 
Sql queries
Sql queriesSql queries
Sql queries
 
The sqlite3 commnad line tool
The sqlite3 commnad line toolThe sqlite3 commnad line tool
The sqlite3 commnad line tool
 
Sql statements function join
Sql statements function joinSql statements function join
Sql statements function join
 
5 Cool Things About SQL
5 Cool Things About SQL5 Cool Things About SQL
5 Cool Things About SQL
 
The select statement
The select statementThe select statement
The select statement
 
Oracle 10g Performance: chapter 00 sampling
Oracle 10g Performance: chapter 00 samplingOracle 10g Performance: chapter 00 sampling
Oracle 10g Performance: chapter 00 sampling
 
Constraints constraints of oracle data base management systems
Constraints  constraints of oracle data base management systemsConstraints  constraints of oracle data base management systems
Constraints constraints of oracle data base management systems
 
SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1
 

Último

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 

Último (20)

Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 

Joining tables

  • 1. Home Contents Joining tables In this part of the SQLite tutorial, we will join tables in SQLite. The real power and benefits from relational databases come from joining tables. The SQL JOIN clause combines records from two or more tables in a database. There are basically two types of joins. INNER and OUTER. In this part of the tutorial, we will work with Customers and Reservations tables. sqlite> SELECT * FROM Customers; CustomerId Name ---------- ----------- 1 Paul Novak 2 Terry Neils 3 Jack Fonda 4 Tom Willis Values from the Customers table. sqlite> SELECT * FROM Reservations; Id CustomerId Day
  • 2. -- ---------- ---------- 1 1 2009-22-11 2 2 2009-28-11 3 2 2009-29-11 4 1 2009-29-11 5 3 2009-02-12 Values from the Reservations tables. Inner joins The inner join is the most common type of joins. It is the default join also. The inner join selects only those records from database tables that have matching values. We have three types of INNER JOINS. INNER JOIN, NATURAL INNER JOIN and CROSS INNER JOIN. The INNER keyword can be omitted. INNER JOIN sqlite> SELECT Name, Day FROM Customers AS C JOIN Reservations AS R ON C.CustomerId=R.CustomerId; Name Day ----------- ---------- Paul Novak 2009-22-11
  • 3. Terry Neils 2009-28-11 Terry Neils 2009-29-11 Paul Novak 2009-29-11 Jack Fonda 2009-02-12 In this SELECT statement, we have selected all customers, that have made some reservations. Note, that we have omitted the INNER keyword. The statement is equivalent to the following one: sqlite> SELECT Name, Day FROM Customers, Reservations WHERE Customers.CustomerId = Reservations.CustomerId; Name Day ----------- ---------- Paul Novak 2009-22-11 Terry Neils 2009-28-11 Terry Neils 2009-29-11 Paul Novak 2009-29-11
  • 4. Jack Fonda 2009-02-12 We get the same data. NATURAL INNER JOIN The NATURAL INNER JOIN automatically uses all the matching column names for the join. In our tables, we have a column named CustomerId in both tables. sqlite> SELECT Name, Day FROM Customers NATURAL JOIN Reservations; Name Day ----------- ---------- Paul Novak 2009-22-11 Terry Neils 2009-28-11 Terry Neils 2009-29-11 Paul Novak 2009-29-11 Jack Fonda 2009-02-12 We get the same data. The SQL statement is less verbose. CROSS INNER JOIN The CROSS INNER JOIN combines all records from one table with all records from another table. This type of join has little practical value. It is also called a cartesian product of records.
  • 5. sqlite> SELECT Name, Day FROM Customers CROSS JOIN Reservations; Name Day ----------- ---------- Paul Novak 2009-22-11 Paul Novak 2009-28-11 Paul Novak 2009-29-11 Paul Novak 2009-29-11 Paul Novak 2009-02-12 Terry Neils 2009-22-11 Terry Neils 2009-28-11 Terry Neils 2009-29-11 Terry Neils 2009-29-11 Terry Neils 2009-02-12 ... The same result can be achieved with the following SQL statement:
  • 6. SELECT Name, Day FROM Customers, Reservations; Outer joins An outer join does not require each record in the two joined tables to have a matching record. There are three types of outer joins. Left outer joins, right outer joins, and full outer joins. SQLite only supports left outer joins. LEFT OUTER JOIN The LEFT OUTER JOIN returns all values from the left table, even if there is no match with the right table. It such rows, there will be NULL values. In other words, left outer join returns all the values from the left table, plus matched values from the right table. Note, that the OUTER keyword can be omitted. sqlite> SELECT Name, Day FROM Customers LEFT JOIN Reservations ON Customers.CustomerID=Reservations.CustomerId; Name Day ----------- ---------- Paul Novak 2009-22-11 Paul Novak 2009-29-11 Terry Neils 2009-28-11 Terry Neils 2009-29-11 Jack Fonda 2009-02-12
  • 7. Tom Willis NULL Here we have all customers with their reservations, plus a customer, who has no reservation. There is NULL value in his row. We can use the USING keyword to achieve the same result. The SQL statement will be less verbose. sqlite> SELECT Name, Day FROM Customers LEFT JOIN Reservations USING (CustomerId); Name Day ----------- ---------- Paul Novak 2009-22-11 Paul Novak 2009-29-11 Terry Neils 2009-28-11 Terry Neils 2009-29-11 Jack Fonda 2009-02-12 Tom Willis NULL Same result, with shorter SQL statement. NATURAL LEFT OUTER JOIN The NATURAL LEFT OUTER JOIN automatically uses all the matching column names for the join. sqlite> SELECT Name, Day FROM Customers NATURAL LEFT OUTER JOIN Reservations;
  • 8. Name Day ----------- ---------- Paul Novak 2009-22-11 Paul Novak 2009-29-11 Terry Neils 2009-28-11 Terry Neils 2009-29-11 Jack Fonda 2009-02-12 Tom Willis NULL Same result, but with fewer key strokes. In this part of the SQLite tutorial, we were joining tables.