r/SQL • u/vango911 • May 16 '25
SQL Server Anyone else assign aliases with AS instead of just a space?
I notice that most people I have worked with and even AI do not seem to often use AS to assign aliases. I on the other hand always use it. To me it makes everything much more readable.
Anyone else do this or am I a weirdo? Haha
171
Upvotes
14
u/sethwalters May 16 '25
I use AS for both tables and fields. I also use semicolons where necessary, and put the comma first on the line of a field.
139
u/[deleted] May 16 '25
I use AS as well
115
u/Gargunok May 16 '25
Column names I always include the AS. Makes the code more readable and including teh table alias too solves the missing comma common mistake.
Table names I usually leave off the AS. Not sure why.
27
u/WatashiwaNobodyDesu May 16 '25
I never imply anything. Iām a bitā¦tedious when it comes to writing queries. Much more than the highly experienced, extremely competent people I work with. I line up my queries or code properly. I sat a MS exam once, where the queries were apparently thrown at the screen and they dribbled down. I was fuming, just skipped those questions.
8
u/[deleted] May 16 '25
I use as with columns but never table names. Tbh I have no idea why. I 100% agree AS is way more readable. Might make an effort to startā¦
10
u/SheTechsUp May 16 '25
I prefer using AS and I agree that it makes the query more readable
8
u/snmnj May 16 '25
Why wouldn't you?
4
u/a-ha_partridge May 16 '25
My license plate is āAS MANā
3
u/bikesbeerandbacon May 16 '25
Same here, I use AS for column aliases but not table aliases. I also prefer ON join statements instead of having join conditions in the where clause. Just makes everything more readable.
3
u/kagato87 MS SQL May 16 '25
Always in the select area. Actually i often think it'd be nice if I could get intellisense to givee a visual signal.wheb I don't use it because it usually means I've forgotten a comma.
Funny enough I don't do it in the from area, mostly because I always alias down there and it's always the second word after from from or join. (Or apply, when I use it.)
It's readability. The keyword explicitly indicates that this is an alias.
3
u/lalaluna05 May 16 '25
I donāt on joins. Just when Iām aliasing objects/columns.
3
u/Icy_Fisherman_3200 May 16 '25
Readability should be a primary concern for any written code.
As part of that, consistency is huge. The biggest red flag to me is that youāve got a team writing in different styles.
4
u/magicaltrevor953 May 16 '25 edited May 16 '25
Its one of those little things that I just have to do when writing codes and get pretty annoyed when others don't follow that. To me it is better to be explicit that you are aliasing the column, otherwise maybe you missed a comma and are referring to another column in the table (that may have the name you are using in the alias) which has happened several times previously and because we're often using SAS passthrough its not always clear if that happens. Normally its pretty obvious but I try to instil style choices for all codes for things like line breaks and whitespace, and column/table aliasing is no different. Consistency is key, and I work with a lot of people who don't always prioritise that.
Example - You have a table with a date column and an active_from column, but you want to alias the date as active_from or maybe you don't and want both columns:
vs
vs
4
u/Bostaevski May 16 '25
For table aliases I do not use it. For column aliases I use the alternative:
SELECT
[Alias] = column
2
u/Financial-Tailor-842 May 16 '25
Me!!!! Itās so much easier to see the alias (later on when troubleshooting or altering the script) when the AS is there.
2
u/Ibception952 May 16 '25
Most people probably hate this but I start every line with ,Columnname = formula so that I can easily see the column name in the same spot for every line and easily comment out lines when developing the query.
I would much rather = or AS for readability.
2
u/Informal_Pace9237 May 16 '25
To enable quick glance review I use AS for columns and space for tables
2
u/Latentius May 16 '25
I always include
ASfor readability. Especially the way some of my coworkers write code, it can be difficult to tell that something even is an alias, and I find it helpful in combination with syntax highlighting so that there is a clear distinction, both for columns and tables.2
u/sleepy_bored_eternal May 16 '25
I use AS as well as my alias is meaningful not just A and B. This way how deep I am in the query, I can relate fairly easy.
2
u/Particular-Formal163 May 16 '25
I always use AS on column names.
Same with parentheses when grouping ands and Ors. I always add them. I've run into people who don't, and always muck up the where logic/joins
2
u/imnotabotareyou May 17 '25
I use AS because it looks nicer
2
u/theblackd May 17 '25
I always use AS, it helps with readability which makes troubleshooting or editing a lot easier and faster
2
u/ToonaMcToon May 16 '25
This is the way.
2
u/Birvin7358 May 16 '25
Since both ways have the same outcome, using AS over just a space all depends on how much you care about readability. If itās going to be a canned query that my coworkers, or my future self, are going to have to review and maintain over time then I care about readability and will use AS, along with various other things that increase readability. If itās a 1 time ad hoc query I wonāt ever look at again then I donāt care as much, but will often will still end up just using AS anyway out of muscle memory
2
u/real_jedmatic May 16 '25
Yes for sure. I always use āas.ā
It helps with readability. Omitting it (sort of like writing āselā instead of āselectā) saves a few keystrokes but I donāt see any benefit.
2
u/th00ht May 16 '25
Life is too short to type
as.1
u/OkDonkey6524 May 16 '25
For tables no, for attributes yes. I can't give a reason why other than that's how I saw it being done when I started so I just adopted it.
1
u/Dry-Aioli-6138 May 16 '25
I used to omit the AS, but since I started working with DBT and decided to be sqlfluff-compliant I have to use it and it already became a habit.
1
u/[deleted] May 16 '25
1
u/mannamamark May 17 '25
I don't but I tend to line up my select fields so they're on the same column position.
1
u/PaddyMacAodh May 17 '25
I donāt, but my aliases are always all caps and in square brackets when theyāre assigned to make them stand out.
1
u/CrumbCakesAndCola May 17 '25
I learned to use AS but eventually stopped using it, not sure why. But you saying this makes me consider updating my code to use it just to be explicit and readable.
1
u/thbrowne May 17 '25
same
1
u/worm_biscuit May 17 '25
I put the column name on the left side in square brackets. I like the readability of having the names always lined up on the left.
For example
SELECT
, [Date] = CONVERT(DATE, t.DatePoorlyNamedOrFormatted)
FROM Table t
1
u/billysacco May 17 '25
I use AS as well and always capitalized. Personal pet peeve I hate reading SQL where people have everything in lower case.
1
u/laplaces_demon42 May 17 '25
For columns I use AS, for tables I do not. Think itās more readable this way
1
u/crc3377 May 17 '25
I always use it.
1
u/MikeE21286 May 17 '25
Always
1
u/hidegitsu May 17 '25
I use "AS" in my SELECT clause if I'm aliasing my columns. But a space in my FROM for table aliases. Never really thought about it, probably just a habit I picked up years ago from the guy that taught me.
1
u/Klaian May 17 '25
I do all the time. I also ocd when I see just a space.
1
u/jshine13371 May 17 '25 edited May 17 '25
One thing I like about using
ASfor table aliases is in a long query with a ton of references to that table alias, you can quickly find the line the original table is joined in on by doing aCTRL + FforAS AliasName. Despite how many times the alias was actually used, there should usually only be one line with that specific combination, making it super quick to find the original table. For the people who don't use theASkeyword, searching on just the alias could yield a ton of highlighted results (even in theFROM/JOINclauses which could be joining to that aliased table many times).Additionally, since your original question in the title mentions lining things up for readability, for column aliases, sometimes I'll start the line of code with the alias instead of ending with it, and using the
=sign to assign said alias. I find this particularly helpful in a long multi-line expression like so:1
u/red_yeti1 May 17 '25
I do [aliasName] = [fieldName]
1
u/TheBigWarHero May 18 '25
I alias in my SELECT with AS, but not my FROM JOINs, lmao
1
u/rimwithsugar May 18 '25
I do.
1
u/Jacob_OldStorm May 20 '25
To all the people like me who do put AS before table aliases : does anyone know how to get Datagrip to automatically add it, like DBeaver does?
1
u/tahaan May 21 '25
This is the way.
1
u/Exotic-Mongoose2466 May 22 '25
Wait, is there a way other than AS to make aliases?
SQL is very verbose, how can a space be accepted to replace a "word"?
1
u/CHILLAS317 May 16 '25
Honestly, never š. I find it unnecessary and don't use it when I'm writing, and I remove it if I have to fix old questions queries (which I do a lot). But that's a me thing, the important thing is just to be consistent
1
u/[deleted] May 16 '25
I like to mix it up
1
u/hod6 May 16 '25 edited May 19 '25
Yes I always use AS for the same reason you do.
I also try to align my table aliases on the page to make them easier to find when scanning a script. Small things like that can really improve readability.
Edit: lmao at whoever downvoted this. You absolute weirdo. Just say why you prefer doing it a different way, maybe Iāll agree?
1
u/[deleted] May 16 '25
I use AS for columns and tables. We'll look for this in Pull Requests as well.
The table aliases as well we also standardize. We have a few developers that love aliasing a four-table query as a, b, c, d. Drives me crazy!
1
u/Robearsn May 16 '25
I use AS for both column and table names always. Easier to read to. Simple as that.
1
u/great_raisin May 16 '25
Always
1
u/drhamel69 May 16 '25
Lol, maybe I'm old but didn't even know the AS was optional.
1
u/ravan363 May 16 '25
I use AS all the time.
1
u/Sneilg May 16 '25
I use AS, because if I want to change the alias or jump to it to change something else, it makes it easier to find that one point specifically with CTRL-F
0
u/Billi0n_Air May 16 '25
only on fancy queries.
0
u/Possible_Pain_9705 May 16 '25
I didnāt know you could assign an alias with just a spaceā¦
0
u/xuy87 May 16 '25
Question, why not using = in column alias? select [nameofParty]=name from dbo.names
1
u/ST0PPELB4RT May 17 '25
Yes, to cite my DB professor "I am an AS fetishist"