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
wherefilters rows. Put the time filter first, then the most selective conditions.projectkeeps only the columns you need, andextendadds calculated ones.summarizecounts and groups, for examplecount() by UserPrincipalName. Withbin(TimeGenerated, 1h)it builds a timeline.joinandunioncombine tables, such as a sign-in followed by a mailbox action.letnames a value or a subquery so you can reuse it, which keeps long hunts readable.top,sort byandtakelimit and order what you look at.
Example
SigninLogs
| where TimeGenerated > ago(1d)
| where ResultType != "0"
| summarize Failures = count(), IPs = dcount(IPAddress) by UserPrincipalName
| top 10 by FailuresThis 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:
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, ProcessCommandLineNote 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
whereon time right after the table name skips most of the data. - Prefer
hasovercontains.hasmatches whole terms using the index;containsscans for substrings and is much slower on large tables. - Prefer case-sensitive operators such as
==andinwhen you know the exact value. - Put the smaller table first in a
join. - Test with
take 10orcountbefore 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
ResultTypeto the number0instead of the string"0"inSigninLogs.
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
Tutorials · · 6 min read
Your first Microsoft Sentinel analytics rule, step by step
Build a scheduled analytics rule in Microsoft Sentinel from a KQL query: schedule, threshold, entity mapping, incident grouping, testing and tuning.
Threat Analysis · · 6 min read
Device code phishing: how it works and how to detect it
How attackers abuse the OAuth device code flow to steal M365 tokens without a fake login page, and how to catch and block it with KQL and Conditional Access.
Tutorials · · 8 min read
Windows Security event IDs every SOC analyst should know
The Windows event IDs that matter in an investigation: logons, account and group changes, Kerberos, processes, services and log clearing, with KQL to find them.
Tutorials · · 5 min read
Conditional Access policies every Microsoft 365 tenant should have
A starter set of Conditional Access policies for Microsoft Entra ID, the order to roll them out safely, and the KQL to check their impact before you enforce.
Tutorials · · 5 min read
Triaging your first incident in Microsoft Defender XDR
A repeatable way to triage an incident in the Defender portal: pick it from the queue, check scope and evidence, decide what it is and close it properly.
Tutorials · · 3 min read
Investigating a malicious OAuth app consent in Entra ID
A user approved an unknown app with access to their mail. How to find the consent, judge the app, see what it accessed and remove it, with KQL for Sentinel.
Tutorials · · 4 min read
Hunting suspicious PowerShell with KQL in Defender XDR
Five advanced hunting queries that surface encoded commands, Office spawning PowerShell, download cradles and rare scripts, and how to triage each hit.
Tutorials · · 3 min read
Handling a reported phishing email in Microsoft 365, end to end
From the user's report to removing every copy: how to analyze a phishing email, find who received and clicked it, clean up mailboxes and close the loop.
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.
Tutorials · · 4 min read
Investigating a suspicious sign-in in Entra ID, step by step
What to check in the sign-in logs, which KQL queries to run, how to compare against the user's normal behavior, and when to revoke sessions and reset access.
Related terms
- 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.