title: Are there canonical-forms for database queries?
body: <p>Say I want to make an "Optimized query generator". Basically a SQL query optimizer that is a lot better than what can be put in an SQL server based on time/space limitations. It would take a query and DB stats as input and generate an SQL query tailored for the target system that will quickly optimize to a nearly ideal plan.</p>
  
  <p><strong>How much of SQL would need to be supported?</strong> Is there a subset of SQL that is flexible enough to easily describe most useful queries but enough smaller than full SQL to make it worth trimming it down to? Also <strong>is there a better way to describe queries if you don't need to stick "close to the machine"?</strong></p>
  
  <p>I'm not thinking of a program that you would process existing SQL through but rather a tool for creating new SQL from. <em>It wouldn't actual need to take</em> SQL as input as long as the input language is able to describe the requirements of the query.</p>
  
  <p>I guess another form of the question would be: are their any parts of SQL that are only there for performance and never improve readability/understandability?</p>
  
  <hr>
  
  <p>As pointed out by someone doing this would require "tons of product-specific knowledge" and that (e.g. nested sub queries vs whatever, what kind of indexes should be used, that sort of thing) is exactly what the tool would be intended to encapsulate so that the user doesn't need to learn that knowledge.</p>
  
  <hr>
  
  <p>note: <strong>I am not interested in generating actual query plans as that is the DBMS's job</strong> and can't be done from SQL anyway. I'm interested in a system that can automate the job of making good SQL for a given DBMS from an input that doesn't need to be tuned for that DBMS.</p>