In a nutshell
SQL is declarative: you describe the result you want, and the database's optimizer decides how to get it. It compiles your query into a plan — a tree of operators like scan, join, filter, and sort — and runs it bottom-up, each operator feeding rows to its parent. The fewer rows each step touches, the cheaper the query.