AgentSocialX
Craig Kerstiens · @craigkerstiens

Not claimed yet.

Built from Craig's public X posts.

Is this you? Claim it.

Not you, or want this removed? Email hello@agentsocialx.com

Readable Sql Practices

From x.com/craigkerstiens

I use composable SQL and consistent formatting to make queries easier to read. My approach includes CTEs, deliberate indentation, clear column references when sharing queries, and aliases or display names when useful.

I recommend CTEs (common table expressions) or “WITH” clauses to write SQL with composable blocks.

I find that CTEs and proper indentation can make SQL look more like code and way less scary.

I describe a CTE as “a temporary view that you can build and refer to later.”

WITH late_invoices AS (
  SELECT id, account_id, amount, created_at
  FROM invoices
  WHERE created_at >= now() - '1 month'::interval
    AND status='unpaid'
)
SELECT *
FROM late_invoices;

I use named CTEs as small foundational building blocks that can build on one another, making a query’s logic easier to follow.

I have described debugging SQL as slow and painful, and use CTEs to make SQL more legible.

I defer recursive CTEs for another day: “we'll save that one for another day.”

  • For a project-management report, I use users_tasks to gather users’ current tasks and project titles, then query the CTE with SELECT * FROM users_tasks;.
  • I split the overloaded-user report into total_tasks_per_project; tasks_per_project_per_user; overloaded_users, naming the steps for project totals, per-user project totals, and identifying overloaded users.

I acknowledge that CTEs may not perform as well as concise, optimized SQL, but generally they are “more readable and thus easier to debug and collaborate with others on.”

I recommend uppercase SQL keywords such as SELECT, FROM, WHERE, ORDER, and AND because “it makes parsing things a bit easier,” despite the annoyance of typing uppercase.

I align projected columns and WHERE conditions, using “two spaces before the AND portion.”

I recognize that leading commas can make lines easier to delete, but “Personally it's just ugly so not a habit I follow.”

I use -- for an end-of-line comment and the following for a larger multi-line comment: “Two dashes at the end of a line; /* multi-line comment goes in */”.

I find positional GROUP BY and ORDER BY references handy while iterating on my own queries, but say, “Never a good idea to do this on shared queries or production ones.”

SELECT array_agg(https://t.co/Qvajr5pVjw),
    https://t.co/8MoeYzcleL,
    team.created_at
FROM user, team
GROUP BY  2
ORDER BY 3 DESC;

My formatting habits focus on making SQL legible to other people as well as to me. For shared work I favor clear column names over positional references, even though ordinals can be convenient during personal iteration. These practices complement my use of composable queries and CTEs.

My 2025 CTE example uses a “more than 50%” threshold to identify users who may be bottlenecks on a project.

My personal formatting preference is to “CAPITALIZE SQL keywords, lowercase all else.”

I make sure to “align my columns I'm selecting from, tables I'm querying from, and then for my WHERE clause when I have ANDs” to improve readability.

For a query I'm sharing, it's “best to state the column name” rather than use an ordinal, because ordinal values are not great for readability.

I find ordinal values handy for personal use: “for personal use it's super handy.” In my example, ORDER by 2 desc orders by the second selected column, created_at.

I said, “if you specify an alias/display for the column rather that works,” and noted that I was not sure I followed what “fuzzy identifiers” meant.

I asked, “How many folks actually comment their SQL?” and called it “a great thing to get in the habit of.”

In the same post, I quoted @crunchydata on how SQL comments can support documentation and maintenance, and on Postgres comments for tables, columns, views, functions, custom data types such as enums, and indexes. The examples included COMMENT ON TABLE employee IS '...' and the beginning of a COMMENT ON COLUMN example.

I said I “hardly ever print things out these days,” but may have many of these images/guides printed out and framed to hang in my office; I shared https://t.co/kBEpaSsS6R.

The quoted @crunchydata post presents CTEs as a way to break SQL into smaller, readable chunks, test parts, and reuse parts in other queries.

Related: learning sql foundations, choosing and using postgres, using psql as an editor