Top 10 source IPs blocked more than once in a firewall log window

Quick Overview

Write a SQL query over firewall logs that finds the ten source IP addresses blocked most often within a one-day window, keeping only addresses blocked more than once. It tests time-range filtering with inclusive endpoints, GROUP BY with HAVING, tie-breaking in ORDER BY and LIMIT.

Top 10 source IPs blocked more than once in a firewall log window

Company: Palo Alto Networks

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of `firewall_logs` is one connection attempt and the action the firewall took on it. Find the top 10 `source_ip` values that were blocked more than once between `2024-01-14 00:00:00` and `2024-01-15 00:00:00`. Return `source_ip` and `block_count`, ordered by `block_count` descending, then `source_ip` ascending, limited to 10 rows. ```hint Where each filter goes Some conditions test a single log row and one tests a whole group of rows. Put each one where it can run. ``` ### Input Table(s) `firewall_logs` | Column | Type | Meaning | |---|---|---| | log_id | INT | Primary key, one row per log entry | | source_ip | VARCHAR(45) | IPv4 address that started the connection, NOT NULL | | destination_ip | VARCHAR(45) | IPv4 address it tried to reach, NOT NULL. Not needed for this query. | | action | VARCHAR(20) | Firewall decision, exactly `'ALLOW'` or `'BLOCK'`, NOT NULL | | log_time | TIMESTAMP | When the entry was logged (DATETIME in MySQL), NOT NULL | Keys: `log_id` is the primary key. The same `source_ip` can appear in many rows. ### Output Contract | Column | Type | Meaning | |---|---|---| | source_ip | VARCHAR | The blocked source address | | block_count | BIGINT | Number of `BLOCK` rows for this address inside the window | - Only addresses with `block_count` of 2 or more. - Order by `block_count` descending, then `source_ip` ascending. Compare `source_ip` as plain text, byte by byte (C collation), so `10.0.0.10` sorts before `10.0.0.9`. Each address appears once in the output, so the order is fully determined. - Return at most 10 rows, and fewer when fewer addresses qualify. ### Example `firewall_logs` | log_id | source_ip | destination_ip | action | log_time | |---|---|---|---|---| | 1 | 10.0.0.5 | 172.16.0.1 | BLOCK | 2024-01-14 01:15:00 | | 2 | 10.0.0.5 | 172.16.0.2 | BLOCK | 2024-01-14 09:40:00 | | 3 | 10.0.0.5 | 172.16.0.1 | BLOCK | 2024-01-14 23:59:59 | | 4 | 192.168.1.20 | 172.16.0.3 | BLOCK | 2024-01-14 00:00:00 | | 5 | 192.168.1.20 | 172.16.0.3 | BLOCK | 2024-01-15 00:00:00 | | 6 | 10.0.0.10 | 172.16.0.4 | BLOCK | 2024-01-14 12:00:00 | | 7 | 10.0.0.10 | 172.16.0.4 | BLOCK | 2024-01-14 12:00:30 | | 8 | 10.0.0.9 | 172.16.0.2 | BLOCK | 2024-01-14 03:00:00 | | 9 | 10.0.0.9 | 172.16.0.5 | BLOCK | 2024-01-14 04:00:00 | | 10 | 10.0.0.7 | 172.16.0.1 | BLOCK | 2024-01-14 08:00:00 | | 11 | 10.0.0.7 | 172.16.0.1 | ALLOW | 2024-01-14 08:05:00 | | 12 | 10.0.0.7 | 172.16.0.2 | BLOCK | 2024-01-13 23:59:59 | | 13 | 10.0.0.8 | 172.16.0.2 | BLOCK | 2024-01-14 18:30:00 | | 14 | 10.0.0.8 | 172.16.0.2 | BLOCK | 2024-01-15 00:00:01 | Expected result: | source_ip | block_count | |---|---| | 10.0.0.5 | 3 | | 10.0.0.10 | 2 | | 10.0.0.9 | 2 | | 192.168.1.20 | 2 | `192.168.1.20` keeps both blocks because the window includes both endpoints. `10.0.0.7` has one block inside the window (its `ALLOW` row does not count and its other block is one second early), and `10.0.0.8` has one block inside and one a second after the end, so both drop out. The three addresses tied at 2 appear in text order. ### Constraints and Clarifications - Up to 10,000 rows. - `action` is exactly `'ALLOW'` or `'BLOCK'`, in uppercase. - `source_ip` and `destination_ip` are valid IPv4 addresses. - The window includes both endpoints: a row logged at exactly `2024-01-14 00:00:00` or exactly `2024-01-15 00:00:00` counts. - Only `BLOCK` rows count. `ALLOW` rows for the same address neither add to the count nor reduce it. - Every row is a separate event, even when two rows share the same addresses and timestamp. - No column is NULL. - If several addresses tie around 10th place, the `source_ip` tiebreak decides which ones are kept.

Overview: Write a SQL query over firewall logs that finds the ten source IP addresses blocked most often within a one-day window, keeping only addresses blocked more than once. It tests time-range filtering with inclusive endpoints, GROUP BY with HAVING, tie-breaking in ORDER BY and LIMIT.

Palo Alto Networks logo
Palo Alto Networks
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of firewall_logs is one connection attempt and the action the firewall took on it. Find the top 10 source_ip values that were blocked more than once between 2024-01-14 00:00:00 and 2024-01-15 00:00:00. Return source_ip and block_count, ordered by block_count descending, then source_ip ascending, limited to 10 rows.

Input Table(s)

firewall_logs

ColumnTypeMeaning
log_idINTPrimary key, one row per log entry
source_ipVARCHAR(45)IPv4 address that started the connection, NOT NULL
destination_ipVARCHAR(45)IPv4 address it tried to reach, NOT NULL. Not needed for this query.
actionVARCHAR(20)Firewall decision, exactly 'ALLOW' or 'BLOCK', NOT NULL
log_timeTIMESTAMPWhen the entry was logged (DATETIME in MySQL), NOT NULL

Keys: log_id is the primary key. The same source_ip can appear in many rows.

Output Contract

ColumnTypeMeaning
source_ipVARCHARThe blocked source address
block_countBIGINTNumber of BLOCK rows for this address inside the window
  • Only addresses with block_count of 2 or more.
  • Order by block_count descending, then source_ip ascending. Compare source_ip as plain text, byte by byte (C collation), so 10.0.0.10 sorts before 10.0.0.9 . Each address appears once in the output, so the order is fully determined.
  • Return at most 10 rows, and fewer when fewer addresses qualify.

Example

firewall_logs

log_idsource_ipdestination_ipactionlog_time
110.0.0.5172.16.0.1BLOCK2024-01-14 01:15:00
210.0.0.5172.16.0.2BLOCK2024-01-14 09:40:00
310.0.0.5172.16.0.1BLOCK2024-01-14 23:59:59
4192.168.1.20172.16.0.3BLOCK2024-01-14 00:00:00
5192.168.1.20172.16.0.3BLOCK2024-01-15 00:00:00
610.0.0.10172.16.0.4BLOCK2024-01-14 12:00:00
710.0.0.10172.16.0.4BLOCK2024-01-14 12:00:30
810.0.0.9172.16.0.2BLOCK2024-01-14 03:00:00
910.0.0.9172.16.0.5BLOCK2024-01-14 04:00:00
1010.0.0.7172.16.0.1BLOCK2024-01-14 08:00:00
1110.0.0.7172.16.0.1ALLOW2024-01-14 08:05:00
1210.0.0.7172.16.0.2BLOCK2024-01-13 23:59:59
1310.0.0.8172.16.0.2BLOCK2024-01-14 18:30:00
1410.0.0.8172.16.0.2BLOCK2024-01-15 00:00:01

Expected result:

source_ipblock_count
10.0.0.53
10.0.0.102
10.0.0.92
192.168.1.202

192.168.1.20 keeps both blocks because the window includes both endpoints. 10.0.0.7 has one block inside the window (its ALLOW row does not count and its other block is one second early), and 10.0.0.8 has one block inside and one a second after the end, so both drop out. The three addresses tied at 2 appear in text order.

Constraints and Clarifications

  • Up to 10,000 rows.
  • action is exactly 'ALLOW' or 'BLOCK' , in uppercase.
  • source_ip and destination_ip are valid IPv4 addresses.
  • The window includes both endpoints: a row logged at exactly 2024-01-14 00:00:00 or exactly 2024-01-15 00:00:00 counts.
  • Only BLOCK rows count. ALLOW rows for the same address neither add to the count nor reduce it.
  • Every row is a separate event, even when two rows share the same addresses and timestamp.
  • No column is NULL.
  • If several addresses tie around 10th place, the source_ip tiebreak decides which ones are kept.