KQL vs SQL

If you know SQL, you know most of what Kusto Query Language (KQL) does. The differences are the order you write things in and a handful of defaults that work the other way round.

Reading order

SQL is written in a fixed clause order (SELECT … FROM … WHERE … GROUP BY …) but runs in a different one. KQL is a pipeline that runs top to bottom: a table, then each line after a |changes the previous result. Stop after any line to see the intermediate result.

SQL
SELECT RemotePort, COUNT(*) AS Connections
FROM DeviceNetworkEvents
WHERE Protocol <> 'Udp'
GROUP BY RemotePort
HAVING COUNT(*) > 10
ORDER BY Connections DESC;
KQL
DeviceNetworkEvents
| where Protocol != "Udp"
| summarize Connections = count() by RemotePort
| where Connections > 10
| sort by Connections desc

There is no HAVING: a where after summarize does the same job. There is no separate SELECT either: project picks columns wherever you put it.

The same tasks side by side

TaskSQLKQL
Pick a tableSELECT * FROM SigninLogsSigninLogs
Filter rowsWHERE ResultType <> '0'| where ResultType != "0"
Choose columnsSELECT UserPrincipalName AS User, IPAddress| project User = UserPrincipalName, IPAddress
Add a computed columnSELECT *, lower(Location) AS Loc| extend Loc = tolower(Location)
Group and countSELECT IPAddress, COUNT(*) ... GROUP BY IPAddress| summarize count() by IPAddress
Count distinctCOUNT(DISTINCT UserPrincipalName)dcount(UserPrincipalName)
Filter after groupingHAVING COUNT(*) > 10| where count_ > 10
SortORDER BY Failures DESC| sort by Failures desc
First N rows by a columnORDER BY Failures DESC LIMIT 5| top 5 by Failures
Unique valuesSELECT DISTINCT Location| distinct Location
Value in a listLocation IN ('DE', 'AT')Location in ("DE", "AT")
Substring searchName LIKE '%admin%'Name contains "admin"
Starts withName LIKE 'svc%'Name startswith "svc"
Missing valueUrl IS NULL OR Url = ''isempty(Url)
If / elseCASE WHEN x > 5 THEN 'high' ELSE 'low' ENDiff(x > 5, "high", "low")
Inner joinJOIN B ON A.Key = B.Key| join kind=inner (B) on Key
Stack two tablesSELECT ... UNION ALL SELECT ...union A, B
Last 24 hoursTimeGenerated > now() - INTERVAL '1 day'| where TimeGenerated > ago(1d)
Group per hourdate_trunc('hour', TimeGenerated)bin(TimeGenerated, 1h)
Name a sub-queryWITH failures AS (SELECT ...)let failures = SigninLogs | where ...;

Seven differences that catch SQL users

  1. Sorting is descending by default. sort by TimeGenerated puts the newest row first. Write asc for oldest first. More
  2. == is case-sensitive. "Admin" == "admin" is false. Use=~ to ignore case. More
  3. has is not LIKE. contains is the substring search;has matches whole terms only (and is faster). More
  4. The default join is not an inner join. Without kind=, KQL usesinnerunique, which keeps only one left row per key. Always write the kind.More
  5. Strings are never null. A missing string is "". Use isempty(), notisnull(), on text. More
  6. Aggregates get automatic names. count() becomes a column calledcount_, avg(X) becomes avg_X. Name them yourself to avoid surprises.More
  7. Strings use double or single quotes, and identifiers are case-sensitive."text" and 'text' are both strings; ipaddress is not the columnIPAddress.

Examples

1 The SQL query above, in KQL
DeviceNetworkEvents
| where Protocol != "Udp"
| summarize Connections = count() by RemotePort
| where Connections > 10
| sort by Connections desc
2 Case matters with ==, not with =~
DeviceProcessEvents
| summarize WithEquals = countif(FileName == "winword.exe"), WithTilde = countif(FileName =~ "winword.exe")

The file is logged as WINWORD.EXE.

Next: the KQL cheat sheet, or the full reference.