r/SQL • • May 20 '26

PostgreSQL What are common SQL red flags?

Hello! interview prepping, here wondering what are some common red flags for wrioting SQL?

Like

LIKE failing to index, not having trasnactions, usign SELECT * instead of specific collums, etc 😃

48 Upvotes

192 comments sorted by

View all comments

246

u/pilesofbutts May 20 '26

Others may have differing opinions but I personally hate a b c aliases for joins. I prefer SQL join aliases to be an abbreviation for the table name. e.g. contact_info is aliased to ci. it helps with readability in my opinion.

65

u/Soccermom233 May 20 '26

OhHH I remember trying to read stack overflow examples and all the tables are aliased like t1, t2, t3. Ugh.

49

u/MakeoutPoint May 20 '26

Even without SQL, programmers who do this with any variable outside of the i in a for loop, then leave that legacy psychosis behind for someone else, can die in a hole. Or be forced to debug their old code 10 years later, for eternity, either works.

→ More replies (1)
→ More replies (2)

38

u/pceimpulsive May 20 '26

Taking it further CTE aliases should he descriptive of the output, just like a table name would be..

Too often I see CTE called ci and get tilted... :P

16

u/pilesofbutts May 20 '26

I've had to deal with "aggie" and "firstaggie".. These people should not be allowed a computer.

24

u/National_Cod9546 May 20 '26

CTE

CTE1

CTE2

CTE2.5

CTE2.7

CTE4

And then every CTE links at least two other ctes. The guy doing that finally retired two years ago. We're still working to get rid of all his logic. 

→ More replies (2)

2

u/[deleted] May 21 '26

[removed] — view removed comment

→ More replies (2)
→ More replies (1)

11

u/ComicOzzy sqlHippo May 20 '26

The entire codebase at my current company uses T1, T2, etc... and it's the most frustrating thing when you read the next query in the procedure that uses most of the same tables, but they joined them in a different order, so now that table that was T1 in the previous query is T3. It's like someone told them it was very important to always alias them this way or the queries might explode. And for a bonus frustration, temp tables are always #TEMP1, #TEMP2, #TEMP3, etc. COME ON, PEOPLE! Naming things is a lot easier than maintaining anonymously-named objects.

3

u/pilesofbutts May 21 '26

That would.. infuriate me beyond belief. Good grief. I bet the joins are backwards too, lol.

Can you just truncate these people?

3

u/ComicOzzy sqlHippo May 22 '26

All I can do is rewrite everything over time as the opportunities arise. As much as I complain about it, I really get a lot of joy rewriting everything. Maybe it's vanity.

3

u/pilesofbutts May 22 '26

I feel the same way and complain as well. However, I do enjoy doing better optimization and bringing about more elegance.

8

u/amayle1 May 20 '26

You worked with satan?

8

u/pilesofbutts May 20 '26 edited May 20 '26

Unfortunately more than I care to admit. The most evil one did "brick code" (zero formatting.. it was one giant brick).. and to make things worse.. Everything was also a full outer apply. I got to the point where I would refused to follow them on any code and would rewrite from scratch. I hear they learned inner joins this year.. Maybe there is a God?

6

u/MachineParadox May 21 '26

Declaring aliases but then not using them so you have no idea where the column is.

8

u/twillrose47 maybeSQL May 20 '26

I've never seen a b c -- always table abbreviations. Yes, agree, if I were hiring and I saw

select * from product a 
left join product_category b

this would be a red flag, wtaf moment to me :D

→ More replies (2)

3

u/IHoppo May 21 '26

As a corollary to this, use aliases to give domain knowledge too when using (for example) classification tables - for instance, if a table holds roles for parties, and you want student and lecturer roles from the party classification table linked to party - so using 2 hits on the classification table, use

From classification studentClassification,
classification lecturerClassification

Etc.

4

u/Common-Author-8441 May 20 '26

agreed. doesn't it depend on how long the table names are? if it's really long, then please, use an alias. if not, why not be 100% clear/explicit and use the table names?

3

u/pilesofbutts May 20 '26

I think it's up to personal interpretation there. For me, I don't like doing one full table name and the rest aliased if that makes sense? I like the consistency. Call it the tisms or what have you, but just the way I personally like to roll.

→ More replies (2)
→ More replies (1)

4

u/SpaceDrama May 20 '26

Unless there’s similar table names, I prefer to just not use alias names at all

2

u/PrisonerOne May 20 '26

First thing I do is F2 that puppy 

2

u/Red__M_M May 20 '26

I would aliase that as ContactInfo

2

u/snowmaninheat May 21 '26 edited May 21 '26

My preferred method is reverse alphabetical order (e.g., `cte_z`, `cte_y`), then so on. It’s an easy way to see the steps.

2

u/Tontonsb May 21 '26

As a programmer vulgaris, not an SQL one, I hate short aliases entirely. When I write SQL, I use aliases to make it more descriptive not less, e.g. if the join takes some specific rows, I will do something like join prices as prices_usd or join orders as latest_orders.

→ More replies (9)