This document contains advanced SQL queries for managing and analyzing fleet operations, addressing vehicle usage, route efficiency, driver activity, and operational metrics.
Description: Retrieve trip details for vehicle ID VH001
during January 2024.
SELECT trip_id, vehicle_id, start_time, end_time, distance
FROM trips
WHERE vehicle_id = 'VH001'
AND start_time >= '2024-01-01' AND end_time <= '2024-01-31';
Description: Calculate total revenue generated by route ID RT001
.
SELECT route_id, SUM(ticket_price * passengers) AS total_revenue
FROM trips
WHERE route_id = 'RT001'
GROUP BY route_id;
Description: Identify vehicles not used in the past 30 days.
SELECT vehicle_id, last_trip_date
FROM vehicles
WHERE last_trip_date < DATEADD(DAY, -30, GETDATE());
Description: List the top 5 routes with the highest passenger count.
SELECT route_id, SUM(passengers) AS total_passengers
FROM trips
GROUP BY route_id
ORDER BY total_passengers DESC
LIMIT 5;
Description: Compute the average travel time between stops for route ID RT001
.
SELECT stop_id, AVG(DATEDIFF(MINUTE, start_time, end_time)) AS avg_travel_time
FROM trips
WHERE route_id = 'RT001'
GROUP BY stop_id;
Description: Identify vehicles where passenger count exceeded capacity.
SELECT trip_id, vehicle_id, capacity, passengers
FROM trips t
JOIN vehicles v ON t.vehicle_id = v.vehicle_id
WHERE passengers > capacity;
Description: List routes that have no recorded trips.
SELECT r.route_id, r.route_name
FROM routes r
LEFT JOIN trips t ON r.route_id = t.route_id
WHERE t.route_id IS NULL;
Description: Find trips on route ID RT001
with delays exceeding 0 minutes.
SELECT trip_id, scheduled_time, actual_time, (actual_time - scheduled_time) AS delay
FROM trips
WHERE route_id = 'RT001' AND (actual_time - scheduled_time) > 0;
Description: Summarize maintenance costs for each vehicle.
SELECT vehicle_id, SUM(maintenance_cost) AS total_cost
FROM maintenance
GROUP BY vehicle_id;
Description: Find the trip with the longest distance covered.
SELECT trip_id, vehicle_id, route_id, MAX(distance) AS longest_trip
FROM trips;
Description: Compute the average passenger count for each route.
SELECT route_id, AVG(passengers) AS avg_passenger_count
FROM trips
GROUP BY route_id;
Description: Identify drivers who have not been assigned trips in the past month.
SELECT driver_id, name
FROM drivers
WHERE driver_id NOT IN (
SELECT DISTINCT driver_id
FROM trips
WHERE start_time >= DATEADD(MONTH, -1, GETDATE())
);
Description: Summarize fuel consumption for each vehicle.
SELECT vehicle_id, SUM(fuel_consumption) AS total_fuel
FROM trips
GROUP BY vehicle_id;
Description: Count trips by date and sort by date.
SELECT DATE(start_time) AS trip_date, COUNT(*) AS trip_count
FROM trips
GROUP BY DATE(start_time)
ORDER BY trip_date;
Description: List the top 5 vehicles with the highest trip counts.
SELECT vehicle_id, COUNT(*) AS trip_count
FROM trips
GROUP BY vehicle_id
ORDER BY trip_count DESC
LIMIT 5;
Description: Calculate revenue loss for routes with delays over 30 minutes.
SELECT route_id, SUM(loss_amount) AS total_loss
FROM (
SELECT route_id, (ticket_price * passengers * 0.1) AS loss_amount
FROM trips
WHERE (actual_time - scheduled_time) > 30
) subquery
GROUP BY route_id;
Description: Find stops with no recorded passenger pickups.
SELECT s.stop_id, s.stop_name
FROM stops s
LEFT JOIN trips t ON s.stop_id = t.stop_id
WHERE t.passengers IS NULL;
Description: Summarize monthly revenue for each route.
SELECT route_id, YEAR(start_time) AS year, MONTH(start_time) AS month, SUM(ticket_price * passengers) AS total_revenue
FROM trips
GROUP BY route_id, YEAR(start_time), MONTH(start_time)
ORDER BY year, month;
Description: Identify the top 5 drivers with the most driving hours.
SELECT driver_id, SUM(DATEDIFF(HOUR, start_time, end_time)) AS total_hours
FROM trips
GROUP BY driver_id
ORDER BY total_hours DESC
LIMIT 5;
Description: Identify vehicles with more than 5 maintenance records.
SELECT vehicle_id, COUNT(*) AS maintenance_count
FROM maintenance
GROUP BY vehicle_id
HAVING COUNT(*) > 5;