SQL Query for Multiple Table SELECT, INSERT, UPDATE and DELETE WITH JOIN CLAUSE
Order Database
A. SELECT with multiple table in JOIN clause:
--Select records form OrderItem table for a given OrderNumber SELECT * FROM dbo.OrderItem INNER JOIN dbo.[Order] ON dbo.OrderItem.OrderId = dbo.[Order].Id WHERE dbo.[Order].OrderNumber = 542393 GO
Result:
B. INSERT with multiple table in JOIN clause:
--Insert records in Invoice table for a given OrderNumber INSERT INTO dbo.Invoice (OrderId, ItemCount, ItemTotalAmount) SELECT dbo.[Order].Id as OrderId, Sum(dbo.OrderItem.Quantity) as ItemCount, Sum(dbo.OrderItem.Quantity * dbo.OrderItem.UnitPrice) as ItemTotalAmount FROM dbo.OrderItem INNER JOIN dbo.[Order] ON dbo.OrderItem.OrderId = dbo.[Order].Id WHERE dbo.[Order].OrderNumber = 542393 GROUP BY dbo.[Order].Id GO
Result:
C. UPDATE with multiple table in JOIN clause:
--Updating LastModifiedDate in OrderItem table for a given OrderNumber UPDATE dbo.OrderItem SET dbo.OrderItem.LastModifiedDate = GETDATE() FROM dbo.OrderItem INNER JOIN dbo.[Order] ON dbo.OrderItem.OrderId = dbo.[Order].Id WHERE dbo.[Order].OrderNumber = 542393 GO
--Select records form OrderItem table for a given OrderNumber SELECT * FROM dbo.OrderItem INNER JOIN dbo.[Order] ON dbo.OrderItem.OrderId = dbo.[Order].Id WHERE dbo.[Order].OrderNumber = 542393 GO
Result:
D. DELETE with multiple table in JOIN clause:
--Deleting record from OrderItem table for a given OrderNumber DELETE dbo.OrderItem FROM dbo.OrderItem INNER JOIN dbo.[Order] ON dbo.OrderItem.OrderId = dbo.[Order].Id WHERE dbo.[Order].OrderNumber = 542393 GO
--Select records form OrderItem table for a given OrderNumber SELECT dbo.OrderItem.* FROM dbo.OrderItem INNER JOIN dbo.[Order] ON dbo.OrderItem.OrderId = dbo.[Order].Id WHERE dbo.[Order].OrderNumber = 542393 GO
Result:
.NET Professional | Microsoft Certified Professional | DZone’s Most Valuable Blogger | Web Developer | Author | Blogger
Doctorate in Computer Science and Engineering
Microsoft Certified Professional (MCP) with over 12+ years of software industry experience including development, implementation & deployment of applications in the .NET framework
Experienced and skilled Agile Developer with a strong record of excellent teamwork, successful coding & project management. Specialises in problem identification and proposal of alternative solutions. Provided knowledge and individual mentoring to team members as needed
Among top 3% overall in terms of contribution on Stack Overflow (~2.3 million people reached my posts). Part of the top 1% Stack Overflow answerers in ASP.NET technology.
DZone’s Most Valuable Blogger (MVB)
Created and actively maintain the TechCartNow.com tech blog while also editing, writing, and researching topics for publication.
Excellent skills in Application Development using C#/Vb.Net, .NET Framework, ASP.NET, MVC, ADO.NET, WCF, WPF, Web API, SQL Server, jQuery, Angular, React, BackboneJS