Everything from top of the Wbsp file (or from <!--WB_BeginTemplate--> comment, if it exists) to <!--WB_BeginDetail--> comment is considered to be a Top section. It will be processed by WBSP engine and sent to client, before WBSP processes the Detail section . It can contain DB related functions , but it will ignore WB_StartRec variable (i.e. for all $wbf functions it will return the value of the first record, and not the record at the position defined in WB_StartRec ).
Here's an example (top section is marked blue)