Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What is the Result when we Join two tables using Inner join but without using primary key conditions or (on)?

user-image
Question ajoutée par Utilisateur supprimé
Date de publication: 2015/12/01
Jehangir Wahid
par Jehangir Wahid , Lead Software Developer , Inaequo Solutions

Actually in SQL, JOIN is used when you want to retrieve data from multiple tables. The Query with JOIN Clause will return rows including columns which you have mentioned in the SELECT Clause and obviously it will contain columns of interest from the tables, which are joined together.

 

Applying INNER JOIN on tables results rows as long as the mentioned columns from one table are associated with the mentioned columns of the other table. For Example You have a tables tblStudent and tblDepartment, and you want to retrieve students associated with atleast one department.

 

SELECT tblStudents.studentName, tblDepartment.departmentName

FROM tblStudents

INNER JOIN tblDepartment

ON tblStudents.studentID = tblDepartment.studentID

ORDER BY tblStudents.studentName

So you will get rows as long as there are students studying in atleast one department. It will not catch the students those are not enrolled yet against any of the departments.

 

But, 

In your case the query will be incorrect. and an error will be shown while writing the query.

Mohammed Aamir Murthaza
par Mohammed Aamir Murthaza , Software Quality Assurance Engineer , Cognizant Technology and Solutions

When you need data from multiple tables, the results you want and the join type you select affect the position and join sequence in which you place the tables in your query. You can specify the order in which tables are joined in the Query and View designers or in the JOIN clause of the SQL SELECT statement.

For example, suppose you want all the records from a field in one table and only those records in another table that match on the join condition. You could use the LEFT JOIN operation and place the first table on the left side and the second table on the right side of the JOIN clause. However, if you selected a RIGHT JOIN operation, you would need to reverse the order of these two tables to obtain the same results.

The following code example creates a query that displays three sets of results, but all of them retrieve all values from the field in the first table and only those values from the field in the second table that meet the join condition. The join condition is specified by the ON clause.

 

When there's no primary or foreign key, we won't able to perform an inner join, it will throw an error message.

 

More Questions Like This