Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a SQL style guide #555

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _articles/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: "Reporting Queries"
description: "Queries to run in the Rails console for common reporting questions"
layout: article
category: "Reporting"
subcategory: "Queries"
---

## Query timeout
Expand Down
67 changes: 67 additions & 0 deletions _articles/sql-style-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: "SQL Style Guide"
description: "Conventions for formatting SQL queries"
layout: article
category: "Reporting"
subcategory: "Queries"
---

This is a guide for writing "raw" SQL queries, used in ETLs and reports. It does
not apply to queries that are auto-generated via ORMs like ActiveRecord.

Currently we do not have any automated linting/enforcement of this guide.

## Keywords

Capitalize keywords

```sql
-- DO
SELECT 1
FROM mytable;

-- DO NOT
select 1
from mytable;
```

## Commas

Prefer leading commas (they usually make for less diff noise when adding or removing items in a list)

```sql
-- DO
SELECT
col1
, col2
, col3
FROM mytable;

-- DO NOT
SELECT
col1,
col2,
col3
FROM mytablel
```

## `SELECT *`

Avoid `SELECT *` and select specific fields when possible. Using explicit columns
makes analysis of queries easier.

(This rule applies most to checked-in code. For running test queries interactively,
`SELECT *` is totally fine).

```sql
-- DO
SELECT
col1
, col2
, col3
FROM mytable;

-- DO NOT
SELECT *
FROM mytable;
```
Loading