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.
SELECT RemotePort, COUNT(*) AS Connections
FROM DeviceNetworkEvents
WHERE Protocol <> 'Udp'
GROUP BY RemotePort
HAVING COUNT(*) > 10
ORDER BY Connections DESC;DeviceNetworkEvents
| where Protocol != "Udp"
| summarize Connections = count() by RemotePort
| where Connections > 10
| sort by Connections descThere 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
| Task | SQL | KQL |
|---|---|---|
| Pick a table | SELECT * FROM SigninLogs | SigninLogs |
| Filter rows | WHERE ResultType <> '0' | | where ResultType != "0" |
| Choose columns | SELECT UserPrincipalName AS User, IPAddress | | project User = UserPrincipalName, IPAddress |
| Add a computed column | SELECT *, lower(Location) AS Loc | | extend Loc = tolower(Location) |
| Group and count | SELECT IPAddress, COUNT(*) ... GROUP BY IPAddress | | summarize count() by IPAddress |
| Count distinct | COUNT(DISTINCT UserPrincipalName) | dcount(UserPrincipalName) |
| Filter after grouping | HAVING COUNT(*) > 10 | | where count_ > 10 |
| Sort | ORDER BY Failures DESC | | sort by Failures desc |
| First N rows by a column | ORDER BY Failures DESC LIMIT 5 | | top 5 by Failures |
| Unique values | SELECT DISTINCT Location | | distinct Location |
| Value in a list | Location IN ('DE', 'AT') | Location in ("DE", "AT") |
| Substring search | Name LIKE '%admin%' | Name contains "admin" |
| Starts with | Name LIKE 'svc%' | Name startswith "svc" |
| Missing value | Url IS NULL OR Url = '' | isempty(Url) |
| If / else | CASE WHEN x > 5 THEN 'high' ELSE 'low' END | iff(x > 5, "high", "low") |
| Inner join | JOIN B ON A.Key = B.Key | | join kind=inner (B) on Key |
| Stack two tables | SELECT ... UNION ALL SELECT ... | union A, B |
| Last 24 hours | TimeGenerated > now() - INTERVAL '1 day' | | where TimeGenerated > ago(1d) |
| Group per hour | date_trunc('hour', TimeGenerated) | bin(TimeGenerated, 1h) |
| Name a sub-query | WITH failures AS (SELECT ...) | let failures = SigninLogs | where ...; |
Seven differences that catch SQL users
- Sorting is descending by default.
sort by TimeGeneratedputs the newest row first. Writeascfor oldest first. More ==is case-sensitive."Admin" == "admin"is false. Use=~to ignore case. Morehasis notLIKE.containsis the substring search;hasmatches whole terms only (and is faster). More- 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 - Strings are never null. A missing string is
"". Useisempty(), notisnull(), on text. More - Aggregates get automatic names.
count()becomes a column calledcount_,avg(X)becomesavg_X. Name them yourself to avoid surprises.More - Strings use double or single quotes, and identifiers are case-sensitive.
"text"and'text'are both strings;ipaddressis not the columnIPAddress.
Examples
DeviceNetworkEvents
| where Protocol != "Udp"
| summarize Connections = count() by RemotePort
| where Connections > 10
| sort by Connections descDeviceProcessEvents
| 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.