Lets say I have a table with 2 columns:
- city
- name (of a person).
I also have a Java "city" object which contains:
- city name
- a list of all the people in that city
So now I have two options to get the data:
- First use DISTINCT to get a list of all the cities. Then, for each city, query the database again, using WHERE to get only records where the person lives in that city. Then I can store this in a City object.
- Get a list of all the data, using ORDER BY to order by the city name. Then loop through all the records and start storing them in City objects. When I detect that the city name changes then I can create a new City object and store the records in that.
Which of these methods is faster / better practice? Or is there some better way of getting this information than these two methods? I am using Oracle database.
Mapin the Java code, eliminating the need forORDER BY.DISTINCT,WHERE, andORDER BY, indicating that question is about SQL, so saying "I would recommend making this kind of stuff with SQL" is kind of redundant. --- Also, OP already talks about 1-query solution in #2, so saying "It seems feasible with one query" is kind of redundant. --- Your comment is redundant, and doesn't eveen attempt to answer the question: Which is better?