join
join combines each row of the left table with the matching rows of the right table. The right table
goes in parentheses and can be a whole query:
DeviceProcessEvents
| where FileName == "mstsc.exe"
| join kind=inner (DeviceNetworkEvents | where RemotePort == 3389) on DeviceName
The default kind is not inner
Without kind=, KQL uses innerunique: it first removes duplicate keys from the left side
(keeping one row per key, and which one is not defined), then does an inner join. That surprises people
who expect SQL’s inner join, because rows quietly disappear. Always write the kind you mean.
| Kind | Returns |
|---|---|
inner |
every matching pair of rows |
innerunique (default) |
like inner, after keeping one left row per key |
leftouter |
all left rows; right columns empty where nothing matches |
leftanti |
left rows with no match (left columns only) |
leftsemi |
left rows that have a match (left columns only) |
rightouter, fullouter, rightanti, rightsemi |
the same ideas from the other side |
Keys
on Key joins on a column with the same name on both sides. When the names differ, write
on $left.IPAddress == $right.IpAddress. Several keys are separated by commas.
Columns that exist on both sides appear twice; the right side’s copy gets a number appended
(TimeGenerated1). Remove or rename it with project.
Things to know
- Join keys compare with exact case. Normalise with
tolower()first if the two tables write names differently. - Summarise before you join when you can. Joining two large raw tables multiplies rows quickly.
- To stack tables on top of each other rather than side by side, use
union.
Examples
SecurityEvent
| where EventID == 4672
| summarize PrivilegedLogons = count() by TargetUserName
| join kind=inner (
SecurityEvent
| where EventID == 4624
| summarize AllLogons = count() by TargetUserName
) on TargetUserNameThe right side's copy of the key comes back as TargetUserName1. Drop it with project-away.
DeviceProcessEvents
| where FileName == "cmd.exe" or FileName == "mstsc.exe"
| summarize Runs = count() by DeviceName
| join kind=leftouter (
DeviceNetworkEvents
| where RemotePort == 445
| summarize SmbConnections = count() by DeviceName
) on DeviceName
| project DeviceName, Runs, SmbConnectionsSigninLogs
| where Location == "BR"
| distinct UserPrincipalName
| join kind=leftanti (
SigninLogs
| where Location == "CZ"
| distinct UserPrincipalName
) on UserPrincipalNameUsers who signed in from Brazil but never from the Czech Republic. leftanti returns only left columns.
Practice it
None of the free exercises needs this one yet.
+ 7 in the full SC-200 set · all exercises