Don’t Treat SQL Like a Programming or Scripting Language.

In a programming language like C# or Java, you tell the computer what to do, in order.

Get the customers from California, then get their invoices, then sum the total.

SQL, on the other hand, is a declarative language where you declare the shape of your result set:

Get the total sales from Californian customers.

You’re declaring the output that you want, not the methods the database server uses to build it. Oh sure, you CAN use SQL to declare the shape of your query plan, but generally that leads to heartbreak and despair because:

You don’t know the database engine’s internals