Kusto Query Language 101 (2024)

Recently I’ve started spending more time using Azure Sentinel and I wanted to get up to speed on the Kusto Query Language. This is a collection of my ‘Kusto Query Language 101’ learnings.

What is Kusto Query Language(KQL)?

KQL is a read-only language similar to SQL that’s used to query large datasets in Azure. Unlike SQL, KQL can only be used to query data, not update or delete. KQL is commonly used in the following Azure services:

  • Azure Application Insights
  • Azure Log Analytics
  • Azure Monitor Logs
  • Azure Data Explorer

Fun fact, Kusto is named afterJacques Cousteau, as a reference to “exploring the ocean of data”.

How about an example?

To get started, let’s look at a sample KQL query.

SecurityEvent
| where AccountType == 'User'
| count

  • The query starts with a reference to theSecurityEvent table.
  • The data is then ‘piped’ through a where clause which filters the rows by the AccountType column. The pipe is used to bind together data transformation operators. Both the where clause and pipe (|) delimiter are key to writing KQL queries.
  • The query returns a count of the surviving rows.

You can chain additional statements together by piping the data to further statements:

SecurityEvent
| where AccountType == 'User'
| where TimeGenerated >= datetime(2021-01-01) and TimeGenerated < datetime(2021-01-31)
| count

Operators and Functions

Here are some KQL operators and functions I’ve been using recently.

String comparison

  • Equal: ==
    SecurityEvent
    | where AccountType == 'User'

  • Not equal: !=
    SecurityEvent
    | where AccountType != 'User

String Matching

  • Case Sensitive Match: ==
    SecurityEvent
    | where AccountType == 'User'

  • Case Insensitive Match: Either =~ or contains/has
    SecurityEvent
    | where AccountType =~ 'User'

    SecurityEvent
    | where AccountType contains 'User'

    SecurityEvent
    | where AccountType has
    ‘User’

  • Case Insensitive and not Equal To Match: !~
    SecurityEvent
    | where AccountType !~ 'User'

  • Match on values starting or ending with a specific string.
    SecurityEvent
    | where Account startswith 'NT'

Performance Tips
If there are two operators that do the same task, always use the case-sensitive one.

  • instead of=~, use==
  • instead ofin~, usein
  • instead ofcontains, usecontains_cs

Microsoft has outlined several best practices to improve your KQL query performance. You can find them here.

String Concatenation

The strcat() function allows you to concatenate between 1 and 64 arguments. If one of the arguments is not a string, it will forcibly be converted to a string.

print str = strcat("hello", " ", "world")

Numeric Operators

  • Equal: ==
    SecurityEvent
    | summarize count() by TargetUserName
    | where count_ == 1000

  • Greater Than: >
    SecurityEvent
    | summarize count() by TargetUserName
    | where count_ > 1000

  • Less Than: <
    SecurityEvent
    | summarize count() by TargetUserName
    | where count_ < 1000

  • Less or Equal: <=
    SecurityEvent
    | summarize count() by TargetUserName
    | where count_ <= 1000

  • Greater or Equal: >=
    SecurityEvent
    | summarize count() by TargetUserName
    | where count_ >= 1000

DateTime and Timespan

KQL offers powerful functionality around datetime and timespan values. Here are a few examples:

  • Refer tod,h,m, andsfor days, hours, minutes and seconds.
    SecurityEvent
    | where TimeGenerated > now(-7d)
  • Perform arithmetic operations on values of typesdatetimeandtimespan:
    datetime(2021-01-31) + 1d
    Returns: 2/1/2021, 12:00:00.000 AM

    Divide twotimespanvalues to get the quotient
    1h / 1s
    Returns: 3,600

  • Multiply numeric values (such asdoubleandlong) by atimespanvalue to get atimespanvalue.
    1.5 * 1hr
    Returns: 1:30:00

Sorting

  • Sort by:
    Sort the rows of the input table
    SecurityEvent
    | sort by TimeGenerated
  • Take:
    Returns up to the specified number of rows.
    SecurityEvent
    | take 5
  • Top:
    Returns the firstNrecords sorted by the specified columns.
    SecurityEvent
    | top 5 by TimeGenerated

Summarize

Similar to the GROUP BY SQL command, summarize groups together rows using some comparison, and then performs an aggregation.

SecurityEvent
| summarize count() by TargetUserName

The above query returns the number of security events by each target user.

Render

The render operator is used to create visualizations. These visualizations include:

  • Area Chart
  • Bar Chart
  • Column Chart
  • Pie Chart
  • Scatter Chart
  • Table
  • Time Chart

Let’s add the render operator to the above query:
SecurityEvent
| summarize count() by TargetUserName
| render piechart

This generates a pie chart based on the query provided.
Kusto Query Language 101 (1)

Resources

Here are some resources I’ve been using on my KQL journey. You may find them valuable too!

Summary

In this blog post I covered some of the essential functions and operators of KQL. In future blog posts I will continue sharing my learnings around KQL and how to effectively use it with Azure Sentinel.

Thank you for reading!

Comments are closed.

Kusto Query Language 101 (2024)
Top Articles
Did you get a call or text about a suspicious purchase on Amazon? It’s a scam
Retire by 40: How I retired at 35 + Calculator - Think Save Retire
Diario Las Americas Rentas Hialeah
Gomoviesmalayalam
RuneScape guide: Capsarius soul farming made easy
The Many Faces of the Craigslist Killer
Nestle Paystub
Craigslist Chautauqua Ny
Aktuelle Fahrzeuge von Autohaus Schlögl GmbH & Co. KG in Traunreut
Moparts Com Forum
Dutch Bros San Angelo Tx
My.tcctrack
Define Percosivism
Second Chance Maryland Lottery
Destiny 2 Salvage Activity (How to Complete, Rewards & Mission)
使用 RHEL 8 时的注意事项 | Red Hat Product Documentation
How Much Is Tay Ks Bail
Craigslist In Visalia California
Kirksey's Mortuary - Birmingham - Alabama - Funeral Homes | Tribute Archive
Nz Herald Obituary Notices
Baja Boats For Sale On Craigslist
Coomeet Premium Mod Apk For Pc
11 Ways to Sell a Car on Craigslist - wikiHow
Airline Reception Meaning
Cpt 90677 Reimbursem*nt 2023
When His Eyes Opened Chapter 3123
Best Town Hall 11
Rgb Bird Flop
91 Octane Gas Prices Near Me
Learn4Good Job Posting
Que Si Que Si Que No Que No Lyrics
Shaman's Path Puzzle
Everstart Jump Starter Manual Pdf
Newsday Brains Only
Vip Lounge Odu
Elgin Il Building Department
Deshuesadero El Pulpo
Sept Month Weather
How to Get a Better Signal on Your iPhone or Android Smartphone
Oppenheimer Showtimes Near B&B Theatres Liberty Cinema 12
Sand Castle Parents Guide
Owa Hilton Email
Hawkview Retreat Pa Cost
Craigslist Houses For Rent Little River Sc
How the Color Pink Influences Mood and Emotions: A Psychological Perspective
The Complete Uber Eats Delivery Driver Guide:
Mountainstar Mychart Login
Wisconsin Volleyball titt*es
Chitterlings (Chitlins)
Coldestuknow
Mast Greenhouse Windsor Mo
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6372

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.