Table Schema
Inspect
Table
| Column | Type |
|---|---|
| query | varchar |
| search_date | date |
Find queries whose frequency increased compared to the previous day.
Table Schema
| Column | Type |
|---|---|
| query | varchar |
| search_date | date |
Sample Data
| query | search_date |
|---|---|
| AI | 2024-01-01 |
| AI | 2024-01-02 |
| AI | 2024-01-02 |
| SQL | 2024-01-01 |
| SQL | 2024-01-02 |
| Python | 2024-01-02 |
| Python | 2024-01-02 |
| query |
|---|
| AI |
SQL Editor
| query |
|---|
| AI |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
WITH daily_counts AS (
SELECT query, search_date, COUNT(*) AS frequency
FROM search_logs
GROUP BY query, search_date
),
with_previous AS (
SELECT
query,
search_date,
frequency,
LAG(frequency) OVER (PARTITION BY query ORDER BY search_date) AS previous_frequency
FROM daily_counts
)
SELECT DISTINCT query
FROM with_previous
WHERE frequency > previous_frequency;Explanation
Aggregate daily frequencies.
Compare each day with LAG.
Select queries with an increase over the previous day.
Related Questions