Interrogating Logs with Basic KQL Queries
This lesson teaches the basic KQL operators through examples.
Learning outcomes
After completing this lesson, you will be able to:
- Read a KQL pipeline.
- Use filtering and aggregation operators.
- Build a time series with bin().
- Complete a query in an exam snippet.
How a KQL query works
A KQL query starts with a table, and each pipe character passes the results to the next operator. Reading a query from top to bottom therefore describes a sequence of steps. Time filters should come early, as this reduces the data that later steps must process.
AppRequests
| where TimeGenerated > ago(1h)
| where Success == false
| summarize Failures = count() by Name
| order by Failures desc
| take 10This query starts with the requests table, keeps the last hour, keeps failed requests, counts them per operation name, sorts them in descending order and returns the top 10. Note that KQL is case-sensitive for table names, column names and operators.
Building a KQL query
Essential operators
The following table lists the operators that the exam expects you to know.
| Operator | Purpose | Example |
|---|---|---|
| where | Filter rows | where ResultCode == "500" |
| project | Choose or rename columns | project TimeGenerated, Name, DurationMs |
| extend | Add a calculated column | extend Seconds = DurationMs / 1000 |
| summarize | Aggregate | summarize count() by Name |
| order by | Sort | order by DurationMs desc |
| take | Return any N rows | take 20 |
| top | Return the first N by a column | top 5 by DurationMs desc |
| distinct | Return unique values | distinct ClientCity |
| join | Combine two tables | join kind=inner (AppExceptions) on OperationId |
| union | Combine rows from tables | union AppRequests, AppDependencies |
| render | Draw a chart | render timechart |
Filtering
== is a case-sensitive comparison, and =~ ignores case. has matches a whole term and is fast, contains matches any part of a string, and startswith matches the start. in tests a list of values. ago(1h) gives a time relative to now, with units such as d, h, m and s.
Aggregating
summarize is the most important operator. Functions include count(), sum(), avg(), max(), min(), dcount() for a distinct count, countif(), and percentile(). by sets the grouping, and bin() groups a time column into fixed periods.
KQL operators
Common query patterns
A failure rate over time
AppRequests
| where TimeGenerated > ago(24h)
| summarize Total = count(), Failed = countif(Success == false) by bin(TimeGenerated, 15m)
| extend FailureRate = 100.0 * Failed / Total
| project TimeGenerated, FailureRate
| render timechartLatency percentiles per operation
AppRequests
| where TimeGenerated > ago(1h)
| summarize P50 = percentile(DurationMs, 50), P95 = percentile(DurationMs, 95) by Name
| top 10 by P95 descFailed requests with their exceptions
AppRequests
| where TimeGenerated > ago(1h) and Success == false
| join kind=inner (AppExceptions | project OperationId, ExceptionType) on OperationId
| summarize count() by Name, ExceptionTypeHigh CPU on virtual machines
Perf
| where TimeGenerated > ago(30m)
| where ObjectName == "Processor" and CounterName == "% Processor Time" and InstanceName == "_Total"
| summarize AvgCpu = avg(CounterValue) by Computer, bin(TimeGenerated, 5m)
| where AvgCpu > 80With VM insights, the InsightsMetrics table holds similar data. Application Insights data in a workspace uses tables such as AppRequests, AppDependencies, AppExceptions, AppTraces and AppPageViews, whereas the Logs view inside a classic Application Insights experience may use requests, dependencies and exceptions, with timestamp in place of TimeGenerated. Recognizing both forms helps with exam snippets. Azure Monitor Logs also has the AzureActivity table for the activity log, and the AzureDiagnostics table for many resource logs.
The following diagram annotates each line of a KQL query that counts failed requests per hour.

A failure-rate query that misleads
From a query to an alert
A saved query can be turned into a log search alert, which runs it on a schedule, and fires according to the number of rows or a measured value. A query can also be pinned to a dashboard or used in a workbook. Efficient alert queries filter on time and on specific columns, as a broad query costs more and takes longer.
Queries as alerts
Completing a query
You need a chart of the number of failed requests per hour for the last day. Which operators complete the query?
AppRequests
| where TimeGenerated > ago(1d) and Success == false
| summarize Failures = count() by bin(TimeGenerated, 1h)
| render timechartwhere filters the time and result, summarize with bin creates one row per hour, and render timechart draws a line. project would only pick columns, without aggregating. extend would add a column for each row, and would not reduce them. take would return arbitrary rows.
What is the difference between project and extend?
Show answer
project chooses or renames columns, and drops the rest. extend adds a calculated column, and keeps the others.
Exam Tips
Remember for the exam
- A query starts from a table, and each pipe passes results to the next operator.
- summarize aggregates; bin() groups by time.
- project chooses columns; extend adds them; take returns any N rows.
- Filter by time first for efficient queries.
- Workspace tables use AppRequests and TimeGenerated; classic use requests and timestamp.