Forum Discussion

frithjof_v's avatar
frithjof_v
Icon for Community Champion rankCommunity Champion
1 year ago
Solved

Default joins work differently in KQL vs. SQL

When doing a default join in KQL (without specifying join kind), it seems that the left or right position of the tables affect the number of rows returned by the query.   This is not the case with ...
  • frithjof_v's avatar
    1 year ago

    It works similar like SQL's default join type (which is inner join) if I explicitly add kind=inner to the join in KQL.

     

     

    let varStockMarketLast10 = 
    StockMarketRaw
    | project ['time'], symbol, bidPrice, bidSize, askPrice, askSize, volume
    | order by ['time']
    | take 10;
    
    varStockMarketLast10;
    
    let varDimStockMarket = 
    StockMarketRaw
    | distinct symbol, sector, securityType;
    
    varDimStockMarket;
    
    varStockMarketLast10
    | join kind=inner varDimStockMarket on symbol;
    
    varDimStockMarket
    | join kind=inner varStockMarketLast10 on symbol;

     

     

    So the default join kind in KQL is not an inner join, whereas in SQL, the default join kind is inner join.

     

    The default join kind in KQL is:

    • innerunique (default)

    Inner join with left side deduplication
    Schema: All columns from both tables, including the matching keys
    Rows: All deduplicated rows from the left table that match rows from the right table

     

    https://learn.microsoft.com/en-us/training/modules/multi-table-queries-with-kusto-query-language/2-multi-table-queries

     

    To be honest, I find the results provided by KQL's default join kind confusing. What is the practical use case for those results? (Ref. step 3 in the original post).

     

    What is the benefit of KQL's default join kind (innerunique)?

     

    I think I will always need to specify join kind in KQL. (Or, be careful about which table is on the right side or left side of the join, when using the default join.)

     

    In SQL, by contrast, it can many times make sense to use the default join kind (inner join), and the left/right position won't affect the number of rows returned.
    Still, it is always a good practice to specify join kind explicitly also in SQL, for readability.

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi  frithjof_v

     

    I agree with your insights.

    Quote from Microsoft:

     

    By default, the innerunique join flavor is used if the kind parameter isn't specified.

     

    This default implementation is useful in log/trace analysis scenarios, where you aim to correlate two events based on a shared correlation ID.

     

    It allows you to retrieve all instances of the phenomenon while disregarding duplicate trace records that contribute to the correlation.


    More information for your reference:

    innerunique join - Kusto | Microsoft Learn

     

    Best regards,

    Joyce

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.