Order and Group
Explore how to use Ecto's order_by and group_by keywords to sort and aggregate query results in Elixir. Understand ordering by multiple columns, controlling null placements, and applying the having clause to filter grouped data. This lesson helps you write efficient database queries that organize and summarize your data accurately.
SQL allows us to be specific about how our results are ordered. We can also group result rows, which comes in handy when getting counts in our results. The order by and group by expressions in SQL are available in Ecto via the order_by and group_by keywords. Let’s see how those work.
The order_by expression
If we wanted a list of all of the artists in alphabetical order, we could use order_by, like this:
The order_by expression returns the results in ascending order by default, but we can change this with the desc: keyword.
The order_by expression with multiple columns
We can also order by multiple columns by providing a list of column names to ...