I have 3 tables below. I want to create a views table joining with three tables. Example:
Employee Table
| EID | Name |
|---|---|
| 001 | Mr. Devid |
| 002 | Mr. Kamal |
Location Table
| LID | Location |
|---|---|
| 1 | Dhaka |
| 2 | Khulna |
| 3 | Rangpur |
Tour Table
| EID | DepartureID | ArrivalID |
|---|---|---|
| 001 | 1 | 2 |
| 002 | 3 | 1 |
My Views Table
| EID | Name | Departure | Arrival |
|---|---|---|---|
| 001 | Mr. Devid | Dhaka | Khulna |
| 002 | Mr. Kamal | Rangpur | Dhaka |
How can i create the View table?

Gurpreet AroraRank #327 · 7.6K reputationPosted Sep 25, 2024, 12:19 AM EDT
Query :
CREATE VIEW MyViewsTable AS SELECT e.EID, e.Name, d.Location AS Departure, a.Location AS Arrival FROM Employee e JOIN Tour t ON e.EID = t.EID JOIN Location d ON t.DepartureID = d.LID JOIN Location a ON t.ArrivalID = a.LID;
Explanation:
Employee e: This represents theEmployeetable (aliased ase).Tour t: This represents theTourtable (aliased ast), which linksEmployeewithLocationthroughEID.Location d: This is the first join with theLocationtable (aliased asd), which matches theDepartureIDwithLIDto get the departure location.Location a: This is the second join with theLocationtable (aliased asa), which matches theArrivalIDwithLIDto get the arrival location.CREATE VIEWstatement defines the viewMyViewsTableto store this result.SELECT * FROM MyViewsTable;
Vishal JoshiRank #377 · 6.7K reputationPosted Sep 25, 2024, 1:26 AM EDT
Hello Debashish,
You need to use INNER JOIN To get the expected data. to create view you can use the below SQL.
CREATE VIEW [dbo].[vw_Employee_Travel] AS SELECT t.EID, e.[Name], l_departure.[Location], l_arrival.[Location] FROM tblTour AS t INNER JOIN tblEmployee AS e ON t.EID = e.EID INNER JOIN tblLocation AS l_arrival ON t.ArrivalID = l_arrival.LID INNER JOIN tblLocation AS l_departure ON t.DepartureID = l_departure.LID GOThanks
Vishal Joshi