Basic Queries
Overview:
The following section goes over the basic queries that can be run within KQL.
Data Retrieval
Selecting Columns
Use the
projectoperator to select specific columns from a table.Example with security logs:
AzureActivity | project TimeGenerated, Computer, EventID, AccountName, LogonType
Filtering Rows with
whereClauseThe
whereclause filters rows based on a condition.Example to filter security logs for logon events:
SecurityEvent | where EventID == 4624Multiple conditions can be combined using logical operators (
and,or,not).Example to filter for successful logons by a specific user:
SecurityEvent | where EventID == 4624 and AccountName == "jdoe"
Sorting and Limiting Data
Using
sort byThe
sort byoperator sorts the results by one or more columns.Example to sort security logs by time:
SecurityEvent | sort by TimeGenerated desc
Limiting Results with
topandtakeThe
topoperator returns the top N rows based on a specific column.Example to get the top 10 most recent security events:
SecurityEvent | top 10 by TimeGenerated descThe
takeoperator returns the first N rows from the result set.Example to take the first 5 rows from the security event log:
SecurityEvent | take 5
Aggregating Data
Using
summarizefor AggregationsThe
summarizeoperator is used for data aggregation, such as calculating sums, averages, counts, etc.Example to count the number of events per computer:
SecurityEvent | summarize EventCount = count() by ComputerAggregations can be grouped by one or more columns.
Example to count the number of logon events per account:
SecurityEvent | where EventID == 4624 | summarize LogonCount = count() by AccountName
Common Aggregation Functions
sum(): Calculates the sum of values.SecurityEvent | summarize TotalEvents = sum(EventCount)count(): Counts the number of rows.SecurityEvent | summarize TotalEvents = count()avg(): Calculates the average of values.SecurityEvent | summarize AvgEventDuration = avg(Duration)min(),max(): Finds the minimum and maximum values.SecurityEvent | summarize MinTime = min(TimeGenerated), MaxTime = max(TimeGenerated)
Last updated