SQL seems to work now

Need to test it further.