This sort of thing should be done in SQL as opposed to java
Otherwise it will still take the time to send the data over which costs bandwidth and time as opposed to only getting what is needed