AI Prompt: Postgres SQL Style Guide
How to use
Copy the prompt to a file in your repo.
Use the "include file" feature from your AI tool to include the prompt when chatting with your AI assistant. For example, with GitHub Copilot, use #<filename>, in Cursor, use @Files, and in Zed, use /file.
You can also load the prompt directly into your IDE via the following links:
Prompt
1# Postgres SQL Style Guide23## General45- Use lowercase for SQL reserved words to maintain consistency and readability.6- Employ consistent, descriptive identifiers for tables, columns, and other database objects.7- Use white space and indentation to enhance the readability of your code.8- Store dates in ISO 8601 format (`yyyy-mm-ddThh:mm:ss.sssss`).9- Include comments for complex logic, using '/* ... */' for block comments and '--' for line comments.1011## Naming Conventions1213- Avoid SQL reserved words and ensure names are unique and under 63 characters.14- Use snake_case for tables and columns.15- Prefer plurals for table names16- Prefer singular names for columns.1718## Tables1920- Avoid prefixes like 'tbl_' and ensure no table name matches any of its column names.21- Always add an `id` column of type `identity generated always` unless otherwise specified.22- Create all tables in the `public` schema unless otherwise specified.23- Always add the schema to SQL queries for clarity.24- Always add a comment to describe what the table does. The comment can be up to 1024 characters.2526## Columns2728- Use singular names and avoid generic names like 'id'.29- For references to foreign tables, use the singular of the table name with the `_id` suffix. For example `user_id` to reference the `users` table30- Always use lowercase except in cases involving acronyms or when readability would be enhanced by an exception.3132#### Examples:3334```sql35create table books (36 id bigint generated always as identity primary key,37 title text not null,38 author_id bigint references authors (id)39);40comment on table books is 'A list of all the books in the library.';41```424344## Queries4546- When the query is shorter keep it on just a few lines. As it gets larger start adding newlines for readability47- Add spaces for readability.4849Smaller queries:505152```sql53select *54from employees55where end_date is null;5657update employees58set end_date = '2023-12-31'59where employee_id = 1001;60```6162Larger queries:6364```sql65select66 first_name,67 last_name68from69 employees70where71 start_date between '2021-01-01' and '2021-12-31'72and73 status = 'employed';74```757677### Joins and Subqueries7879- Format joins and subqueries for clarity, aligning them with related SQL clauses.80- Prefer full table names when referencing tables. This helps for readability.8182```sql83select84 employees.employee_name,85 departments.department_name86from87 employees88join89 departments on employees.department_id = departments.department_id90where91 employees.start_date > '2022-01-01';92```9394## Aliases9596- Use meaningful aliases that reflect the data or transformation applied, and always include the 'as' keyword for clarity.9798```sql99select count(*) as total_employees100from employees101where end_date is null;102```103104105## Complex queries and CTEs106107- If a query is extremely complex, prefer a CTE.108- Make sure the CTE is clear and linear. Prefer readability over performance.109- Add comments to each block.110111```sql112with department_employees as (113 -- Get all employees and their departments114 select115 employees.department_id,116 employees.first_name,117 employees.last_name,118 departments.department_name119 from120 employees121 join122 departments on employees.department_id = departments.department_id123),124employee_counts as (125 -- Count how many employees in each department126 select127 department_name,128 count(*) as num_employees129 from130 department_employees131 group by132 department_name133)134select135 department_name,136 num_employees137from138 employee_counts139order by140 department_name;141```