Window Fnctions:
*Adding the total rows selected to every row
SELECT your_columns, COUNT(*) OVER() as Ttl_Rows FROM your_data_set
id | name | Ttl_Rows | —— | —— | —— | 1 | example | 5 | 2 | foo | 5| 3 | bar | 5| 4 | baz | 5| 5 | quux | 5 |
Instead of using two queries to get a count then the line, you can use an aggregate as a window function and use the full result set as the window.
This can be used as a base for further calculation without the complexity of extra self joins.