SQL fixed?

Needs testing