Understanding KQL
This lesson explains where KQL runs and the operators and functions for filtering, shaping, time handling, aggregation and charting.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the structure of a KQL query.
- Choose operators for filtering and shaping.
- Explain string comparison operators.
- Explain why percentiles are used for latency.
Where KQL runs
Kusto Query Language (KQL) is a read-only query language for large volumes of log and telemetry data. In Azure Monitor it queries Log Analytics workspaces, which store Application Insights data (for workspace-based resources), Container Apps and Container insights logs, resource logs sent by diagnostic settings and many other sources. Queries are written on the Logs page of a workspace, an Application Insights resource or another resource, and the same queries drive log search alerts, workbooks and dashboards.
A query starts with a table name and passes rows through a sequence of operators separated by the pipe character |. Each operator takes the rows from the previous step and produces new rows, so a query reads from top to bottom like a recipe.
Filtering and shaping
The most frequently used operators filter rows and shape columns.
| Operator | Purpose | Example |
|---|---|---|
where | Keep rows that match a condition | where Success == false and DurationMs > 1000 |
project | Choose, rename and compute columns | project TimeGenerated, Name, DurationMs |
extend | Add calculated columns, keeping existing ones | extend Seconds = DurationMs / 1000 |
take or limit | Return a number of arbitrary rows | take 10 |
sort by / order by | Sort rows | order by DurationMs desc |
top | Sort and keep the first N | top 5 by DurationMs desc |
distinct | Unique combinations of columns | distinct ResultCode |
String comparisons follow precise rules. == is case-sensitive and =~ is case-insensitive. has matches whole terms using the index and is fast, while contains matches any substring and is slower; startswith, endswith, in and matches regex cover other cases. Dynamic (JSON) columns, such as custom dimensions, are accessed with dot or bracket notation and usually converted with tostring() or toint().
The following diagram shows a KQL query as a pipeline of operators.

KQL operators
Time
Every log table has a time column, TimeGenerated in Log Analytics tables (timestamp in the classic Application Insights tables). Filtering on time early in the query is the most important performance practice, because the engine can skip data outside the range. ago(1h) returns the time one hour before now; timespans are written as 30s, 5m, 2h or 7d; between (datetime(2026-11-01) .. datetime(2026-11-02)) selects a range; and startofday() and similar functions round times.
Working with time
Aggregation and charts
summarize groups rows and computes aggregates for each group. Common aggregation functions include count(), countif(), sum(), avg(), min(), max(), dcount() (approximate distinct count), percentile() and percentiles(), and make_set(). Grouping by bin(TimeGenerated, 5m) buckets rows into five-minute intervals, which produces a time series that render timechart draws.
AppRequests
| where TimeGenerated > ago(24h)
| summarize Requests = count(), Failures = countif(Success == false),
P95 = percentile(DurationMs, 95) by bin(TimeGenerated, 15m)
| extend FailureRate = round(100.0 * Failures / Requests, 2)
| render timechartAverages can hide problems: a service with most requests at 100 ms and a few at 10 seconds has a reasonable average but unhappy users. Percentiles, such as the 95th or 99th, describe what most users experience and are the right measure for latency objectives. let statements name values or sub-queries for reuse, which keeps longer queries readable.
Building a latency chart
Reading a query
An engineer must find which API operations had the slowest typical latency for failed requests in the last six hours, considering only operations with at least 20 failures.
The query starts from AppRequests, filters early on time and failure, groups by operation name with a count and a median (50th percentile) of duration, keeps groups with at least 20 failures, and sorts.
AppRequests
| where TimeGenerated > ago(6h) and Success == false
| summarize Failures = count(), MedianMs = percentile(DurationMs, 50) by Name
| where Failures >= 20
| order by MedianMs descFiltering with where Failures >= 20 after summarize works on the aggregated rows, which a filter before summarize could not do. Using avg() would let a few extreme failures dominate. Omitting the time filter would scan the whole retention period.
What is the difference between has and contains in KQL?
Show answer
has matches whole terms using the index and is fast; contains matches any substring and is slower.
Exam Tips
Remember for the exam
- A query is a table followed by operators joined with |; filter on time first.
- where filters, project selects, extend adds, summarize aggregates by groups.
- == is case-sensitive, =~ is not; has matches whole terms faster than contains.
- Use percentile() for latency, bin() for time series and render timechart to chart.