Updated Query 4 in java, tested query in SQL