Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active December 17, 2019 12:17
Show Gist options
  • Save vielhuber/826c6f3b9ef670a0d3e54a1d2a96a06d to your computer and use it in GitHub Desktop.
Save vielhuber/826c6f3b9ef670a0d3e54a1d2a96a06d to your computer and use it in GitHub Desktop.
mysql / postgresql: get total count 3 different alternatives with window function in one query #sql
# 2 queries (best)
SELECT col1, col2, col3 FROM bar ORDER BY col4 LIMIT 10;
SELECT COUNT(*) FROM bar;
# postgres
SELECT col1, col2, col3, count(*) OVER() AS full_count FROM bar ORDER BY col4 LIMIT 10;
# mysql (deprecated and slow!)
SELECT SQL_CALC_FOUND_ROWS col1, col2, col3 FROM bar ORDER BY col4 LIMIT 10;
SELECT FOUND_ROWS();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment