Kusto Query Language (KQL) is used to query data in Azure, particularly within Azure Monitor, Azure Log Analytics, and Azure Sentinel. Understanding the KQL query structure is key to efficiently retrieving and analyzing data. Below are the fundamental elements of KQL query structure:
Basic Structure
A KQL query typically follows this general structure:
xxxxxxxxxx41TableName2| Operator13| Operator24| ...TableName: Specifies the data table you want to query (e.g.,
AzureDiagnostics,Perf,Heartbeat, etc.).Operators: Perform actions such as filtering, aggregating, sorting, or transforming the data.
Clauses and Operators
KQL queries are composed of a sequence of operators. Each operator processes the data in stages. The most common operators include:
where: Filters data based on specific conditions.
xxxxxxxxxx21TableName2| where ColumnName == "value"project: Selects the columns you want to display.
xxxxxxxxxx21TableName2| project Column1, Column2summarize: Aggregates the data (e.g., count, average, sum).
xxxxxxxxxx21TableName2| summarize Count = count() by Column1extend: Creates new columns based on expressions.
xxxxxxxxxx21TableName2| extend NewColumn = Column1 + Column2order by: Sorts the data by specified columns.
xxxxxxxxxx21TableName2| order by Column1 descproject-away: Removes specified columns from the result.
xxxxxxxxxx21TableName2| project-away Column1, Column2join: Combines two tables based on a common column.
xxxxxxxxxx21Table12| join kind=inner (Table2) on ColumnNameTime Handling
Time-based queries are a core part of KQL. Common time-related functions and clauses include:
ago(): Returns a time span relative to the current time (e.g.,ago(1h)for the last hour).
xxxxxxxxxx21TableName2| where TimeGenerated > ago(1d)bin(): Groups data into time buckets (e.g., 1 hour, 30 minutes).
xxxxxxxxxx21TableName2| summarize count() by bin(TimeGenerated, 1h)TimeGenerated: Most tables in Log Analytics contain this column, which holds the timestamp for each log entry.
Data Aggregation
You can use aggregation functions in KQL to summarize or group your data. Examples include:
count(): Counts the number of records.
xxxxxxxxxx21TableName2| summarize TotalRecords = count() by ColumnNameavg(): Calculates the average of a numerical column.
xxxxxxxxxx21TableName2| summarize AvgValue = avg(ColumnName) by ColumnNamesum(): Sums the values of a numerical column.
xxxxxxxxxx21TableName2| summarize Total = sum(ColumnName) by ColumnNamemin()andmax(): Finds the minimum and maximum values.
xxxxxxxxxx21TableName2| summarize MinValue = min(ColumnName), MaxValue = max(ColumnName) by ColumnNameData Transformation
You can create new data or manipulate existing data using functions like:
extend: Adds new columns based on expressions.
xxxxxxxxxx21TableName2| extend NewColumn = Column1 * 10project-rename: Renames columns for clarity.
xxxxxxxxxx21TableName2| project-rename NewColumnName = OldColumnNameJoins
KQL allows you to join data from different tables. There are several types of joins:
inner join: Combines records that match from both tables.
xxxxxxxxxx21Table12| join kind=inner (Table2) on ColumnNameleftouter join: Includes all records from the left table and matching records from the right table.
xxxxxxxxxx21Table12| join kind=leftouter (Table2) on ColumnNameRegex and String Functions
KQL provides support for pattern matching and string manipulation:
matches regex: Matches strings based on regular expressions.
xxxxxxxxxx21TableName2| where ColumnName matches regex @"\d+"strcat(): Concatenates multiple strings.
xxxxxxxxxx21TableName2| extend FullName = strcat(FirstName, " ", LastName)Subqueries
KQL allows the use of subqueries, which are queries within queries:
xxxxxxxxxx21TableName2| where ColumnName in (TableName2 | project ColumnName)Limit and Pagination
To limit the result set, KQL uses the take operator:
take: Limits the number of rows returned.
xxxxxxxxxx21TableName2| take 10top: Sorts and limits the result set based on a column's values.
xxxxxxxxxx21TableName2| top 10 by ColumnName descCommenting
KQL allows comments within queries, using // for single-line comments and /* ... */ for multi-line comments.
Single-line comment:
xxxxxxxxxx11// This is a single-line commentMulti-line comment:
xxxxxxxxxx41/* 2This is a3multi-line comment4*/Best Practices for KQL
Start simple: Begin with basic queries and build complexity as needed.
Optimize for performance: Avoid unnecessary joins, especially across large datasets, and use indexing and filtering strategies efficiently.
Use descriptive names: Name your columns and variables in a way that makes the results easy to understand.
Example Query
xxxxxxxxxx61AzureDiagnostics2| where TimeGenerated > ago(1d)3| where Resource == "myResource"4| project TimeGenerated, Resource, Message5| summarize Count = count() by bin(TimeGenerated, 1h)6| order by TimeGenerated descThis query retrieves records from the AzureDiagnostics table where the data is from the past 24 hours, filters by resource, selects specific columns, counts the occurrences per hour, and sorts the result by the timestamp in descending order.
Summary
By mastering these fundamental concepts, you can leverage KQL effectively for querying and analyzing data in Azure services.




















Leave a Reply