22
MarGrab Deal : Flat 30% off on live classes + 2 free self-paced courses! - SCHEDULE CALL
Joins in SQL server are used to retrieve data from two or more related tables. In general tables are related to each other using foreign key constraints.
Let’s understand Join types with examples and the differences between them.
Read More: Different Types of SQL Keys
1.Employee Table (tblEmployee)
2.Department table (tblDepartment)
Return only matching rows between both the tables. On matching rows are eliminated.
Read: Microsoft Power BI Certification Exam- 2024 Updated Guidelines
SELECT Name,Gender,Salary,DepartmentName FROM tblEmployee INNER JOIN tblDepartment ON tblEmployee.DepartmentId=tblDepartment.Id
Read More: Different Types of SQL Database Functions
If you look at the output we got only 8 rows but in Employee table it has 10 rows. We didn’t got James and Russell records. This is because DepartmentId, in Employee table is NULL for these two employees and doesn’t match ID column in Department table.
Returns all the matching rows and non-matching row from left table. In reality, LEFT JOIN and INNER JOIN are extensively used.
SELECT Name,Gender,Salary,DepartmentName FROM tblEmployee LEFT OUTER JOIN tblDepartment ON tblEmployee.DepartmentId=tblDepartment.Id
Read: How To Become SQL Certified :Boost Your Career & Income With Right Certification
Returns all the matching rows and non-matching row from right table.
SELECT Name,Gender,Salary,DepartmentName FROM tblEmployee RIGHT JOIN tblDepartment ON tblEmployee.DepartmentId=tblDepartment.Id
Returns all the rows from both left and right of the table, including non-matching rows.
SELECT Name,Gender,Salary,DepartmentName FROM tblEmployee FULL JOIN tblDepartment ON tblEmployee.DepartmentId=tblDepartment.Id
Cross Join: -
Read: What is Complex SQL Queries? Explain Complex SQL Queries with Examples
Read More: Different Types of SQL Injection
Cross Join produces the cartesian product of the two tables involved in the join. For example, in the employee table we have 10 records and in the department table we have 4 records. So as a cross join between two tables it will produce 40 records. Cross join shouldn’t have ON clause.
SELECT Name,Gender,Salary,DepartmentName FROM tblEmployee CROSS JOIN tblDepartment
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
Normalization in SQL | 1NF, 2NF, 3NF and BCNF with Examples 487.3k
View vs Table In SQL: A Comprehensive Insight You Have To Know 615.7k
SQL Data Types for Oracle PL/SQL, MySQL, SQL Server, and MS Access 270.9k
Learn How to Add Column Using SQL Server ALTER TABLE ADD Column Statement 84.2k
How to Insert Multiple Rows Using Stored Procedure in SQL? 630k
Receive Latest Materials and Offers on SQL Server Course
Interviews