Write a query to detect accounts with more than 5 failed login attempts within a 10-minute window.
-
SQL
- Write a SQL query to find the second highest salary from an Employees table without using LIMIT or TOP.
- Write a query to find all customers who placed orders in every month of 2025.
- Write a query to identify duplicate rows in a Transactions table based on customer_id, amount, and date.
- Write a query using GROUP BY and HAVING to find departments with more than 10 employees earning above the department average salary.
- Write a query to return the top 3 selling products per category using a subquery (no window functions allowed).
- Write a query to calculate the running total of daily revenue for a store using a self-join.
- Write a query to find the average order value per customer segment, excluding cancelled orders.
- Write a query to find employees who earn more than their direct manager, using a self-join on an Employees table.
- Write a query to list all customers who have not placed any order in the last 90 days.
- Write a query to compute the percentage of total revenue contributed by each product category.
- Write a query to find the first purchase date and last purchase date for every customer.
- Write a query to find the three most recent orders for each customer.
- Write a query that returns customers whose total spend is above the 90th percentile of all customers.
- Write a query to find customers who bought Product A but never bought Product B.
- Write a query to count how many orders each customer placed and rank customers by order count within their city.
- Write a query to identify the busiest hour of the day for support ticket creation using a Tickets table.
- Write a query to find the top 5 referral sources by conversion rate from a Signups table.
- Write a query using EXISTS to find suppliers who have never had a late delivery.
- Write a query to detect gaps in a sequential invoice_number column.
- Write a query to find all products whose price increased in the last price update compared to the previous one.
- Write a query to calculate month-over-month growth rate in active users from a DailyActiveUsers table.
- Write a query to return the department with the highest average tenure of employees.
- Write a query to find all orders placed on weekends versus weekdays and compare average order value.
- Write a query to find customers who upgraded their subscription plan more than once in the last year.
- Write a query to return the top 10% of sales reps by revenue generated in Q1.
- Write a query using a CTE to calculate a 7-day moving average of website visits.
- Write a query to find all patients who missed at least two scheduled appointments in the past year.
- Write a query to identify employees who have never taken a single day of leave in the last two years.
- Write a query to compute year-over-year revenue comparison for each region using self-joins.
- Write a query to find the longest streak of consecutive days a user logged into an app.
- Write a query to identify vendors whose invoice amounts differ by more than 15% from their historical average.
- Write a query to find all students who scored above the class average in more than 3 subjects.
- Write a query to detect accounts with more than 5 failed login attempts within a 10-minute window.
- Write a query to find the second most frequently ordered item for each customer.
- Write a query to identify products frequently bought together, based on an OrderItems table.
- Write a query to find the average time between a support ticket being opened and first responded to, grouped by priority.
- Write a query to find the top-selling product in each store location for the current quarter.
- Write a query to list all foreign keys referencing a given table in a schema (engine-specific system tables allowed).
- Write a query to compute the cumulative distinct count of new customers acquired each month.
Answer
Use a self-join to pair each failed login with other failed logins from the same account within 10 minutes, then flag accounts with 5 or more in that window.
Solution
SELECT DISTINCT a.account_id
FROM LoginAttempts a
JOIN LoginAttempts b
ON a.account_id = b.account_id
AND b.attempt_time BETWEEN a.attempt_time AND a.attempt_time + INTERVAL '10 minutes'
AND a.status = 'failed' AND b.status = 'failed'
GROUP BY a.account_id, a.attempt_time
HAVING COUNT(*) >= 5;Step by Step
- The self-join pairs every failed attempt (
a) with every other failed attempt (b) from the same account occurring within the next 10 minutes. - Grouping by
account_id, attempt_time(using eacharow as a rolling window start) and counting matches tells you how many failed attempts cluster around that starting point. HAVING COUNT(*) >= 5flags any 10-minute window containing 5 or more failed attempts.
Alternative: Window Function Approach (Simpler, More Scalable)
WITH failed AS (
SELECT account_id, attempt_time,
COUNT(*) OVER (
PARTITION BY account_id
ORDER BY attempt_time
RANGE BETWEEN INTERVAL '10 minutes' PRECEDING AND CURRENT ROW
) AS attempts_in_window
FROM LoginAttempts
WHERE status = 'failed'
)
SELECT DISTINCT account_id FROM failed WHERE attempts_in_window >= 5;A RANGE-based window function directly counts failed attempts within a trailing 10-minute range per account, without the self-join's combinatorial row explosion - meaningfully more efficient on large login logs.
Key Exam Traps
- A self-join on a time range creates a row for every pair within the window, which can be very expensive on large datasets - flagging the window-function alternative as the production-scale answer is important here.
- "Within a 10-minute window" is a sliding window, not fixed 10-minute buckets - 5 failed attempts spanning from 9:58 to 10:07 should count, even though they cross a fixed clock-hour boundary; a naive
GROUP BYon truncated 10-minute buckets would miss exactly this kind of case. - Successful login resets nothing in this query - if the real security requirement is "5 failed attempts without an intervening success," that's a materially different (and more complex) condition than this time-window-only check.