union

union takes two or more tables and returns all their rows in one result, one table under the other. Use it when the same kind of activity is logged in different places, for example failed logons in both cloud sign-in logs and Windows security events.

union SigninLogs, AuditLogs
| where TimeGenerated > ago(1d)

It can start a query (union A, B) or appear after a pipe (A | union B), where it adds the rows of B to the rows so far. Each side can be a table name or a query in parentheses.

Columns

The result has every column of every table. A row from a table that lacks a column gets an empty value there: an empty string for text, null for numbers and dates. Columns that share a name (like TimeGenerated) are combined into one. If they have different types, this site refuses the query rather than guess; rename one side first.

To make the result tidy, project each side to the same few columns before the union.

withsource

union withsource = SourceTable A, B adds a column called SourceTable that says which table each row came from. Very handy when you then summarise.

union or join? union puts rows under each other. join puts matching rows side by side.

Examples

1 Events per day across two tables
union SigninLogs, AuditLogs
| summarize Events = count() by bin(TimeGenerated, 1d)
| sort by TimeGenerated asc
2 Which table did each row come from?
union withsource = SourceTable DeviceProcessEvents, DeviceNetworkEvents
| where DeviceName == "ws-prg-011.kestrelbay.example"
| summarize Events = count() by SourceTable
3 union in the middle of a query
SigninLogs
| where ResultType != "0"
| project TimeGenerated, Account = UserPrincipalName, Source = "cloud"
| union (
    SecurityEvent
    | where EventID == 4625
    | project TimeGenerated, Account = TargetUserName, Source = "windows"
  )
| summarize Failures = count() by Source

Giving both sides the same column names first makes the combined result easy to read.

Practice it

None of the free exercises needs this one yet.