Skip to content

KQL

noun · also Kusto Query Language · Microsoft Learn: KQL best practices

The read-only query language used by Microsoft Sentinel, Defender XDR advanced hunting, Azure Monitor and Azure Data Explorer.

Kusto Query Language (KQL) is how you ask questions of log data across Microsoft's security and monitoring tools. A query starts with a table and passes the data through a pipeline of operators separated by |: filter with where, choose columns with project, aggregate with summarize, and so on. It reads top to bottom, much like a sentence, and it only reads data: a query cannot change or delete the logs it runs on.

The same language works in Microsoft Sentinel, in advanced hunting in the Microsoft Defender portal, in Azure Monitor Log Analytics and in Azure Data Explorer. Only the tables change: SigninLogs and AuditLogs come from Entra ID, DeviceProcessEvents from Defender for Endpoint, EmailEvents from Defender for Office 365.

KQL and SQL

If you know SQL, KQL will feel familiar but backwards. SQL starts with what you want (SELECT ... FROM ... WHERE); KQL starts with where the data is and narrows it step by step. Each pipe takes the result of the previous line, so you can build a query one line at a time, run it, look at the output and add the next step. That habit is exactly how investigations work: start broad, look, then filter.

The operators you use every day

  • where filters rows. Put the time filter first, then the most selective conditions.
  • project keeps only the columns you need, and extend adds calculated ones.
  • summarize counts and groups, for example count() by UserPrincipalName. With bin(TimeGenerated, 1h) it builds a timeline.
  • join and union combine tables, such as a sign-in followed by a mailbox action.
  • let names a value or a subquery so you can reuse it, which keeps long hunts readable.
  • top, sort by and take limit and order what you look at.

Example

top-failed-signins.kql
SigninLogs
| where TimeGenerated > ago(1d)
| where ResultType != "0"
| summarize Failures = count(), IPs = dcount(IPAddress) by UserPrincipalName
| top 10 by Failures

This returns the ten accounts with the most failed sign-ins in the last day, along with how many different IP addresses tried. Many failures from many IPs across many accounts looks like a password spray; many failures on one account from one IP looks more like a user who forgot a password.

A second example from advanced hunting, looking for PowerShell started by an Office application:

office-spawns-powershell.kql
DeviceProcessEvents
| where Timestamp > ago(7d)
| where InitiatingProcessFileName in~ ("winword.exe", "excel.exe", "outlook.exe")
| where FileName in~ ("powershell.exe", "pwsh.exe")
| project Timestamp, DeviceName, AccountName, InitiatingProcessFileName, ProcessCommandLine

Note the different time column: Sentinel and Log Analytics tables use TimeGenerated, while the native Defender tables use Timestamp.

Writing queries that run fast

Microsoft's own guidance comes down to processing less data:

  • Filter on time first. Time columns are indexed, so a where on time right after the table name skips most of the data.
  • Prefer has over contains. has matches whole terms using the index; contains scans for substrings and is much slower on large tables.
  • Prefer case-sensitive operators such as == and in when you know the exact value.
  • Put the smaller table first in a join.
  • Test with take 10 or count before running an unbounded query over a large table.

Advanced hunting also has limits worth knowing: native Defender data goes back 30 days, a query returns at most 100,000 rows and runs for at most 10 minutes. All times are in UTC.

Common mistakes

  • Forgetting the time filter and hitting a timeout.
  • Treating "no results" as "nothing happened" when the data source is not connected or the column name is wrong.
  • Comparing ResultType to the number 0 instead of the string "0" in SigninLogs.

Why it matters for a SOC analyst

In a Microsoft environment, almost every investigation, hunt and detection rule is written in KQL. You do not need to master the whole language to be effective: where, project, summarize, extend, join and time filters cover most daily work. Keep a personal library of queries that worked, and reuse them. The PowerShell hunting tutorial and the suspicious sign-in investigation are good next steps.

Articles that use this term

  1. Certifications · · 3 min read

    SC-200 study plan for SOC beginners

    A six-week plan to prepare for Microsoft's Security Operations Analyst exam: what to study each week, how to practice for free, and the KQL you need to know.

  • SIEM

    A platform that collects logs from across an organization, correlates them and raises alerts, giving the SOC one place to investigate.

  • EDR

    Security software on laptops and servers that records process, file and network activity, detects malicious behavior and lets analysts respond.

  • Threat hunting

    The proactive search for attackers who are already inside the environment but have not triggered any alert, usually starting from a hypothesis.