Mindfulness

KQL Syntax Cheat Sheet for Beginners

Updated · By the Relaxmid editors

Reading time
4 min
Sections
11
Topic
Mindfulness

KQL (Kusto Query Language) is a read-only query language used to explore and analyze data in Azure Data Explorer, Azure Monitor, and Microsoft Sentinel. The most common mistake beginners make is treating KQL like SQL: KQL uses a pipe (|) to pass a table from one operator to the next, not SELECT or FROM. Start with a table name, then pipe it through filters, projections, and aggregations.

Core Query Structure

Every KQL query begins with a table name (or a function that returns a table). Then you use the pipe operator | to apply successive operations. The result of each step is a table that flows into the next step.

StormEvents
| where State == "FLORIDA"
| project EventType, DamageProperty
| take 10

This query gets the StormEvents table, filters to Florida, selects two columns, and shows the first 10 rows. The order matters: you usually filter before projecting to reduce data early.

Key Operators

  • where – filters rows by a condition (like SQL’s WHERE).
  • project – selects columns (like SELECT).
  • extend – adds a new computed column.
  • summarize – groups rows and computes aggregates (like GROUP BY).
  • order by – sorts rows (use asc or desc).
  • take – returns the first N rows (like LIMIT).

Filtering with where

The where operator is the workhorse for narrowing data. Use comparison operators: == (equals), != (not equals), >, <, >=, <=. Combine conditions with and and or.

StormEvents
| where State == "TEXAS" and DamageProperty > 1000
| where EventType in ("Tornado", "Flood")

For strings, use contains for a case-insensitive substring match, and startswith or endswith for prefix/suffix. To match a pattern, use matches regex.

// Find events with "wind" in the description
StormEvents
| where EventType contains "wind"

Projecting and Extending Columns

project keeps only the columns you list. extend adds a new column while keeping existing ones. Use project-rename to rename columns, and project-away to drop specific columns.

StormEvents
| project EventType, DamageProperty
| extend DamageCategory = iff(DamageProperty > 5000, "High", "Low")

The iff() function is like a ternary operator: iff(condition, true_value, false_value). You can also use case() for multiple conditions.

Summarizing and Aggregating

summarize groups rows and computes aggregates. The syntax is summarize aggregate_function(column) by group_column. Common aggregates: count(), sum(), avg(), min(), max(), dcount() (distinct count).

StormEvents
| summarize TotalDamage = sum(DamageProperty), EventCount = count() by State
| order by TotalDamage desc

This gives total damage and event count per state, sorted by damage. You can group by multiple columns: by State, EventType.

Time Bucketing

Use bin() to group time into buckets. For example, bin(StartTime, 1d) groups by day. This is essential for time-series analysis.

StormEvents
| summarize EventCount = count() by bin(StartTime, 1d)
| order by StartTime asc

Joining Tables

KQL supports join to combine two tables. Specify the kind of join: inner, leftouter, rightouter, fullouter, and more. The syntax is:

Table1
| join kind=inner (Table2) on KeyColumn

You can join on multiple keys with on Key1, Key2. For example, joining storm events with population data:

StormEvents
| join kind=leftouter (PopulationData) on State
| project State, EventType, Population

Sorting and Limiting

Use order by (or sort by) to sort. Add asc or desc. take limits the number of rows returned. top N by Column is a shortcut for sorting and taking.

StormEvents
| top 5 by DamageProperty desc

This returns the 5 rows with the highest damage. For a random sample, use sample N.

Working with Strings and Dates

String functions: strcat() to concatenate, substring(), toupper(), tolower(), split(). Date functions: datetime_add() to add time, datetime_diff() to find difference, format_datetime() to format.

StormEvents
| extend Month = format_datetime(StartTime, "MM")
| extend DamageLabel = strcat("$", DamageProperty)

Common Pitfalls and Rules of Thumb

Rule of thumb: always filter (where) before projecting (project) to reduce data early. Also, use take while exploring to avoid huge results.

  • Case sensitivity: KQL is case-sensitive for table and column names, but string comparisons are case-insensitive by default.
  • Quotes: use single quotes for string literals, but double quotes also work.
  • Nulls: use isnull() and isnotnull() to check for null values.
  • Comments: use // for single-line comments, /* ... */ for block comments.
  • Pipe order: the order of operators changes the result. where then project is not the same as project then where.

Putting It All Together: A Realistic Example

Suppose you want to find the top 3 states with the most tornado events in 2023, and the average damage per event.

StormEvents
| where EventType == "Tornado" and StartTime >= datetime(2023-01-01) and StartTime < datetime(2024-01-01)
| summarize TornadoCount = count(), AvgDamage = avg(DamageProperty) by State
| order by TornadoCount desc
| take 3

This filters to tornadoes in 2023, groups by state, calculates count and average damage, sorts descending, and takes the top 3.

Next Steps: What to Learn After the Basics

Once you are comfortable with these operators, explore let statements to define variables and functions, materialize() to cache intermediate results, and render to create charts. Also learn about union to combine multiple tables, and mv-expand to expand arrays or multi-value columns.

Practice by running queries in your own environment. Start with a small table you know well, and try each operator one at a time. The official Microsoft documentation has a KQL quick reference guide that you can keep open while you work.

Related

General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more

Spotted an error? Tell us and we will correct it (corrections policy).