Home › Guides › Reference › KQL Cheat Sheet

KQL Cheat Sheet

Kusto Query Language operators, string matching, joins and sample Log Analytics queries, with the has versus contains rules.

By the tools4free.dev team · Last reviewed 4 December 2026

How to use this cheat sheet

Kusto Query Language (KQL) is the read-only query language behind Azure Monitor Logs, Log Analytics, Microsoft Sentinel and Microsoft Defender advanced hunting. A KQL query is a pipeline. You start with a table name, then pass the rows through operators separated by the pipe character |. Each operator takes the table produced by the one before it and returns a new table. That is the whole mental model: filter early, shape the columns, aggregate, then sort or chart.

Read the tables below by task. The first section lists the operators you will type in almost every query. The later sections cover string matching, time, joins, parsing and charts. The sample queries use four common Log Analytics tables: SigninLogs (Microsoft Entra sign-ins), SecurityEvent (Windows security events), Heartbeat (agent check-ins) and AzureActivity (Azure control-plane operations). Which tables exist in your workspace depends on the data connectors and diagnostic settings you have turned on, so an empty or missing table usually means the data is not being collected yet.

KQL appears in two exams on this site. The SC-200 study hub relies on it for Microsoft Sentinel and advanced hunting, and the AZ-104 monitoring and backup hub covers Azure Monitor and Log Analytics queries. To run any example, open a Log Analytics workspace and choose Logs, or open the Logs page in Microsoft Sentinel.

The operators you will use most

where to filter, project to choose columns, extend to add columns, summarize to aggregate, sort by and top to order, and take to peek at a few rows. Always put a time filter such as where TimeGenerated > ago(1d) first.

Core tabular operators

OperatorWhat it doesExample
where (alias filter)Keeps the rows for which a predicate is true.| where ResultType != 0
projectSelects, renames and computes columns. Only the columns you list stay, in the order you list them.| project TimeGenerated, Computer
extendAdds calculated columns and keeps all existing ones.| extend Hour = bin(TimeGenerated, 1h)
summarizeGroups rows with by and computes aggregates for each group.| summarize Failures = count() by UserPrincipalName
countReturns a single row with the number of rows in the input.Heartbeat | count
distinctReturns the distinct combinations of the columns you name.| distinct Computer, OSType
topReturns the first N rows sorted by an expression, highest first by default.| top 10 by Failures
sort byOrders the rows. top 5 by name is equivalent to sort by name | take 5.| sort by TimeGenerated desc
takeReturns some rows, with no guarantee about which.| take 10
letNames a value, a table expression or a function. Each let statement ends with a semicolon.let window = 7d;
renderAsks the portal to draw the result as a chart. It must be the last operator.| render timechart

Two details save time. project drops every column you do not name, while extend keeps them all, so use extend when you want to keep working with the full row. And the plain count operator is not the same as the count() aggregation: the operator returns the row count of the whole table, while count() is used inside summarize, usually with by.

Aggregation functions for summarize

FunctionReturns
count(), countif(predicate)Number of rows, or number of rows where the predicate is true.
dcount(column)Estimated number of distinct values.
sum(), avg(), min(), max()The usual numeric aggregates.
percentile(column, p)The value at the given percentile.
make_list(), make_set()A dynamic array of values. make_set removes duplicates.
arg_max(by, *), arg_min(by, *)The whole row with the largest or smallest value of by.
take_any()An arbitrary value from the group.

When the input is empty, summarize behaves differently with and without a by clause. With no group expression it still returns one row, where counts and sums are 0 and other aggregates are null. With a group expression it returns no rows. Aggregates such as avg() ignore null values.

Working with time

FunctionWhat it doesExample
ago(timespan)The current UTC time minus a timespan. Several uses in one query refer to the same instant.where TimeGenerated > ago(24h)
now()The current UTC time.extend Age = now() - TimeGenerated
bin(value, size)Rounds a value down to a multiple of the bin size. Use it with summarize to group times into buckets.summarize count() by bin(TimeGenerated, 1h)
between (a .. b)Keeps values in a range.where TimeGenerated between (ago(2d) .. ago(1d))

Timespan literals use a number and a unit, such as 30m, 1h, 7d. bin() works on numbers, timespans and datetimes: bin(4.5, 1) returns 4, and bin(datetime(1970-05-11 13:45:07), 1d) returns the start of that day. Modern versions no longer bin datetime columns automatically, so write bin(TimeGenerated, 1h) explicitly.

String operators, has and contains, and case

Choosing the right string operator is the most common KQL performance question. Kusto indexes string columns by terms: runs of alphanumeric characters that are at least three characters long. The operators with has in their name match whole terms and use that index. The contains family matches any substring and has to scan the values, which is slower.

OperatorMeaningCase-sensitive
==, !=Equals, not equals.Yes
=~, !~Equals, not equals.No
has, !hasThe right side is a whole term in the left side.No
has_cs, !has_csAs has.Yes
has_any, has_allAs has, against a list of values. Any one, or all of them.No
hasprefix, hassuffixThe right side is a term prefix or suffix.No
contains, !containsThe right side occurs anywhere inside the left side.No
contains_csAs contains.Yes
startswith, endswithThe left side begins or ends with the right side.No (add _cs for yes)
in, !inEquals any element of a list.Yes
in~, !in~Equals any element of a list.No
matches regexThe left side contains a match for a regular expression.Yes

Microsoft's own example shows the difference: "North America" has "america" is true, but "North America" has "amer" is false, because amer is only part of a term. "KustoExplorerQueryRun" contains "Explorer" is true while the same test with has is false. Prefer the case-sensitive form of an operator when the case is known, because it is faster: use == rather than =~, and in rather than in~. A term shorter than three characters is not indexed, so the query falls back to scanning.

Join kinds

The syntax is LeftTable | join kind=<flavour> (RightTable) on Key. If the key has a different name on each side, write on $left.A == $right.B. For best performance, put the smaller table on the left. If you leave out kind, the default is innerunique, which removes duplicate rows from the left side before matching. That surprises many people, so say kind=inner when you want a standard inner join.

KindRows returnedColumns returned
innerunique (default)Deduplicated left rows that match a right row.Both tables.
innerOnly matching rows from both tables.Both tables.
leftouterAll left rows, plus matching right rows.Both tables.
rightouterAll right rows, plus matching left rows.Both tables.
fullouterAll rows from both tables. Unmatched cells are null.Both tables.
leftsemiLeft rows that have a match on the right.Left table only.
leftanti (also anti, leftantisemi)Left rows that have no match on the right.Left table only.
rightsemiRight rows that have a match on the left.Right table only.
rightanti (also rightantisemi)Right rows that have no match on the left.Right table only.

KQL has no cross-join kind. You can imitate one by adding a placeholder column with the same value to both sides and joining on it.

Parsing, arrays and variables

OperatorUseExample
parseSplits a string column into new columns by matching the text around them. Kinds are simple (default, strict), regex and relaxed. Unmatched rows get null.| parse Properties with * "user=" User ";" *
parse-whereLike parse, but drops rows that do not match.| parse-where Text with "id=" Id:long
mv-expandTurns each element of an array or property bag into its own row. Other columns are repeated. An empty array produces no row.| mv-expand Items
letBinds a name to a calculation. It is re-evaluated on each use, so wrap it in toscalar() or materialize() when you need one fixed result.let threshold = 5;
unionCombines the rows of several tables.union SigninLogs, AzureActivity
datatable, range, printCreate small inline tables, sequences and single values for testing.print now()

Two mv-expand columns in one operator are zipped together, row by row. To get every combination, expand one column after the other. Add to typeof(int) to give the expanded column a type, because the default is dynamic.

Render options

End a query with render to chart it. In Azure Monitor and Microsoft Sentinel the supported visualisations are areachart, barchart, columnchart, piechart, scatterchart, table (the default) and timechart. For timechart the first column is the x axis and should be a datetime, the other numeric columns are the y values, and one string column splits the data into separate lines. Other Kusto hosts support more kinds, so a chart name that works in Azure Data Explorer may not work in Log Analytics.

Sample queries on Log Analytics tables

Failed sign-ins by user (SigninLogs)

In SigninLogs, a ResultType of 0 means success and any other value is a failure.

SigninLogs
| where TimeGenerated > ago(7d)
| where ResultType != 0
| summarize Failures = count(), Sources = dcount(IPAddress) by UserPrincipalName
| top 10 by Failures

Failed Windows logons per hour (SecurityEvent)

Event ID 4625 is the Windows event "An account failed to log on".

SecurityEvent
| where TimeGenerated > ago(1d)
| where EventID == 4625
| summarize Failed = count() by bin(TimeGenerated, 1h), Computer
| render timechart

Computers that stopped reporting (Heartbeat)

Take the latest heartbeat for each computer and keep those older than 15 minutes.

Heartbeat
| summarize arg_max(TimeGenerated, *) by Computer
| where TimeGenerated < ago(15m)
| project Computer, OSType, LastSeen = TimeGenerated

Who changed what in Azure (AzureActivity)

AzureActivity
| where TimeGenerated > ago(7d)
| where ActivityStatusValue == "Succeeded"
| where OperationNameValue has "delete"
| summarize Deletes = count() by Caller, OperationNameValue
| sort by Deletes desc

Combining tables with let and join

Find every sign-in by users who failed more than 20 times in the last day. The let statement holds the list of those users, and the join keeps only their rows.

let suspects = SigninLogs
| where TimeGenerated > ago(1d) and ResultType != 0
| summarize Failures = count() by UserPrincipalName
| where Failures > 20;
SigninLogs
| where TimeGenerated > ago(1d)
| join kind=inner (suspects) on UserPrincipalName
| project TimeGenerated, UserPrincipalName, IPAddress, AppDisplayName, Failures

Common mistakes

For other commands you may need around Azure, see Azure CLI versus Azure PowerShell. Regular expressions used with matches regex and parse kind=regex can be tested in the regex cheat sheet.

Sources