How I analyzed Kerala road accident data with SQL window functions

To analyze road accident data with SQL window functions, clean the crash records, count fatal crashes per district, then rank districts with RANK() OVER. A second query compares night and day crashes. On 34,500+ Kerala records this showed Thiruvananthapuram and Ernakulam hold 29.1% of fatal crashes.

By Akhil Shibu, fresher data analyst · Published 2026-11-01

What question was I trying to answer?

Where and when do the deadliest road accidents happen in Kerala? Without a district and time-of-day breakdown, safety effort is spread evenly instead of aimed at the highest-risk places and hours. I used public MoRTH crash data covering all 14 districts.

Step 1: How do you clean accident data before running SQL?

Profile the schema first. In this dataset the main problems were missing values and inconsistent district names. I standardised the names with Pandas and Excel so that one district is never counted as two. Ranking dirty labels gives wrong rankings, so cleaning always comes before the query.

Step 2: How do you rank districts with RANK() OVER?

Aggregate fatal crashes per district, then add a window function. Unlike GROUP BY alone, a window function ranks rows without collapsing them, so each district keeps its own row.

-- Illustrative query (column names simplified)
SELECT district,
       COUNT(*) AS fatal_crashes,
       RANK() OVER (ORDER BY COUNT(*) DESC) AS fatal_rank
FROM crashes
WHERE severity = 'Fatal'
GROUP BY district;

Step 3: How do you compare night and day crashes?

Use a CASE expression to label each crash as night (20:00 to 04:00) or day, then compare the share of crashes that were fatal in each window. Dividing the night share by the day share gives the fatality severity ratio.

SELECT CASE WHEN crash_hour >= 20 OR crash_hour < 4
            THEN 'Night' ELSE 'Day' END AS time_window,
       AVG(CASE WHEN severity = 'Fatal' THEN 1.0 ELSE 0 END) AS fatal_share
FROM crashes
GROUP BY 1;

What did the analysis show?

Thiruvananthapuram and Ernakulam together account for 29.1% of fatal crashes, and crashes between 20:00 and 04:00 have a fatality severity ratio 64.2% higher than the rest of the day. These counts are not adjusted for traffic volume or population, so they show where deaths concentrate, not which roads are riskiest per vehicle.

The district results were then visualised as Power BI heat maps with time-of-day slicers. Read the full write-up in the Kerala road accident analysis case study.

About the author: Akhil Shibu

Akhil Shibu (MCA 2026, CGPA 8.48, University of Kerala) is a Data Analyst, Business Intelligence Analyst, and Business Analyst from Thiruvananthapuram, Kerala, India. Explore his analytics case studies or ATS-friendly single-page resume.

Related

Download resume (PDF)