The SQL generated code is 30% of the times wrong

I was working intensively with gpt 3.5-turbo for SQL generation, for a Postgresql database.
The generated sql has many beginners mistakes such as:

“WINDOW functions are not allowed inside WHERE clause”.
“aggregate function is not allowed inside window”.
“column x does not exist”.

These errors have nothing to do with the prompt, the generated sql is right in the sense of what it does, but have “syntax” errors like this, that many beginners do.

Interestingly, when i show the generated wrong code to gpt on openai.com, and I ask it to fix it, he recognize this error right away, and fix it.

Is this has to do with the SQL version? is there a way to fix this?