SQL Joins for Efficient Database Queries

SQL is the most demanding skill from Dacades and will be the most demanding in the future. Whatever system we build deals with data for sure, though NoSQL is getting popular day by day SQL has its importance and use.

Sample Tables

Tables

Inner Join in SQL

Inner Join

  • Inner join produces only the set of records that match in both Table A and Table B.
  • Inner Joins do not have to use equality to join the fields.
  • Canuse<, >,<>
  • Most commonly used

Inner Join (2)

SELECT * FROM TableA
INNER JOIN TableB ON
TableA.PK = TableB.PK
//This is the same as doing
SELECT * FROM TableA, TableB WHERE TableA.PK = TableB.PK
SELECT *
FROMTableA
INNER JOIN TableB ON TableA.PK > TableB.PK

Inner Join (3)

Natural Join in SQL

A Natural Join is just an inner equi-join where the join is implicitly created using any matching columns between the two tables

Example

SELECT * FROM TableA NATURAL JOIN TableB

Left Outer Join in SQL

The left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null.

Left outer join(1)

Left outer join table

SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.PK = TableB.PK

Right Outer Join in SQL

The right outer join produces a complete set of records from Table B, with the matching records (where available) in Table A. If there is no match, the left side will contain null.

Right Outer Join

Right Outer Join Table

SELECT * FROM TableA RIGHT OUTER JOIN TableB ON TableA.PK = TableB.PK

Full Outer Join in SQL

Full outer join produces the set of all records in Table A and Table B, with matching records from both sides where available. If there is no match, the missing side will contain null.

Full Outer Join

Full Outer Join Table

SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.PK = TableB.PK
MySQL doesn’t have FULL OUTER JOIN

Simulate using UNION, LEFT and RIGHT JOINs

SELECT * FROM TableA LEFT JOIN TableB ON TableA.PK = TableB.PK
UNION
SELECT * FROM TableA RIGHT JOIN TableB
ON TableA.PK = TableB.PK

Left Join Excluding Inner Join

This query will return all of the records in the left table (table A) that do not match any records in the right table (table B).

Left Join Diagram

Left Join Diagram Table

SELECT * FROM TableA LEFT JOIN TableB ON TableA.PK = TableB.PK WHERE TableB.PK IS NULL

Perform left outer join, then exclude the records we don't want from the right side via a where clause.

Right Join Excluding Inner Join

This query will return all of the records in the right table (table B) that do not match any records in the left table (table A).

Right Join Excluding Diagram

Right Join Excluding Table

SELECT * FROM TableA RIGHT JOIN TableB ON TableA.PK = TableB.PK WHERE TableA.PK IS NULL

Perform right outer join, then exclude the records we don't want from the left side via a where clause.

Full Outer Excluding Inner Join

This query will return all of the records in Table A and Table B that do not have a matching record in the other table.

Full Outer Excluding

Full Outer Excluding Table

SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.PK = TableB.PK
WHERE TableA.PK IS NULL OR TableB.PK IS NULL

Cross Join

  • A cross join is a Cartesian Product join – it is every record in Table A combined with every record in Table B.
  • It gives the same results as not using a WHERE clause when querying two tables in MySQL
  • SELECT * from TableA CROSS JOIN TableB
  • SELECT * from TableA, TableB


Similar Articles
Logiciel Softtech Pvt. Ltd.
We help you transform your business ideas with custom products, migrate from legacy applications.