Getting Started

API 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.

Prompt


_141
# Postgres SQL Style Guide
_141
_141
## General
_141
_141
- Use lowercase for SQL reserved words to maintain consistency and readability.
_141
- Employ consistent, descriptive identifiers for tables, columns, and other database objects.
_141
- Use white space and indentation to enhance the readability of your code.
_141
- Store dates in ISO 8601 format (`yyyy-mm-ddThh:mm:ss.sssss`).
_141
- Include comments for complex logic, using '/* ... */' for block comments and '--' for line comments.
_141
_141
## Naming Conventions
_141
_141
- Avoid SQL reserved words and ensure names are unique and under 63 characters.
_141
- Use snake_case for tables and columns.
_141
- Prefer plurals for table names
_141
- Prefer singular names for columns.
_141
_141
## Tables
_141
_141
- Avoid prefixes like 'tbl_' and ensure no table name matches any of its column names.
_141
- Always add an `id` column of type `identity generated always` unless otherwise specified.
_141
- Create all tables in the `public` schema unless otherwise specified.
_141
- Always add the schema to SQL queries for clarity.
_141
- Always add a comment to describe what the table does. The comment can be up to 1024 characters.
_141
_141
## Columns
_141
_141
- Use singular names and avoid generic names like 'id'.
_141
- For references to foreign tables, use the singular of the table name with the `_id` suffix. For example `user_id` to reference the `users` table
_141
- Always use lowercase except in cases involving acronyms or when readability would be enhanced by an exception.
_141
_141
#### Examples:
_141
_141
```sql
_141
create table books (
_141
id bigint generated always as identity primary key,
_141
title text not null,
_141
author_id bigint references authors (id)
_141
);
_141
comment on table books is 'A list of all the books in the library.';
_141
```
_141
_141
_141
## Queries
_141
_141
- When the query is shorter keep it on just a few lines. As it gets larger start adding newlines for readability
_141
- Add spaces for readability.
_141
_141
Smaller queries:
_141
_141
_141
```sql
_141
select *
_141
from employees
_141
where end_date is null;
_141
_141
update employees
_141
set end_date = '2023-12-31'
_141
where employee_id = 1001;
_141
```
_141
_141
Larger queries:
_141
_141
```sql
_141
select
_141
first_name,
_141
last_name
_141
from
_141
employees
_141
where
_141
start_date between '2021-01-01' and '2021-12-31'
_141
and
_141
status = 'employed';
_141
```
_141
_141
_141
### Joins and Subqueries
_141
_141
- Format joins and subqueries for clarity, aligning them with related SQL clauses.
_141
- Prefer full table names when referencing tables. This helps for readability.
_141
_141
```sql
_141
select
_141
employees.employee_name,
_141
departments.department_name
_141
from
_141
employees
_141
join
_141
departments on employees.department_id = departments.department_id
_141
where
_141
employees.start_date > '2022-01-01';
_141
```
_141
_141
## Aliases
_141
_141
- Use meaningful aliases that reflect the data or transformation applied, and always include the 'as' keyword for clarity.
_141
_141
```sql
_141
select count(*) as total_employees
_141
from employees
_141
where end_date is null;
_141
```
_141
_141
_141
## Complex queries and CTEs
_141
_141
- If a query is extremely complex, prefer a CTE.
_141
- Make sure the CTE is clear and linear. Prefer readability over performance.
_141
- Add comments to each block.
_141
_141
```sql
_141
with department_employees as (
_141
-- Get all employees and their departments
_141
select
_141
employees.department_id,
_141
employees.first_name,
_141
employees.last_name,
_141
departments.department_name
_141
from
_141
employees
_141
join
_141
departments on employees.department_id = departments.department_id
_141
),
_141
employee_counts as (
_141
-- Count how many employees in each department
_141
select
_141
department_name,
_141
count(*) as num_employees
_141
from
_141
department_employees
_141
group by
_141
department_name
_141
)
_141
select
_141
department_name,
_141
num_employees
_141
from
_141
employee_counts
_141
order by
_141
department_name;
_141
```