Query Syntax
KQL Query Structure and Syntax
Kusto Query Language (KQL) is the foundation of data exploration and analysis in Microsoft Sentinel. Understanding its structure and syntax is critical for crafting effective threat-hunting queries. A typical KQL query follows a pipeline model, where data flows through a sequence of operations that filter, transform, and analyze it.
1. Core Query Structure¶
A KQL query begins with a table name and is followed by a chain of operators connected by the | (pipe) symbol. Each operator processes the output of the previous step.
Basic syntax:
Example:
EventID 4624) and counts them by computer.
2. Filtering Data with where¶
The where clause filters rows based on conditions. Use logical operators (and, or, not) and comparison operators (==, !=, >, <, in, between) to refine results.
Example:
Tips:
- Use parentheses for complex conditions:
3. Joining Tables¶
Use the join operator to combine data from multiple tables. Specify the join type (inner, left, right, full) and the matching columns with on.
Example:
explorer.exe.
Key considerations:
- Ensure matching columns exist in both tables.
- Use join sparingly for large datasets; consider filtering first.
- Use extend to add computed fields before joining.
4. Aggregation and Summarization¶
Aggregation functions like count(), sum(), avg(), min(), and max() summarize data. Use summarize to group results by one or more fields.
Example:
EventID 4,624) per computer and time.
Advanced aggregations:
- Use argmax()/argmin() to find rows with extreme values:
extend for custom calculations:5. Projecting and Transforming Data¶
Use project to select specific columns and extend to add computed fields.
Example:
SecurityEvent
| project TimeGenerated, Computer, EventID, Description
| extend RiskScore = if(EventID == 4624, 10, 0)
Key takeaways¶
- Pipeline structure: Use
|to chain operations and process data step-by-step. - Filtering: Leverage
wherewith logical and comparison operators for precise data selection. - Joining: Combine tables with
joinand ensure matching columns for accurate results. - Aggregation: Use
summarizewith functions likecount()andavg()to derive insights. - Optimization: Filter and project early in the pipeline to improve performance and clarity.