Getting Started

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.

Prompt


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