runtime
68 / 88

lesson 68 Performance & query plans · advanced

SARGable ranges, not speed commandments

Compare range filters and functions on indexed columns.

the idea

A predicate is SARGable when the database can use an index to search it: the indexed column appears bare, compared with a value or range. Wrapping the column in a function, such as date(scheduled_departure) = '2025-01-15', hides it from a plain index; the equivalent half-open range keeps it searchable.

Dataset: U.S. Flights

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

flights25 columns · 124,460 rows
flight_idnumber · unique idflight_datedate · links to calendarairline_codetext · links to airlinesflight_numbernumbertail_numbertextorigintext · links to airportsdestinationtext · links to airportsroute_idnumber · links to routesscheduled_departuredate & timeactual_departuredate & timedeparture_delaynumbertaxi_out_minutesnumberscheduled_arrivaldate & timeactual_arrivaldate & timearrival_delaynumberair_time_minutesnumberdistance_milesnumbercancellednumbercancellation_codetext · links to cancellation_reasonsdivertednumbercarrier_delaynumberweather_delaynumbernas_delaynumbersecurity_delaynumberlate_aircraft_delaynumber
12025-01-01AA2158N960NNSMFORD50422025-01-01 00:252025-01-01 00:3712102025-01-01 06:342025-01-01 06:18-1620217810NULL0NULLNULLNULLNULLNULL
22025-01-01AS150N524ASANCLAX902025-01-01 00:442025-01-01 00:42-2102025-01-01 07:082025-01-01 06:44-2428123450NULL0NULLNULLNULLNULLNULL
32025-01-01AA2319N861NNPHXORD40782025-01-01 00:452025-01-01 00:39-6172025-01-01 05:162025-01-01 04:44-3216014400NULL0NULLNULLNULLNULLNULL
42025-01-01UA2232N24516SFOORD48202025-01-01 00:592025-01-01 00:56-3312025-01-01 07:262025-01-01 07:23-321418460NULL0NULLNULLNULLNULLNULL
52025-01-01AA575N163AADFWCLT14572025-01-01 05:002025-01-01 04:55-5232025-01-01 08:322025-01-01 08:14-181059360NULL0NULLNULLNULLNULLNULL
62025-01-01AA1196N338STEWRMIA18082025-01-01 05:002025-01-01 04:53-7222025-01-01 08:202025-01-01 08:10-1015110850NULL0NULLNULLNULLNULLNULL
flight_idnumber · unique id
1
flight_datedate · links to calendar
2025-01-01
airline_codetext · links to airlines
AA
flight_numbernumber
2158
tail_numbertext
N960NN
origintext · links to airports
SMF
destinationtext · links to airports
ORD
route_idnumber · links to routes
5042
scheduled_departuredate & time
2025-01-01 00:25
actual_departuredate & time
2025-01-01 00:37
departure_delaynumber
12
taxi_out_minutesnumber
10
scheduled_arrivaldate & time
2025-01-01 06:34
actual_arrivaldate & time
2025-01-01 06:18
arrival_delaynumber
-16
air_time_minutesnumber
202
distance_milesnumber
1781
cancellednumber
0
cancellation_codetext · links to cancellation_reasons
NULL
divertednumber
0
carrier_delaynumber
NULL
weather_delaynumber
NULL
nas_delaynumber
NULL
security_delaynumber
NULL
late_aircraft_delaynumber
NULL
flight_idnumber · unique id
2
flight_datedate · links to calendar
2025-01-01
airline_codetext · links to airlines
AS
flight_numbernumber
150
tail_numbertext
N524AS
origintext · links to airports
ANC
destinationtext · links to airports
LAX
route_idnumber · links to routes
90
scheduled_departuredate & time
2025-01-01 00:44
actual_departuredate & time
2025-01-01 00:42
departure_delaynumber
-2
taxi_out_minutesnumber
10
scheduled_arrivaldate & time
2025-01-01 07:08
actual_arrivaldate & time
2025-01-01 06:44
arrival_delaynumber
-24
air_time_minutesnumber
281
distance_milesnumber
2345
cancellednumber
0
cancellation_codetext · links to cancellation_reasons
NULL
divertednumber
0
carrier_delaynumber
NULL
weather_delaynumber
NULL
nas_delaynumber
NULL
security_delaynumber
NULL
late_aircraft_delaynumber
NULL
flight_idnumber · unique id
3
flight_datedate · links to calendar
2025-01-01
airline_codetext · links to airlines
AA
flight_numbernumber
2319
tail_numbertext
N861NN
origintext · links to airports
PHX
destinationtext · links to airports
ORD
route_idnumber · links to routes
4078
scheduled_departuredate & time
2025-01-01 00:45
actual_departuredate & time
2025-01-01 00:39
departure_delaynumber
-6
taxi_out_minutesnumber
17
scheduled_arrivaldate & time
2025-01-01 05:16
actual_arrivaldate & time
2025-01-01 04:44
arrival_delaynumber
-32
air_time_minutesnumber
160
distance_milesnumber
1440
cancellednumber
0
cancellation_codetext · links to cancellation_reasons
NULL
divertednumber
0
carrier_delaynumber
NULL
weather_delaynumber
NULL
nas_delaynumber
NULL
security_delaynumber
NULL
late_aircraft_delaynumber
NULL
flight_idnumber · unique id
4
flight_datedate · links to calendar
2025-01-01
airline_codetext · links to airlines
UA
flight_numbernumber
2232
tail_numbertext
N24516
origintext · links to airports
SFO
destinationtext · links to airports
ORD
route_idnumber · links to routes
4820
scheduled_departuredate & time
2025-01-01 00:59
actual_departuredate & time
2025-01-01 00:56
departure_delaynumber
-3
taxi_out_minutesnumber
31
scheduled_arrivaldate & time
2025-01-01 07:26
actual_arrivaldate & time
2025-01-01 07:23
arrival_delaynumber
-3
air_time_minutesnumber
214
distance_milesnumber
1846
cancellednumber
0
cancellation_codetext · links to cancellation_reasons
NULL
divertednumber
0
carrier_delaynumber
NULL
weather_delaynumber
NULL
nas_delaynumber
NULL
security_delaynumber
NULL
late_aircraft_delaynumber
NULL
flight_idnumber · unique id
5
flight_datedate · links to calendar
2025-01-01
airline_codetext · links to airlines
AA
flight_numbernumber
575
tail_numbertext
N163AA
origintext · links to airports
DFW
destinationtext · links to airports
CLT
route_idnumber · links to routes
1457
scheduled_departuredate & time
2025-01-01 05:00
actual_departuredate & time
2025-01-01 04:55
departure_delaynumber
-5
taxi_out_minutesnumber
23
scheduled_arrivaldate & time
2025-01-01 08:32
actual_arrivaldate & time
2025-01-01 08:14
arrival_delaynumber
-18
air_time_minutesnumber
105
distance_milesnumber
936
cancellednumber
0
cancellation_codetext · links to cancellation_reasons
NULL
divertednumber
0
carrier_delaynumber
NULL
weather_delaynumber
NULL
nas_delaynumber
NULL
security_delaynumber
NULL
late_aircraft_delaynumber
NULL
flight_idnumber · unique id
6
flight_datedate · links to calendar
2025-01-01
airline_codetext · links to airlines
AA
flight_numbernumber
1196
tail_numbertext
N338ST
origintext · links to airports
EWR
destinationtext · links to airports
MIA
route_idnumber · links to routes
1808
scheduled_departuredate & time
2025-01-01 05:00
actual_departuredate & time
2025-01-01 04:53
departure_delaynumber
-7
taxi_out_minutesnumber
22
scheduled_arrivaldate & time
2025-01-01 08:20
actual_arrivaldate & time
2025-01-01 08:10
arrival_delaynumber
-10
air_time_minutesnumber
151
distance_milesnumber
1085
cancellednumber
0
cancellation_codetext · links to cancellation_reasons
NULL
divertednumber
0
carrier_delaynumber
NULL
weather_delaynumber
NULL
nas_delaynumber
NULL
security_delaynumber
NULL
late_aircraft_delaynumber
NULL

first 6 of 124,460 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Wrap the column in a function. date(scheduled_departure) hides the indexed column from a plain index search, so the plan often still scans despite idx_flight_time.the pattern

    example.sql
    CREATE INDEX idx_flight_time ON flights(scheduled_departure);
    EXPLAIN QUERY PLAN
    SELECT flight_id
    FROM flights
    WHERE date(scheduled_departure) = '2025-01-15';
  2. Filter with a searchable range. Leaving scheduled_departure bare in a half-open >= / < range is the SARGable form—the plan can search the index for January 15th flights.

    step-2.sql
    CREATE INDEX idx_flight_time ON flights(scheduled_departure);
    EXPLAIN QUERY PLAN
    SELECT flight_id
    FROM flights
    WHERE scheduled_departure >= '2025-01-15 00:00'
      AND scheduled_departure < '2025-01-16 00:00';

common mistake

SARGability is about access paths, not a rule that functions are slow. On a tiny table a scan can be faster; on 124,460 flights the difference is visible in the plan.

challenge 1

Create index idx_flight_time on flights(scheduled_departure), then return flight_id and scheduled_departure for flights scheduled on 15 January 2025 using a half-open range the index can search.

what to return

Columns
flight_idscheduled_departure in this order
Rows
any order
Also checked
TEMP / table changes from your script
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
SARGable ranges, not speed commandments — Performance & query plans · Runtime