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.

You can also load the prompt directly into your IDE via the following links:

Prompt#

1
# Postgres SQL Style Guide
2
3
## General
4
5
- 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.
10
11
## Naming Conventions
12
13
- 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 names
16
- Prefer singular names for columns.
17
18
## Tables
19
20
- 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.
25
26
## Columns
27
28
- 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` table
30
- Always use lowercase except in cases involving acronyms or when readability would be enhanced by an exception.
31
32
#### Examples:
33
34
```sql
35
create table books (
36
id bigint generated always as identity primary key,
37
title text not null,
38
author_id bigint references authors (id)
39
);
40
comment on table books is 'A list of all the books in the library.';
41
```
42
43
## Queries
44
45
- When the query is shorter keep it on just a few lines. As it gets larger start adding newlines for readability
46
- Add spaces for readability.
47
48
Smaller queries:
49
50
```sql
51
select *
52
from employees
53
where end_date is null;
54
55
update employees
56
set end_date = '2023-12-31'
57
where employee_id = 1001;
58
```
59
60
Larger queries:
61
62
```sql
63
select
64
first_name,
65
last_name
66
from employees
67
where start_date between '2021-01-01' and '2021-12-31' and status = 'employed';
68
```
69
70
### Joins and Subqueries
71
72
- Format joins and subqueries for clarity, aligning them with related SQL clauses.
73
- Prefer full table names when referencing tables. This helps for readability.
74
75
```sql
76
select
77
employees.employee_name,
78
departments.department_name
79
from
80
employees
81
join departments on employees.department_id = departments.department_id
82
where employees.start_date > '2022-01-01';
83
```
84
85
## Aliases
86
87
- Use meaningful aliases that reflect the data or transformation applied, and always include the 'as' keyword for clarity.
88
89
```sql
90
select count(*) as total_employees
91
from employees
92
where end_date is null;
93
```
94
95
## Complex queries and CTEs
96
97
- If a query is extremely complex, prefer a CTE.
98
- Make sure the CTE is clear and linear. Prefer readability over performance.
99
- Add comments to each block.
100
101
```sql
102
with
103
department_employees as (
104
-- Get all employees and their departments
105
select
106
employees.department_id,
107
employees.first_name,
108
employees.last_name,
109
departments.department_name
110
from
111
employees
112
join departments on employees.department_id = departments.department_id
113
),
114
employee_counts as (
115
-- Count how many employees in each department
116
select
117
department_name,
118
count(*) as num_employees
119
from department_employees
120
group by department_name
121
)
122
select
123
department_name,
124
num_employees
125
from employee_counts
126
order by department_name;
127
```