Querying logs with KQL
Read and write Kusto queries step by step, then practise on sample VM logs in a live playground.
In this lesson you'll learn
- How a KQL query flows through pipes
- Filtering with where and time with ago()
- Shaping results with project, and counting with summarize
- Sorting, limiting and charting results
1. How KQL reads
New to KQL? Start here
KQL (Kusto Query Language) is how you search logs in Azure. A query starts with a table name, then passes rows through operators joined by the pipe symbol |. Each step takes the rows from the step before, like water through a series of filters.
2. The operators you need
| Operator | Does | Example |
|---|---|---|
| where | Keeps rows that match | where EventLevelName == "Error" |
| ago() | A time relative to now | where TimeGenerated > ago(1h) |
| project | Chooses columns | project Computer, EventID |
| summarize | Groups and counts or averages | summarize count() by Computer |
| sort by | Orders rows | sort by TimeGenerated desc |
| take | Any N rows, no order | take 10 |
| top | First N rows by a column | top 5 by CounterValue desc |
| search | Full-text search | search in (Event) "error" |
| render | Draws a chart | render timechart |
Comparisons: == (exact, case sensitive), =~ (case insensitive), !=, contains, has (whole word, faster), and > < for numbers and times. Combine conditions with and and or.
3. Try it: KQL playground
Two sample tables are loaded: Event (Windows events from three VMs) and Perf (CPU and memory counters). Pick an example or write your own query.
Try it in the app. This lesson has a KQL practice box here. Open the lesson in the exam app
4. Read a query like the exam does
Example: average CPU per computer over the last hour
Perf | where TimeGenerated > ago(1h) | where CounterName == "% Processor Time" | summarize avg(CounterValue) by Computer | sort by avg_CounterValue desc
- Start with the Perf table.
- Keep only rows from the last hour.
- Keep only the CPU counter.
- Group by computer and average the values. The new column is named avg_CounterValue automatically.
- Busiest computer first.
Exam focus
Spot wrong answers by syntax. SELECT and FROM are SQL, not KQL. Verb-Noun commands like Get-Event are PowerShell. A KQL answer starts with a table name or with search.
Exam traps to remember
- take returns random rows. Use top or sort when order matters.
- summarize count() creates a column named count_.
- == is case sensitive. Use =~ to ignore case.
Test yourself: 3 check questions and exam-style practice questions on this topic are in the AZ-104 Azure Administrator app.
Practise in the app