finished part 2 sql code, (pre full bug check)