Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
Normalization in SQL Server
DDL and DML in SQL Server
SQL Server System Databases
SQL Server System Tables
Joins in SQL Server
SQL Server - Indexes
CTE in SQL Server
SQL Server - Locks
Optimization Techniques in SQL Server
How to Setup NDF Files in SQL Server
Deadlocks in SQL Server
Transactions in SQL Server
Difference UDF vs Stored Procedure
SQL Server Triggers
SQL Server Isolation Levels
SQL Server Pivot Tables
SQL Server - Group By Statement
SQL Server - Find Second highest Number Query
SQL Server - Recursion in User Defined Function
SQL Server Error Handling
Difference Table Variable and Temporary Table
SQL Server - Temporal Tables
User Defined Functions - SQL Server
Stored Procedures - SQL Server

Joins in SQL Server



Joins in SQL Server are used to combine rows from two or more tables based on related columns between them. Here are the main types of joins:

  1. INNER JOIN: Returns only the rows where there is a match in both tables.

sql
SELECT a.column1, b.column2
FROM table1 a
INNER JOIN table2 b ON a.common_column = b.common_column;
  1. LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table, and the matched rows from the right table. If there's no match, the result is NULL from the right side.

sql
SELECT a.column1, b.column2
FROM table1 a
LEFT JOIN table2 b ON a.common_column = b.common_column;
  1. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table, and the matched rows from the left table. If there's no match, the result is NULL from the left side.

sql
SELECT a.column1, b.column2
FROM table1 a
RIGHT JOIN table2 b ON a.common_column = b.common_column;
  1. FULL JOIN (or FULL OUTER JOIN): Returns rows when there is a match in one of the tables. This means it returns all rows from the left table and right table, with NULLs where there is no match.

sql
SELECT a.column1, b.column2
FROM table1 a
FULL JOIN table2 b ON a.common_column = b.common_column;
  1. CROSS JOIN: Returns the Cartesian product of the two tables, meaning it returns all possible combinations of rows.

sql
SELECT a.column1, b.column2
FROM table1 a
CROSS JOIN table2 b;
  1. SELF JOIN: A regular join where a table is joined with itself. It's useful for querying hierarchical data or comparing rows within the same table.

sql
SELECT a.column1, b.column2
FROM table1 a
INNER JOIN table1 b ON a.common_column = b.common_column;

These are the most common joins used in SQL Server. Would you like to see an example of a specific join or explore something more complex?




All rights reserved | Privacy Policy | Sitemap