There should be intermediate compilers for structured queries. Lots of database product companies do something like this and none of them do it well.
People should be able to write a bad, messy query and then there should be a compiler that cleans it up and makes it faster and cleaner.