Learn to Code SQL Example – SQL | Join (Cartesian Join & Self Join)

(SQL Example for Citizen Data Scientist & Business Analyst)

 

SQL | Join (Cartesian Join & Self Join)

In this article, we will discuss about the remaining two JOINS:

  • CARTESIAN JOIN
  • SELF JOIN

 

Consider the two tables below:
table

StudentCourse

table5

    1. CARTESIAN JOIN: The CARTESIAN JOIN is also known as CROSS JOIN. In a CARTESIAN JOIN there is a join for each row of one table to every row of another table. This usually happens when the matching column or WHERE condition is not specified.
      • In the absence of a WHERE condition the CARTESIAN JOIN will behave like a CARTESIAN PRODUCT . i.e., the number of rows in the result-set is the product of the number of rows of the two tables.
      • In the presence of WHERE condition this JOIN will function like a INNER JOIN.
      • Generally speaking, Cross join is similar to an inner join where the join-condition will always evaluate to True

      Syntax:

      SELECT table1.column1 , table1.column2, table2.column1...
      FROM table1
      CROSS JOIN table2;
      
      table1: First table.
      table2: Second table
      

Example Queries(CARTESIAN JOIN):

  • In the below query we will select NAME and Age from Student table and COURSE_ID from StudentCourse table. In the output you can see that each row of the table Student is joined with every row of the table StudentCourse. The total rows in the result-set = 4 * 4 = 16.
    SELECT Student.NAME, Student.AGE, StudentCourse.COURSE_ID
    FROM Student
    CROSS JOIN StudentCourse;
    

    Output:
    table_final

  1. SELF JOIN: As the name signifies, in SELF JOIN a table is joined to itself. That is, each row of the table is joined with itself and all other rows depending on some conditions. In other words we can say that it is a join between two copies of the same table.Syntax:
    SELECT a.coulmn1 , b.column2
    FROM table_name a, table_name b
    WHERE some_condition;
    
    table_name: Name of the table.
    some_condition: Condition for selecting the rows.
    

    Example Queries(SELF JOIN):

    SELECT a.ROLL_NO , b.NAME
    FROM Student a, Student b
    WHERE a.ROLL_NO < b.ROLL_NO;
    

    Output:
    tableeee

 

SQL tutorials for Business Analyst – SQL | SELF JOINS

 

Learn to Code SQL Example – SQL | Join (Cartesian Join & Self Join)

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!