Skip to content

Instantly share code, notes, and snippets.

@robcowie
Created April 6, 2019 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robcowie/5cfbb4b0bf50e9451b618dbe17a65654 to your computer and use it in GitHub Desktop.
Save robcowie/5cfbb4b0bf50e9451b618dbe17a65654 to your computer and use it in GitHub Desktop.
Notes and tips for using Postgresql

PostgreSQL Notes

Column Names

All identifiers are coerced to lower-case so column names are effectively case-insensitive. To avoid this, quote the identifier with double quotes.

SELECT "myColA" FROM "camelCaseTable";

Analysing Queries

Prepend the following

explain (analyze on, timing on) 

Remove the timing keyword if you don't want to actually execute the query.

Windowing

SELECT
    *,
    LAG(col, 1) OVER w AS previous_col,
    RANK() OVER w AS "rank"
FROM
    table
WINDOW w AS (
    PARTITION BY col1, col2
    ORDER BY col3
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment