Part 2: KQL Syntax Fundamentals

Building on the Basics

After learning the fundamentals in Part 1, I found that mastering core KQL syntax operators transformed my ability to extract meaningful insights from Azure logs. In this part, I'll share the operators I use daily and how I've learned to combine them effectively.

Core Tabular Operators

KQL queries are built using tabular operators that transform data step by step. Let me walk you through each one based on my experience.

1. where - Filtering Rows

The where operator is the workhorse of KQL queries. I use it in nearly every query to filter data.

Basic filtering:

AzureActivity
| where TimeGenerated > ago(24h)
| where OperationNameValue contains "write"
| where Level == "Informational"

Comparison operators I use:

// Equality
| where Level == "Error"
| where Level != "Informational"

// Numeric comparisons
| where CounterValue > 80
| where CounterValue >= 90
| where DurationMs between (100 .. 1000)

// String operations
| where Message contains "error"       // Case-insensitive substring
| where Message !contains "warning"
| where Message startswith "ERROR:"
| where Message endswith "failed"
| where Message matches regex "\\d{3}-\\d{3}-\\d{4}"  // Regex pattern

// Case-sensitive versions
| where Message contains_cs "Error"    // Case-sensitive
| where Message has "error"            // Faster, word boundary matching
| where Message has_cs "Error"

Logical operators:

Null handling:

2. project - Selecting and Computing Columns

The project operator selects which columns to include and can create new calculated columns.

Basic column selection:

Creating calculated columns:

String manipulation:

DateTime operations:

3. extend - Adding Columns Without Removing Others

Unlike project, extend adds new columns while keeping all existing ones. I use this when I need to add calculations but want to preserve all original data.

Chaining extend operations:

4. summarize - Aggregating Data

The summarize operator is where KQL really shines. I use it for aggregations, grouping, and statistical analysis.

Basic aggregations:

Time-based aggregations:

Advanced aggregations I use:

5. order (sort) - Sorting Results

Both order and sort do the same thing. I prefer order but both work:

6. take (limit) - Limiting Results

Use take or limit to control result set size. Critical during query development:

7. top - Getting Top N Results

top combines ordering and limiting:

8. distinct - Unique Values

Get unique values from a column:

9. sample - Random Sampling

When debugging large datasets, I use sample:

Data Types in KQL

Understanding data types helps write efficient queries. Here's what I've learned:

Common Data Types:

Type Conversion:

String Operations I Use Daily

Working with logs means lots of string manipulation:

Extraction and Parsing:

String Matching:

String Building:

DateTime Operations

Time is fundamental to observability. Here are the time operations I rely on:

Relative Time:

Date Parts and Formatting:

Time Calculations:

Time Binning:

Practical Query Patterns

Here are query patterns I use regularly in production:

Pattern 1: Error Rate Over Time

Pattern 2: Top Talkers

Pattern 3: Resource Health Check

Query Performance Tips

Based on my experience optimizing queries:

1. Filter Early and Aggressively

2. Use Appropriate Operators

3. Limit During Development

4. Project Only Needed Columns

Common Mistakes I Made

Let me share mistakes I made when learning KQL:

Mistake 1: Wrong operator order

Mistake 2: String comparison case sensitivity

Mistake 3: Not handling nulls

Practice Exercises

Try these queries to reinforce your learning:

Exercise 1: Activity Summary

Exercise 2: Performance Analysis

Exercise 3: String Parsing

Key Takeaways

  • where filters rows - use it early and often

  • project selects specific columns and creates calculated fields

  • extend adds columns while keeping all original data

  • summarize aggregates data - the heart of analytics queries

  • Always filter by time first for performance

  • Understand string operations for log parsing

  • DateTime functions are essential for time-series analysis

  • Query performance matters - filter early, project sparingly

In Part 3, we'll explore advanced operators like join, union, mv-expand, and powerful functions that enable complex analysis across multiple data sources.

Last updated