Skip to content
Advertisement

Combine 2 tables without primary key

I have 2 tables and wanna combine as below

Table Student:

Student ID | Student Name
-----------+-------------
100           John
101           Micheal

Table Subject:

Subject Name | Subject Desc
-------------+-------------
Math           Math
History        History

Combined:

Student ID | Student Name | Subject Name | Subject Desc
-----------+--------------+--------------+-------------
100           John          Math           Math 
100           John          History        History
101           Micheal       Math           Math
101           Micheal       History        History

I hope anyone can help. Thanks

Advertisement

Answer

In your case you need to use the SQL CROSS JOIN that produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table (if no WHERE clause is used along with CROSS JOIN).This kind of result is called ‘Cartesian Product’.

    SELECT [Student ID], [Student Name], [Subject Name], [Subject Desc]
    FROM Student
    CROSS JOIN
    Subject
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement