What is the easiest way to join multiple tables in ssms (ms sql)?

warlock 101 Reputation points
2023-03-26T11:53:32.7066667+00:00
i have many tables and i must join it
the first:
select 
iek_actionid,
iek_productid
from iek_action_productid 

then this table join to 
select 
productid
from product
productid=iek_productid

then this table must be join with this table by  "iek_actionid"

select 
iek_actionid,
iek_account_id
from iek_action_partner_id

also by  "iek_actionid"  
must be joined to this one

Select 
iek_actionid,
iek_promotion_typeid
from iek_action

after by "iek_actionid"
  with tables

1
select 
iek_actionid,
accountid
from iek_action_customer

2
select 
iek_actionid,
accountid
from iek_action_customer

3 
select 
iek_actionid,
iek_target_client_group_id
from iek_action_targetclientgroup

I can join them individually, but that would be cumbersome. What is the easiest way to make an inner join of the specified tables. Thanks for any of your help.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,689 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 110.4K Reputation points MVP
    2023-03-26T12:33:13.38+00:00

    What is the best way to join them all depends on the result you want. Since all I know is some table and column names and I know nothing about the data and what the result you desire I cannot give you a query.

    But when you join tables, you use the JOIN operator. For instance:

    SELECT P.ProductName, P.ProductID, A.ActionID, ActionName
    FROM  Product P
    JOIN   ProductActions PA ON P.ProductID = PA.ProductID
    JOIN  Actions A ON A.ActionID = PA.ActionID
    

    If you want more detailed help for your scenario, I would recommend that you post the CREATE TABLE statements for your tables, INSERT statements with sample data, and the desired result given the sample.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. PercyTang-MSFT 12,501 Reputation points Microsoft Vendor
    2023-03-27T07:54:44.8966667+00:00

    Hi @warlock

    I think that to make a multi-table join, the first thing is to sort out the relationship between the tables join, and after doing this, creating join statements like the one shown by Erland is not complicated and cumbersome.

    According to your description of the join of the tables, I also add new tables one by one and select the join fields.

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Sedat SALMAN 13,745 Reputation points
    2023-03-27T08:00:41.5633333+00:00

    inner join is the answer but you can create your query by using ui like

    you can use SQL Server Management Studio (SSMS) or Azure Data Studio.

    1. Right-click on the database and choose "New Query" from the context menu. This will open a new query window.
    2. In the new query window, click on the "Design Query in Editor" button in the toolbar (it looks like a grid with a pencil).
    3. In the "Add Table" dialog, select the tables you want to join and click "Add" for each. When you're done, click "Close".
    4. In the "Query Designer" window, you'll see the selected tables with their columns. To create an inner join, click on the column you want to join from the first table, and then drag and drop it onto the related column in the second table. A line will appear between the two columns, indicating the join.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.