As we know the JOIN clause is very useful when merging more than two table or object data into a single unit. It combines different source elements into one and also creates the relationship between them. Using the join, you can grab the data based on your conditions. So, today I am going to show you how to form a query in SQL as well as LINQ for fetching the data.
Earlier we are using the JOIN in SQL to fetch the data from different tables from the database. SQL stands for Structured Query Language. This is query-based language to work on a relational-based database. In SQL, we manage the relationship between tables using the foreign key and primary key.
There are different types of join in SQL and these are Inner Join, Left Outer Join, Right Outer Join, Full Outer Join, and Cross Join.
But after introducing Linq with C# 3.0, there were huge changes in the programming world. Now, most of the developers use Linq for getting the data from the object. Linq stands for Language Integrated Query. It provides the facilities to access from in memory objects, database, Xml, and any other data source.
In this article, I am going to show the Join on Inventory database between customer and order table. Customer and Order tables are engaged to use CustomerId as primary key and foreign key. You can use the following scripts to generate the Inventory database.
Customer Table
Order Table
Product Table
Use the following scripts to create database and corresponding tables,
- USE [Inventory]
- GO
- /****** Object: Table [dbo].[Customer] Script Date: 1/4/2sql-join-query-with-linq16 11:2sql-join-query-with-linq:47 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [dbo].[Customer](
- [CustomerId] [int] IDENTITY(1,1) NOT NULL,
- [CustomerName] [varchar](6sql-join-query-with-linq) NULL,
- [Email] [varchar](1sql-join-query-with-linqsql-join-query-with-linq) NULL,
- [Address] [varchar](255) NULL,
- [MobileNo] [bigint] NULL,
- CONSTRAINT [PK__Customer__A4AE64D8D4F5Bsql-join-query-with-linq13] PRIMARY KEY CLUSTERED
- (
- [CustomerId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
- /****** Object: Table [dbo].[Orders] Script Date: 1/4/2sql-join-query-with-linq16 11:2sql-join-query-with-linq:47 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[Orders](
- [OrderId] [int] IDENTITY(1,1) NOT NULL,
- [OrderNumber] [int] NULL,
- [ProductId] [int] NULL,
- [CustomerId] [int] NULL,
- [Quantity] [int] NULL,
- [TotalAmount] [int] NULL,
- [OrderDate] [datetime] NULL,
- PRIMARY KEY CLUSTERED
- (
- [OrderId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- /****** Object: Table [dbo].[Product] Script Date: 1/4/2sql-join-query-with-linq16 11:2sql-join-query-with-linq:47 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [dbo].[Product](
- [ProductId] [int] IDENTITY(1,1) NOT NULL,
- [ProductName] [varchar](5sql-join-query-with-linq) NOT NULL,
- [UnitPrice] [int] NULL,
- [CategoryId] [int] NULL,
- [AddedDate] [datetime] NULL,
- PRIMARY KEY CLUSTERED
- (
- [ProductId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
- SET IDENTITY_INSERT [dbo].[Customer] ON
- INSERT [dbo].[Customer] ([CustomerId], [CustomerName], [Email], [Address], [MobileNo]) VALUES (1, N'Mukesh Kumar', N'[email protected]', N'New Delhi', 9898767654)
- INSERT [dbo].[Customer] ([CustomerId], [CustomerName], [Email], [Address], [MobileNo]) VALUES (2, N'Rahul Singh', N'[email protected]', N'Noida', 7878787865)
- INSERT [dbo].[Customer] ([CustomerId], [CustomerName], [Email], [Address], [MobileNo]) VALUES (3, N'SatishGupta', N'[email protected]', N'Mumbai', 9198765432)
- INSERT [dbo].[Customer] ([CustomerId], [CustomerName], [Email], [Address], [MobileNo]) VALUES (4, N'VishalSingh', N'VishalSingh', N'Patna', 7654324566)
- INSERT [dbo].[Customer] ([CustomerId], [CustomerName], [Email], [Address], [MobileNo]) VALUES (5, N'VinayPathak', N'[email protected]', N'Kanpur', 9898989765)
- SET IDENTITY_INSERT [dbo].[Customer] OFF
- SET IDENTITY_INSERT [dbo].[Orders] ON
- INSERT [dbo].[Orders] ([OrderId], [OrderNumber], [ProductId], [CustomerId], [Quantity], [TotalAmount], [OrderDate]) VALUES (1, 8sql-join-query-with-linqsql-join-query-with-linq1, 4, 1, 4, 12sql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linq, CAST(N'2sql-join-query-with-linq15-11-sql-join-query-with-linq3 22:21:13.143' AS DateTime))
- INSERT [dbo].[Orders] ([OrderId], [OrderNumber], [ProductId], [CustomerId], [Quantity], [TotalAmount], [OrderDate]) VALUES (2, 8sql-join-query-with-linqsql-join-query-with-linq2, 4, 2, 1, 3sql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linq, CAST(N'2sql-join-query-with-linq15-11-13 22:21:13.143' AS DateTime))
- INSERT [dbo].[Orders] ([OrderId], [OrderNumber], [ProductId], [CustomerId], [Quantity], [TotalAmount], [OrderDate]) VALUES (3, 8sql-join-query-with-linqsql-join-query-with-linq3, 2, 3, 2, 4sql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linq, CAST(N'2sql-join-query-with-linq15-12-15 22:21:13.143' AS DateTime))
- SET IDENTITY_INSERT [dbo].[Orders] OFF
- SET IDENTITY_INSERT [dbo].[Product] ON
- INSERT [dbo].[Product] ([ProductId], [ProductName], [UnitPrice], [CategoryId], [AddedDate]) VALUES (1, N'Samsung', 3sql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linq, 3, CAST(N'2sql-join-query-with-linq15-sql-join-query-with-linq5-13 22:21:13.143' AS DateTime))
- INSERT [dbo].[Product] ([ProductId], [ProductName], [UnitPrice], [CategoryId], [AddedDate]) VALUES (2, N'Noika', 2sql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linq, 4, CAST(N'2sql-join-query-with-linq15-sql-join-query-with-linq5-sql-join-query-with-linq3 22:21:13.143' AS DateTime))
- INSERT [dbo].[Product] ([ProductId], [ProductName], [UnitPrice], [CategoryId], [AddedDate]) VALUES (3, N'Sony', 15sql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linq, 5, CAST(N'2sql-join-query-with-linq15-sql-join-query-with-linq1-24 22:21:13.143' AS DateTime))
- INSERT [dbo].[Product] ([ProductId], [ProductName], [UnitPrice], [CategoryId], [AddedDate]) VALUES (4, N'Apple', 45sql-join-query-with-linqsql-join-query-with-linqsql-join-query-with-linq, 6, CAST(N'2sql-join-query-with-linq16-sql-join-query-with-linq1-sql-join-query-with-linq3 22:21:13.143' AS DateTime))
- SET IDENTITY_INSERT [dbo].[Product] OFF

Zain TariqPosted Sep 6, 2022, 11:07 AM
Nice Content
Amit MohantyPosted Jun 30, 2019, 11:29 PM
Nice article..
Jitendra WaghalePosted Aug 9, 2018, 9:55 AM
Thanks for sharing this Article!!
Gowtham KPosted Feb 24, 2016, 2:41 AM
Good One, Thanks for sharing:)
Santhakumar MunuswamyPosted Feb 23, 2016, 2:17 PM
Thanks for nice article
Sibeesh VenuPosted Feb 23, 2016, 9:13 AM
Nice Share
Shubham KumarPosted Feb 23, 2016, 5:28 AM
nice share
Raja TPosted Feb 23, 2016, 2:34 AM
Thanks for sharing!!
Anu VPosted Feb 23, 2016, 2:17 AM
Nice article sir
Yashwant VishwakarmaPosted Feb 23, 2016, 12:10 AM
nice article , thanks for sharing!!
Md Almajid KoushikPosted Feb 22, 2016, 11:08 PM
Thanks for share!!!!!
Humayun Kabir MamunPosted Feb 22, 2016, 10:35 PM
Nice...
Pankaj Kumar ChoudharyPosted Feb 22, 2016, 10:34 PM
Nice Explanation Sir.........
Vignesh ManiPosted Feb 22, 2016, 5:46 PM
Nice one
sreenivasa kPosted Feb 22, 2016, 2:47 PM
really superb
Ehsan SajjadPosted Feb 22, 2016, 1:56 PM
Nice article