diff --git a/README.md b/README.md index 2cbae84..d54131e 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Timestamp Functions](postgres/timestamp-functions.md) - [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md) - [Turning Timing On](postgres/turning-timing-on.md) +- [Use Argument Indexes](postgres/use-argument-indexes.md) - [Using Intervals To Offset Time](postgres/using-intervals-to-offset-time.md) - [Who Is The Current User](postgres/who-is-the-current-user.md) - [Word Count for a Column](postgres/word-count-for-a-column.md) diff --git a/postgres/use-argument-indexes.md b/postgres/use-argument-indexes.md new file mode 100644 index 0000000..aa8d54a --- /dev/null +++ b/postgres/use-argument-indexes.md @@ -0,0 +1,24 @@ +# Use Argument Indexes + +In Postgres, each of the arguments you specify in a `select` statement has a +1-based index tied to it. You can use these indexes in the `order by` and +`group by` parts of the statement. + +Instead of writing + +```sql +select id, updated_at from posts order by updated_at; +``` + +you can write + +```sql +select id, updated_at from posts order by 2; +``` + +If you want to group by a table's `type` and then order by the counts from +highest to lowest, you can do the following + +```sql +select type, count(*) from transaction group by 1 order by 2 desc; +```