Stage 01 · Basics — Learn to thinkin KQL
Audience: complete beginners and SOC Tier 1 analysts. Goal: read and write KQL as a sequence of transformations — not memorise snippets. Prerequisite: finish Stage 00 · Setup so
DemoIdentityLogsexists. Every query here runs against it, so you'll see real results immediately.
1. What KQL is
Kusto Query Language (KQL) is a read-only language for exploring large volumes of log/telemetry data. It powers Microsoft Sentinel, Azure Monitor / Log Analytics, Microsoft Defender, and Azure Data Explorer — learn it once, use it everywhere. Reference: KQL overview
2. The mental model: a pipeline
A KQL query starts with a table and flows top-to-bottom through | (pipe) operators. Each operator takes the rows from the line above and transforms them.
TableName ← start with a data source
| where ... ← filter rows (cut noise early)
| extend ... ← add computed columns
| summarize ... ← aggregate / group
| sort by ... ← order
| project ... ← choose columns to display
Read this query like a sentence — "take DemoIdentityLogs, keep only failed sign-ins, count them per user, show the worst first":
DemoIdentityLogs
| where EventType == "Signin"
| where ResultType != 0
| summarize FailedAttempts = count() by UserPrincipalName
| sort by FailedAttempts desc
Golden rule: filter early. The sooner you cut rows with
where, the faster and cheaper the query.
3. Always start with take
Before writing logic, look at the shape of the data. take grabs a few arbitrary rows so you can see the columns and values safely:
DemoIdentityLogs
| take 10
4. Filtering with where
where keeps only rows that match a condition.
// Only sign-in events
DemoIdentityLogs
| where EventType == "Signin"
// Only FAILED sign-ins (ResultType is an int in the demo set; 0 = success)
DemoIdentityLogs
| where EventType == "Signin"
| where ResultType != 0
Common operators inside where:
| Operator | Meaning | Example |
|---|---|---|
==, != |
exact equality (case-sensitive) | EventType == "Audit" |
=~, !~ |
equality, case-insensitive | Location =~ "germany" |
in, !in |
match any value in a list | ResultType in (50126, 50053) |
contains, has |
substring / whole-token match | ResultDescription has "password" |
>, <, >= |
comparisons (numbers, dates) | TimeGenerated > ago(1d) |
hasis faster thancontains— it matches whole tokens and uses the index. Preferhaswhen you can. Reference: String operators
5. Time filtering & query hygiene
Every real investigation starts with time. ago() is a relative time expression.
DemoIdentityLogs
| where TimeGenerated > ago(7d)
⚠️ Forgetting a time filter is the #1 cause of slow, expensive queries on real data. Make it a reflex. Reference: ago() function
6. Choosing columns with project
project selects (and orders, and renames) the columns you want to see. It makes results readable.
DemoIdentityLogs
| where EventType == "Signin"
| where ResultType != 0
| project TimeGenerated, UserPrincipalName, IPAddress, AppDisplayName, ResultDescription
Related: project-away removes columns; project-rename renames them.
7. Adding logic with extend
extend creates a new computed column from existing data.
DemoIdentityLogs
| where EventType == "Signin"
| extend Outcome = iff(ResultType == 0, "Success", "Failure")
| project TimeGenerated, UserPrincipalName, Outcome, ResultDescription
iff(condition, valueIfTrue, valueIfFalse) is the inline if. For many branches, use case().
Reference: iff() · case()
8. Reusable values with let
let names a value or a sub-result so you don't repeat yourself and the intent is clear.
let lookback = 7d;
let failureCodes = dynamic([50126, 50053, 50057, 50055]);
DemoIdentityLogs
| where EventType == "Signin"
| where TimeGenerated > ago(lookback)
| where ResultType in (failureCodes)
| project TimeGenerated, UserPrincipalName, ResultType, ResultDescription, IPAddress, Location
| sort by TimeGenerated desc
Reference: let statement
9. Free-text search with search
When you don't know which column holds a value, search scans them all. Great for exploration, slower than a targeted where — switch to where once you know the column.
DemoIdentityLogs
| search "Failure"
Core operators to memorise
| Operator | Why it matters |
|---|---|
where |
Reduce noise early |
project / project-away |
Control which columns show |
extend |
Add calculated columns |
summarize |
Aggregate (next stage) |
sort by (order by) |
Prioritise results |
take / limit |
Sample data safely |
search |
Explore when you don't know the column |
let |
Reuse values, self-document |
Exercises
Try these against DemoIdentityLogs before peeking at the answers.
- Show only the audit events, newest first, displaying time, user, operation, and result.
- List every failed sign-in from a
mediumorhighrisk level. - For sign-ins, add a column
Countryequal toLocation, and show only sign-ins not from the United States. - Find any record (sign-in or audit) that mentions the word
password.
🎯 Check yourself (against the seeded
DemoIdentityLogs): 1 → 11 audit rows, newest isitadmin"Add member to group". 2 → 16 rows, onlyalexw(Netherlands) andsophial(Germany) —jamest'slow-risk failure is correctly excluded. 3 → 39 rows across four locations:alexw→Netherlands,meganb→UK,sophial→Germany,itadmin→India. 4 → 15 rows — more than you'd expect, becausesearch "password"also matches "Invalid username or password", not just the obvious resets. A good reminder thatsearchcasts a wide net.
Answers
// 1
DemoIdentityLogs
| where EventType == "Audit"
| project TimeGenerated, UserPrincipalName, OperationName, AuditResult
| sort by TimeGenerated desc
// 2
DemoIdentityLogs
| where EventType == "Signin"
| where ResultType != 0
| where RiskLevelDuringSignIn in ("medium", "high")
| project TimeGenerated, UserPrincipalName, IPAddress, RiskLevelDuringSignIn, ResultDescription
// 3
DemoIdentityLogs
| where EventType == "Signin"
| extend Country = Location
| where Country != "United States"
| project TimeGenerated, UserPrincipalName, Country, AppDisplayName
// 4
DemoIdentityLogs
| search "password"
Common beginner mistakes
- Filtering before you understand the columns → always
take 10first. - Forgetting
TimeGeneratedfilters → slow queries on real data. - Using
==when the realSigninLogs.ResultTypeneeds"0"(string) → see schema-gotchas. - Copy-pasting queries without reading them line by line.
You'll know you've completed Stage 01 when you can
- read a piped query top-to-bottom and say what each line does to the rows;
- filter with
where(==vs=~,hasvscontains) and always time-bound first; - choose columns with
projectand add one withextend+iff; - explain why the demo's
ResultType != 0becomes!= "0"on the realSigninLogs.
Next: Stage 02 · Aggregation & Visualization — turn rows into insight with summarize, bin(), and render.
Reference hub: Learn common KQL operators
Mission 01 challenge
In SigninLogs, show the 10 most recent failed sign-ins, with the user, the IP address and the app.
Write it in the box below and check it. Then compare with one answer; others can be right too.
Show one answer
SigninLogs
| where ResultType != "0"
| project TimeGenerated, UserPrincipalName, IPAddress, AppDisplayName
| sort by TimeGenerated desc
| take 10Check your own query
Write a variation of this lesson's queries, on the practice table DemoIdentityLogs or a real table, and check it against the real schema.
Course by Kaushik Saha. Source: sentinel/kql/01-basics/README.md at ea5adab001fa. Queries checked with Microsoft.Azure.Kusto.Language 12.4.1 against schema bundle 2026.10.29.