where
where keeps the rows for which a condition is true and drops the rest. It is the operator you will
write most often, and usually the first one after the table name.
SigninLogs
| where ResultType != "0"
The condition can use comparisons (==, !=, <, >, <=, >=), string operators such as
has or startswith, lists with in, ranges with
between, and any function that returns true or false. Join conditions with and and
or, and wrap a condition in not() to turn it around. and binds tighter than or, so add
parentheses when you mix them.
filter is another name for where; you will rarely see it.
Things to know
==compares strings with exact case:"Admin" == "admin"is false. Use=~when case should not matter (see == vs =~).- Several
wherelines in a row are the same as onewherewithand. Splitting them up often makes a query easier to read and to change. - Filter as early as you can. Every operator after
wherethen works on fewer rows, which is faster on a real workspace with billions of rows. Filtering on time first (where TimeGenerated > ago(1d)) is a good habit.
Examples
SigninLogs
| where AppDisplayName == "Azure Portal" or AppDisplayName == "Microsoft Entra admin center"
| where Location != "CZ"
| project TimeGenerated, UserPrincipalName, AppDisplayName, Location, IPAddressTwo where lines in a row work like one where joined with and.
SecurityEvent
| where EventID == 4625 and Computer == "SRV-SQL01.kestrelbay.example"
| summarize Failures = count() by TargetUserNameEvent 4625 is a failed Windows logon. All of them on this server come from one service account.
SigninLogs
| where not(ResultType == "0" or ResultType == "50140")
| summarize SignIns = count() by ResultType, ResultDescriptionnot() takes a whole condition. Here it drops successes (0) and the harmless "stay signed in" prompt (50140).
Practice it
- #01Find the IP behind a password sprayeasy
- #02Complete the query: brute force against a servermedium
- #03Spot impossible travel: two countries in one hourmedium
- #04Size up a password spray from one IPeasy
- #05Complete the query: did the password spray work?easy
- #06Failed sign-ins by client app, most firsteasy
- #07Which account names did the attacker try?easy
- #08When did the brute force start and stop?medium
- #09Did the brute force succeed?easy
- #10Complete the query: guesses at accounts that don't existmedium
- #11Catch an Office document starting a shelleasy
- #12Complete the query: find encoded PowerShell (has vs contains)medium
- #13What did the attacker run to look around?easy
- #14Find the one-device destination with hundreds of connectionsmedium
- #15Successful sign-ins from countries that almost never appearmedium
+ 24 in the full SC-200 set · all exercises