It’s almost impossible to write procedural queries that always perform, that is a really hard task. That’s why we use a declarative query language, I tell the database what data I need, and the database optimizer will determine the best performing algorithm to fetch the data based on all dynamic statistics it has.
Don’t underestimate how much hard work the database optimizer takes care of, I’m glad I don’t have to program all of that myself.
Better get used to this way of working, it resembles pretty much how AI assists us.