Order
DISTINCT¶
The DISTINCT keyword can be used to filter a result set for only unique values.
There are situations when we are using JOINs or projection where duplicate copies of the same row can end up in our result set. The DISTINCT keyword will eliminate these dupes from the result set.
String query = "SELECT DISTINCT f FROM Film f WHERE f.id < 10";
List<Film> results = em.createQuery(query, Film.class).getResultList();
ORDER BY¶
Select statements can be modified with an ORDER BY clause. ORDER BY is the only way to enforce a sequence with our result sets.
String query = "SELECT f FROM Film f ORDER BY f.title";
List<Film> results = em.createQuery(query, Film.class).getResultList();
We can apply ASC or DESC to the columns in the ORDER BY clause to order them in ascending or descending order.
String query = "SELECT f FROM Film f ORDER BY f.title DESC";
List<Film> results = em.createQuery(query, Film.class).getResultList();
Drill¶
- Create a new class
IntermediateJPQLClientin yourcom.example.jpavideostore.clientpackage.- In a
main, retrieve a list ofStaffmembers sorted in alphabetical order bylastName.
- In a