munk-a
6 days ago
While postgres is indeed case sensitive usually writing queries with keywords in all caps is an effort to increase legibility for visual pattern matching. It absolutely isn't needed but if I'm debugging a query of yours I will send it through my prettifier so that I can breeze through your definitions without getting hung up on minor weird syntax things.
It's like prettification in any other language - visual structures that we can quickly recognize (like consistent indentation levels) make us waste less time on comprehension of the obvious so we can focus on what's important.
The only thing I really object to is "actuallyUsingCaseInIdentifiers" I never want to see columns that require double quotes for me to inspect on cli.
wiredfool
6 days ago
I find all caps identifiers wind up just looking like interchangeable blocks, where lowercase have word shapes. So all caps just slows down my reading.
WorldMaker
6 days ago
I feel similarly and I also have friends with Dyslexia with even stronger opinions on it. All caps in addition to being "shouting" to my ancient internet-using brain (and thus rude in most cases), creates big similar rectangular blocks as word shapes and is such a big speed bump to reading speed for everyone (whether or not they notice it). For some of my friends with Dyslexia that have a huge tough time with word shapes at the best of times, all caps can be a hard stop "cannot read" blocker for them. They say it is like trying to read a redacted document where someone just made rectangular black marker cross outs.
Personally, given SQL's intended similarity to English, I find that I like English "sentence case" for it, with the opening keyword starting with a capital letter and nearly every remaining letter lower case (except for Proper Nouns, the truly case-sensitive parts of SQL like table names). Sentence case has been helpful to me in the past in spotting things like missing semicolons in dialects like Postgres' that require them, and/or near keywords like `Merge` that require them or helping to visually make sure the `select` under a `Merge` is intended as a clause rather than starting a new "sentence".
akira2501
5 days ago
> I find that I like English "sentence case" for it,
I could go either way, but if you want to go back and modify a query, this makes it more difficult for me. I just use a block syntax for my queries:
SELECT *
FROM the_table
WHERE some_column = 12
AND other_column IS NOT NULL
ORDER BY order_column;
WorldMaker
5 days ago
It's a bit of a "Why not both?" situation, I think? You can have blocks and sentence case:
Select *
from the_table
where some_column = 12
and other_column is not null
order by order_column;
That seems so much more readable to me. As I said, that single capital `S` in the outermost "select" has come in surprisingly handy in my experience when scanning through a collection of statements or a transaction or a stored procedure or even just a statement with a bunch of nested sub-selects. It's an interesting advantage I find over "all lower case" or "all upper case" keywords.4ggr0
5 days ago
your example is less readable for me. not by a lot, but still.
the other example has the commands, in caps, on the left and the values on the right, in lowercase. your example removes one of those aspects and makes everything lowercase. my brain can ignore all-caps stuff, as these are just commands and the things i actually care about mostly are the values.
but i mean, in the end, it's just preferences. if you write SQL-queries, #1 is that you understand them well :)
Ntrails
5 days ago
> your example is less readable for me. not by a lot, but still
Agree, but I wonder how much of that is just the lack of colouring. My brain is suuuuper hard wired to expect all the special keywords to be identified that way as well as by case.
Mostly I'm a caps guy because my ahk scripts expand text like "ssf","w" and "gb"* to be the way I learned to write them at first.
wruza
5 days ago
I use a similar structure but without column alignment.
SELECT
a,
b
FROM t1
JOIN t2 ON …
WHERE cond1
AND cond2
ORDER/GROUP/HAVING etc
justanorherhack
4 days ago
This is the way, everyone else is wrong.
cwbriscoe
5 days ago
SELECT *
FROM the_table
WHERE some_column = 12
AND other_column IS NOT NULL
ORDER BY order_column;
I usually don't bother with ALL-CAP keywords.conductr
5 days ago
I prefer lower case for my personal legibility reasons and it seems like a prettyfier should be able to adjust to that user’s preference. It’s not a team sport for me so I never had a conflict of styles other than converting public code samples to match my way.
yen223
5 days ago
I've always found it funny that SQL was designed the way it is to be as close to natural English as possible, but then they went ahead and made everything all-caps
paulryanrogers
5 days ago
Some old terminals didn't have lower case. Like 1960s era
andrei_says_
5 days ago
Also sql editors like datagrip color the sql syntax very well.
gwbas1c
6 days ago
It's really useful to know this when working with SQL interactively.
Specifically, if I'm banging out an ad-hoc query that no one will ever see, and I'm going to throw away, I don't worry about casing.
Otherwise, for me, all SQL that's checked in gets the commands in ALL CAPS.
archsurface
6 days ago
My understanding is that the caps were syntax highlighting on monochrome screens; no longer needed with colour. Can't provide a reference, it's an old memory.
_Wintermute
6 days ago
Most of the SQL I write is within a string of another programming language, so it's essentially monochrome unless there's some really fancy syntax highlighting going on.
jaredklewis
6 days ago
Aside, but jetbrains IDEs seem to have some way to detect embedded sql and highlight it. I don’t remember configuring anything to get this feature.
wmfiv
6 days ago
More than highlight they'll do schema validation against inline SQL strings also.
TRiG_Ireland
5 days ago
VS Code does (did?) detect embedded SQL in PHP and correctly colour it, but only if it's on a single line. Any linebreaks and the colour is turned off. Also, if you're using prepared statements and have an @label, and that label is at the end of the string (so immediately followed by a closing quote), the SQL colouring continues into the rest of the PHP beyond the string. So it's important that single-line SQL statements ending in a @label be edited into multi-line statements to turn off the broken SQL colouring. Odd.
munk-a
5 days ago
PHP strings tend to have better syntax highlighting with here/now docs (i.e. starting with `<<<TOKEN`). I've found SublimeText to have excellent SQL detection when using these tokens to delineate queries (and the syntax lends itself well to block strings anyways).
yen223
5 days ago
This is Jetbrain's "language injection" feature if you want to look it up. It works with any languages that the IDE supports, and like a sibling comment mentioned it does more than syntax highlighting.
https://www.jetbrains.com/help/idea/using-language-injection...
formerly_proven
6 days ago
If you're working with some established framework and project structure their IDEs pull that information out of that, otherwise you'll need to at least tell it the dialect, but if you e.g. configure the database as a data source in the IDE you'll get full schema xref.
tomjen3
6 days ago
Another aside: that is true for a huge range of programming languages as well as things like HTML. I believe it can automatically add \ to " in strings when those strings are marked to the IDE as HTML.
wruza
5 days ago
Vimers can adapt https://vim.fandom.com/wiki/Different_syntax_highlighting_wi... for a similar thing (but you have to wrap a string into some regular syntax).
somat
5 days ago
Same, I am a bit conflicted, I enjoy the sql and don't really like the ORM's but I hate seeing the big blocks of SQL in my code.
So I wrote a thing that lets me use sql text as a function, it is several sorts of terrible, in that way that you should never write clever code. but I am not really a programmer, most of my code is for myself. I keep using it more and more. I dread the day Someone else needs to look at my code.
WorldMaker
6 days ago
I find that in worst cases I can always copy and paste to a quick temporary buffer that is highlighted. I might be doing that naturally anyway if I'm trying to debug it, just to run it in a Data IDE of my choice, but sometimes even just using a scratch VS Code "Untitled" file can be useful (it's SQL auto-detect is usually good enough, but switching to SQL is easy enough if it doesn't auto-detect).
CoastalCoder
6 days ago
I think the "color is all we need" idea makes sense in proportion to how many of our tools actually support colorization.
E.g., the last time I used the psql program, I don't think it had colorization of the SQL, despite running in a color-capable terminal emulator.
It probably doesn't help that terminal colors are a bit of mess. E.g., piping colored output through 'less' can result in some messy control-character rendering rather than having the desired effect.
dllthomas
6 days ago
> piping colored output through 'less' can result in some messy control-character rendering rather than having the desired effect.
It can, but -G or -R can fix that.
mixmastamyk
6 days ago
Try pgcli for color and completion.
renewiltord
6 days ago
Good tip, thank you.
vundercind
6 days ago
Like sigils in that regard. Perl-type sigils are extremely nice... if you're editing in Notepad or some ancient vi without syntax highlighting and the ability to ID references on request. Little point to them, if you've got more-capable tools.
o11c
6 days ago
Note that case handling is a place where postgres (which folds to lowercase) violates the standard (which folds to uppercase).
This is mostly irrelevant since you really shouldn't be mixing quoted with unquoted identifiers, and introspection largely isn't standardized.
yen223
5 days ago
Given that other mainstream RDBMSes lets you configure how case handling should happen, Postgres is arguably the closest to the standard.
Usual caveat of how nobody sticks to the ANSI standard anyway applies.
emmanuel_1234
6 days ago
Any recommendation for a prettifier / SQL linter?
gnulinux
6 days ago
I'm curious about this for DuckDB [1]. In the last couple months or so I've been using DuckDB as a one-step solution to all problems I solve. In fact my development environment rarely requires anything other than Python and DuckDB (and some Rust if native code is necessary). DuckDB is an insanely fast and featureful analytic db. It'd be nice to have a linter, formatter etc specifically for DuckDB.
There is sqlfluff etc but I'm curious what people use.
[1] DuckDB SQL dialect is very close to Postgres, it's compatible in many ways but has some extra QOL features related to analytics, and lacks a few features like `vacuum full`;
chrisjc
3 days ago
Since you're using python, have you looked into sqlglot? I think it has some pretty-print options.
ptman
5 days ago
sqlfluff is better than https://github.com/darold/pgFormatter , but it can get confused at times.
homebrewer
6 days ago
IDEA if you want to use it for other things (or any other JetBrains IDE). Nothing comes close feature-wise.
If you don't:
- https://www.depesz.com/2022/09/21/prettify-sql-queries-from-...
- https://gitlab.com/depesz/pg-sql-prettyprinter
Or https://paste.depesz.com for one-off use.
jillyboel
5 days ago
i think the idea sql prettifier is pretty silly sometimes. it really likes indenting stuff to make sure things are aligned, which often results in dozens of whitespaces
homebrewer
5 days ago
It makes it easy to distinguish null vs not null columns and other similar things, so I personally don't mind.
jillyboel
5 days ago
It's more about queries like (dummy example)
RETURN CASE
WHEN a = 1 THEN 1
ELSE 2
END;
where it insists on aligning WHEN past CASE. I think it would be perfectly reasonable to indent WHEN and ELSE 4 spaces less, for example. Similar things happen with nested conditions like (a or (b and c)) all getting pushed to the rightNegativeLatency
5 days ago
prettier plugin sql, or pg_format
mannyv
5 days ago
The uppercase is usually there to show people what's SQL vs what's custom to your database. In books it's usually set in courier.
I thought he was talking about psql's case sensitivity with table names, which is incredibly aggravating.
avg_dev
6 days ago
i agree; but i use caps in my codebase, and lowercase when testing things out manually, just for ease of typing.
munk-a
6 days ago
Ditto - if I'm throwing out an inspection query just to get a sense of what kind of data is in a column I won't bother with proper readable syntax (i.e. a `select distinct status from widgets`). I only really care about code I'll need to reread.
MetaWhirledPeas
5 days ago
> writing queries with keywords in all caps is an effort to increase legibility for visual pattern matching
Considering most programming languages do just fine without ALL CAPS KEYWORDS I'd say it's a strange effort. I wish SQL didn't insist on being different this way.
I agree with you on prettification though. As long as the repository chooses a prettifier you can view it your way then commit it their way. So that's my advice: always demand prettification for pull requests.
sensanaty
5 days ago
I don't write or read SQL too often, but I prefer the ALL_CAPS because it usually lets me know if something is part of the SQL syntax itself or a function or whatever, or if it's referencing a table/column/etc.
Obviously not very foolproof, but automated linters/prettifiers like the one in DataGrip do a good job with this for every query I've ever thrown at it.
grahamplace
5 days ago
For checked-in SQL queries, we follow: https://www.sqlstyle.guide/
The combination of all caps keywords + following "the river" whitespace pattern dramatically improves readability in my opinion
zusammen
5 days ago
The all caps syntax also helps queries stand out as distinct from typical source languages. It is often helpful, since SQL tends to end up in all sorts of applications.
neves
5 days ago
You can convey more info with color. Any half decent editor can color your SQL.
All caps letters are more similar and harder to read.
marcosdumay
6 days ago
Well, as long as you aren't imposing the noisy syntax into everybody by pushing the case-change back into the code...
But getting some editor that highlights the SQL will completely solve your issue.
munk-a
6 days ago
I think fighting in PRs over syntax preferences is pretty useless so dev shops should generally have code style guidelines to help keep things consistent. In my company we use all caps casing since we have momentum in that direction but I think that decision can be reasonable in either direction as long as it's consistent - it's like tabs vs. spaces... I've worked in companies with both preferences, I just configure my editor to auto-pretty code coming out and auto-lint code going in and never worry about it.
troyvit
5 days ago
> no longer needed with colour.
I think the increased legibility for visual pattern matching also makes SQL easier to read for many of the 350 million color blind people in the world.
jillyboel
5 days ago
what is your prettifier of choice for postgres?