Running total of weekly cyber threats per client

Quick Overview

Write a SQL query that groups each client's detected security threats into Monday-start weeks, counts them per week, and adds a running total over time. It tests date truncation to a week start, aggregation before a window function, and cumulative sums partitioned by client.

Running total of weekly cyber threats per client

Company: Zscaler

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `cyber_threats` table logs every threat detected for a client. For each client, compute the weekly threat count and a running total of threats over time. A week is identified by the Monday of the week in which the threat was detected. ```hint Pin down the week first Turn each detection date into the date of its week's Monday before you aggregate, and test that expression on a Sunday. ``` ### Input Table(s) Table `cyber_threats` | Column | Type | Meaning | |---|---|---| | threat_id | INT | Threat identifier | | client_id | INT | Client the threat was detected for (NOT NULL) | | threat_type | VARCHAR(50) | One of 'Malware', 'Phishing', 'Ransomware', 'DDoS', 'Brute Force', 'SQL Injection' (NOT NULL) | | severity | VARCHAR(20) | One of 'Low', 'Medium', 'High', 'Critical' (NOT NULL) | | date_detected | DATE | Date the threat was detected (NOT NULL) | Keys: `threat_id` is the primary key. ### Output Contract - Columns, in this order: `client_id`, `threat_week`, `weekly_count`, `running_total`. - `threat_week` is a `DATE`: the Monday of the week. - `weekly_count` is the number of threats for that client in that week. - `running_total` is the sum of `weekly_count` for that client over all of its weeks up to and including this one. - One row per client and week that has at least one threat. - Order by `client_id` ascending, then `threat_week` ascending. - All counts are integers; no rounding. ### Example Input `cyber_threats`: | threat_id | client_id | threat_type | severity | date_detected | |---|---|---|---|---| | 1 | 1 | Malware | High | 2024-03-04 | | 2 | 1 | Phishing | Low | 2024-03-06 | | 3 | 1 | DDoS | Critical | 2024-03-10 | | 4 | 1 | Ransomware | Critical | 2024-03-11 | | 5 | 1 | Phishing | Medium | 2024-03-27 | | 6 | 2 | Brute Force | Medium | 2024-03-09 | | 7 | 2 | SQL Injection | High | 2024-03-13 | Expected output: | client_id | threat_week | weekly_count | running_total | |---|---|---|---| | 1 | 2024-03-04 | 3 | 3 | | 1 | 2024-03-11 | 1 | 4 | | 1 | 2024-03-25 | 1 | 5 | | 2 | 2024-03-04 | 1 | 1 | | 2 | 2024-03-11 | 1 | 2 | 2024-03-04 is a Monday, and 2024-03-10 is the Sunday that closes the same week, so client 1 has 3 threats in the week of 2024-03-04. Threat 4 falls on the next Monday and starts a new week. Client 1 has nothing in the week of 2024-03-18, so that week has no row, and the week of 2024-03-25 continues the total from 4 to 5. Client 2's total is separate and starts at 1. ### Constraints and Clarifications - `cyber_threats` has between 1 and 10,000 rows. - `client_id` is a positive integer and `date_detected` is a valid date. No column contains NULLs. - Weeks run Monday through Sunday. A threat detected on a Sunday belongs to the week that began six days earlier. - Weeks in which a client has no threats are not output; the running total simply carries over to the client's next listed week. - A week is identified by its Monday date, so a week that crosses a year boundary stays one week (2025-01-01 belongs to the week of 2024-12-30). - Every threat counts once, whatever its type or severity.

Overview: Write a SQL query that groups each client's detected security threats into Monday-start weeks, counts them per week, and adds a running total over time. It tests date truncation to a week start, aggregation before a window function, and cumulative sums partitioned by client.

Zscaler logo
Zscaler
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The cyber_threats table logs every threat detected for a client. For each client, compute the weekly threat count and a running total of threats over time. A week is identified by the Monday of the week in which the threat was detected.

Input Table(s)

Table cyber_threats

ColumnTypeMeaning
threat_idINTThreat identifier
client_idINTClient the threat was detected for (NOT NULL)
threat_typeVARCHAR(50)One of 'Malware', 'Phishing', 'Ransomware', 'DDoS', 'Brute Force', 'SQL Injection' (NOT NULL)
severityVARCHAR(20)One of 'Low', 'Medium', 'High', 'Critical' (NOT NULL)
date_detectedDATEDate the threat was detected (NOT NULL)

Keys: threat_id is the primary key.

Output Contract

  • Columns, in this order: client_id , threat_week , weekly_count , running_total .
  • threat_week is a DATE : the Monday of the week.
  • weekly_count is the number of threats for that client in that week.
  • running_total is the sum of weekly_count for that client over all of its weeks up to and including this one.
  • One row per client and week that has at least one threat.
  • Order by client_id ascending, then threat_week ascending.
  • All counts are integers; no rounding.

Example

Input cyber_threats:

threat_idclient_idthreat_typeseveritydate_detected
11MalwareHigh2024-03-04
21PhishingLow2024-03-06
31DDoSCritical2024-03-10
41RansomwareCritical2024-03-11
51PhishingMedium2024-03-27
62Brute ForceMedium2024-03-09
72SQL InjectionHigh2024-03-13

Expected output:

client_idthreat_weekweekly_countrunning_total
12024-03-0433
12024-03-1114
12024-03-2515
22024-03-0411
22024-03-1112

2024-03-04 is a Monday, and 2024-03-10 is the Sunday that closes the same week, so client 1 has 3 threats in the week of 2024-03-04. Threat 4 falls on the next Monday and starts a new week. Client 1 has nothing in the week of 2024-03-18, so that week has no row, and the week of 2024-03-25 continues the total from 4 to 5. Client 2's total is separate and starts at 1.

Constraints and Clarifications

  • cyber_threats has between 1 and 10,000 rows.
  • client_id is a positive integer and date_detected is a valid date. No column contains NULLs.
  • Weeks run Monday through Sunday. A threat detected on a Sunday belongs to the week that began six days earlier.
  • Weeks in which a client has no threats are not output; the running total simply carries over to the client's next listed week.
  • A week is identified by its Monday date, so a week that crosses a year boundary stays one week (2025-01-01 belongs to the week of 2024-12-30).
  • Every threat counts once, whatever its type or severity.