Easiest way to JOIN Two or More SQL Tables

Before reading this blog I expect you are familiar with creating Database and Tables.
 
STEPS
  • Suppose we have a Film Database with two table as TblMovie and TblActor.
  • Table structure of TblMovie is 

  •  Table structure of TblActor is 


  •  Lets we have datas in our Table like
    1. USE [Film]    
    2. GO    
    3. /****** Object:  Table [dbo].[TblMovie]    Script Date: 05/06/2015 10:21:01 ******/    
    4. SET ANSI_NULLS ON    
    5. GO    
    6. SET QUOTED_IDENTIFIER ON    
    7. GO    
    8. SET ANSI_PADDING ON    
    9. GO    
    10. CREATE TABLE [dbo].[TblMovie](    
    11.     [MovieId] [int] IDENTITY(1,1) NOT NULL,    
    12.     [MovieName] [varchar](50) NULL,    
    13.     [ReleaseYear] [varchar](50) NULL    
    14. ON [PRIMARY]    
    15. GO    
    16. SET ANSI_PADDING OFF    
    17. GO    
    18. SET IDENTITY_INSERT [dbo].[TblMovie] ON    
    19. INSERT [dbo].[TblMovie] ([MovieId], [MovieName], [ReleaseYear]) VALUES (1, N'My Fair Lady', N'1964')    
    20. INSERT [dbo].[TblMovie] ([MovieId], [MovieName], [ReleaseYear]) VALUES (2, N'Unforgiven', N'1992')    
    21. INSERT [dbo].[TblMovie] ([MovieId], [MovieName], [ReleaseYear]) VALUES (3, N'Time Machine', N'1997')    
    22. SET IDENTITY_INSERT [dbo].[TblMovie] OFF    
    23. /****** Object:  Table [dbo].[TblActor]    Script Date: 05/06/2015 10:21:01 ******/    
    24. SET ANSI_NULLS ON    
    25. GO    
    26. SET QUOTED_IDENTIFIER ON    
    27. GO    
    28. SET ANSI_PADDING ON    
    29. GO    
    30. CREATE TABLE [dbo].[TblActor](    
    31.     [MovieId] [intNOT NULL,    
    32.     [FirstName] [varchar](50) NULL,    
    33.     [LastName] [varchar](50) NULL    
    34. ON [PRIMARY]    
    35. GO    
    36. SET ANSI_PADDING OFF    
    37. GO    
    38. INSERT [dbo].[TblActor] ([MovieId], [FirstName], [LastName]) VALUES (1, N'Rex', N'Harrison')    
    39. INSERT [dbo].[TblActor] ([MovieId], [FirstName], [LastName]) VALUES (1, N'Audrey', N'Hepburn')    
    40. INSERT [dbo].[TblActor] ([MovieId], [FirstName], [LastName]) VALUES (2, N'Clint', N'Eastwood')    
    41. INSERT [dbo].[TblActor] ([MovieId], [FirstName], [LastName]) VALUES (5, N'Humphery', N'Bogart')   
  • Now we have to join the two tables TblMovie and TblActor on the basis of MovieId

    Write SQL Select Statement in the Query Editor
  1. select * from dbo.TblMovie 
Then select the query right click on it and Click Design Query in Editor like the below Image


  • The upcoming window after clicking Design Query in Editor is: 

  • Now right click on it select Add Table and then select Tables or Views you want to Join:

  • The next Pop up window will come like: 

  • Now click add and close the window.
  • Drag MovieId from TblMovie and put it in MovieId of TblActor and check FirstName and LastName like below image.

  • Now you can click ok and execute the query you will get the desired result or else you can do Filter to get your desired result.

I hope you liked it and in the same process you can join multiple tables by adding another Table and View.

Ebook Download
View all
SQL Jobs
Read by 0 people
Download Now!
Learn
View all