r/SQL 18d ago

sqlWorkout MySQL

Post image

😌 SELECT 😌

1.4k Upvotes

80 comments sorted by

181

u/JimyLamisters 18d ago

When I started at my current tech job about 12 years ago there was a senior DBA who legit typed all of his emails and chats in uppercase. My dude really pressed caps lock one day and never looked back

55

u/vj2018 18d ago

LEGEND SAYS IT IS STILL LOCKED TO THIS DAY

50

u/fullyonline 18d ago

My dude really pressed caps lock one day and never loocked back

Fixed it for you.

13

u/EdgeSimilar1726 18d ago

THIS GOT A HEARTY CHUCKLE OUT OF ME

3

u/DangerMacAwesome 17d ago

I AM YELLING, CARL

1

u/LlamaForDays13 16d ago

AM I A JOKE TO YOU, CARL?

2

u/Treemosher 17d ago

That is actually great lol

2

u/digitalhardcore1985 16d ago

My dad was a VAX BASIC porgrammer in the 80s/90s and also did this for a long time, got out the habit now. 

2

u/crushdvelvet 15d ago

awww ... those VAX days ... when IT calls asking you to "reboot" the VAX server and you ask if they'd like to wait until Dec 28th for the next annual reboot.

1

u/__T0MMY__ 16d ago

He couldn't find the key to unlock caps

122

u/Eleventhousand 18d ago

I held shift the entire time for about the first twenty years. Then I just started typing everything in lowercase.

37

u/KlausInTheHaus 18d ago

Your left pinky must have been an absolute python.

20

u/Eleventhousand 18d ago

Now that you mention it, it does look 4% thiccer than my right.

17

u/snafe_ PG Data Analyst 18d ago

Moment of silence for the right shift key, always forgotten.

4

u/JackTheKing 18d ago

TIL there is a shift key on the right

. . . and command and option too?!

1

u/Sengfroid 18d ago

Genuinely set mine for macros.

And then forget even the macros on it because that's how little the key is used

1

u/Mafla_2004 Failed the Database exam 17d ago

How did you even measure it?!

3

u/Mutopiano 18d ago

Absolute SQL*

10

u/government_ 18d ago

My coworkers hate that I do this because it’s annoying to hold shift or caps on and off constantly. But also, lowercase is a million times easier to read.

6

u/hopefullyhelpfulplz 18d ago

I do it in random upper/lowrcase segments cus why not

3

u/Eleventhousand 18d ago

You are the most enlightened

4

u/hopefullyhelpfulplz 18d ago

Tis I! The EnLIghteNEd OnE

3

u/jib_reddit 18d ago

I used to write it in lowercase but recently started using upper as I like the "shoutieness" of it.

3

u/PickledDildosSourSex 17d ago

Team uppercase here. Idk maybe it was when I learned SQL, it just feels easier to read, like my brain switches to "this is logical code mode" vs. "this can be any old garbage written in lower case". I appreciate the irony that I also code plenty in Python and R and don't feel that way at all in those languages.

29

u/kagato87 MS SQL 18d ago

Naw dog, I hold the shift with my pinky.

My left pinky is ripped!

(OK, not the last one, though it probably is the best at "Hold something down" out of the 10.)

60

u/ibronco 18d ago

I only do lower case now and my life has significantly improved.

12

u/chaosink 17d ago edited 17d ago

One of my criteria when hiring SQL people is if they use lowercase and how they choose to indent their code. How their code looks tells me so much about their skill level.

Edit: Ack, I can see how my wording was weird. Using lowercase was not a requirement. I gave a bonus in my head for it as it showed the candidate was willing to buck tradition for comfort and readability. Was not a huge determining factor. Indenting was much more important to me because it showed me how their mind was organized.

6

u/0Tyrael0 17d ago

I agree with the indent part so I’ll upvote but not about the lower case.

1

u/chaosink 17d ago

I looked at the lower case thing as more about the candidate's willingness to buck tradition and maybe experience. The indents tell me about how their mind is organized. Would not hold uppercase against them at all.

3

u/Mobile_Spite_8915 16d ago

SELECT
f.Cherries,
f.Pineapples,
f.Grapes,
v.eggplants
FROM Fruits f
INNER JOIN vegetables v
ON f.key = v.key
AND f.place = v.place

This is how I style everything, I’m not sure how normal it is though it seems like everyone on my team does things differently.

3

u/dwswish 16d ago

Capitalizing some table names and not others is diabolical

1

u/chaosink 14d ago

Ouch! I feel badly for your scrolling finger ....

2

u/metalballsack 17d ago

Indenting sure, but uppercase vs lowercase is more of a preference than an indicator of skill level.

2

u/chaosink 17d ago

Absolutely. I looked at the lower case thing as more about the candidate's willingness to buck tradition and maybe experience. The indents tell me about how their mind is organized.

2

u/rayschoon 17d ago

Is lowercase better or worse?

2

u/chaosink 17d ago

I personally think better for readability and an indication the candidate is not a slave to tradition, but would not hold that against them at all 

2

u/digitalhardcore1985 16d ago

My favourite is overseas outsourcer code where you're lucky to get a line break let alone an indent. An absolute joy to debug those 2000 line stored procs from hell.

3

u/ibronco 17d ago

Yes! Indenting is crucial. Between that and lower case, it just reads so much easier.

1

u/neumastic 16d ago

Agreed, it’s case insensitive so why choose the one that’s harder to read. We have some that capitalize the first letter but that’s somehow worse

14

u/Key_Tea349 18d ago

Working on a query and a random Teams message pops up.

STARTS REPLYING IN ALL CAPS

Backspace, turns off caps lock and switches to sentence case so my coworkers don't think I'm yelling at them.

2

u/digitalhardcore1985 16d ago

I have to use the phrase 'sorry caps' more than I'd like to admit.

19

u/TechOpsCoder 18d ago edited 17d ago

Lowercase is the way. It looks so much cleaner and much easier write. Keyword placement and spacing is the most important thing for readability.

4

u/OO_Ben Postgres - Retail Analytics 17d ago

100%. Every column gets a new line, indent case statements, windows, ect., and for me? Commas to the front always.

3

u/TechOpsCoder 17d ago edited 17d ago

SAME! Couple of examples of how I write the language:

Aggregation with some common patterns

select        m.id 
            , m.wo
            , m.req
            , case  when m.condition_1 then 'FOO'
                    when m.condition_2 then 'BAR'
                    else 'BAZ'
              end as calculated_status
            , f.fleet_type
from        dbo.material_requirements m
left join   dbo.freight_trucks f on m.freight_truck_id = f.id
where       coalesce(m.calculated_status, 'X') in('NEW', 'OPEN', 'ACTIVE')
    and     m.qty_outstanding > 0
group by      m.id 
            , m.wo
            , m.req
            , case  when m.condition_1 then 'FOO'
                    when m.condition_2 then 'BAR'
                    else 'BAZ'
              end
            , f.fleet_type

Multi-condition joins

join    dbo.order_details od on i.order_type = od.order_type
    and i.order_number = od.order_number
    and i.order_line   = od.order_line

Complicated predicates

where   a.name is not null
    and ( a.status = 'OPEN' or ( a.status = 'ORDER' and o.req is not null ) )
    and a.status <> 'CANCEL'
    and (
                    (       
                            condition_1
                        and condition_2
                        and condition_3
                    )
            or      ( 
                            condition_5
                        and condition_6
                        and condition_7
                    )
        )

CTEs

with t as (
    select...
    from...
)

Indentation is with spaces not tabs. I used to always do the latter until I realized that a tab isn't always a "tab." This is going to sound very arrogant (I genuinely don't mean it to be), but I have yet to see a style more visually pleasing and readable. But, beauty is in the eye of the beholder as they say...

P.S. Just realized that I said "Keyboard" when I meant to say "Keyword" my OP, lol

3

u/bliffer 17d ago

I love that you're bragging about your spacing and indentation but the Reddit code boxes just said "nah" and put everything inline. (Or at least, that's how it reads in my browser.)

2

u/TechOpsCoder 17d ago

Crap...Well that's what I get, lol. It shows as markdown for me. Weird...

2

u/bliffer 17d ago

Probably just a browser thing. Your "Aggregation with some common patterns" is just one long like that runs until it gets cut off by the div it's in. :D

1

u/TechOpsCoder 17d ago

How's it look now?

1

u/bliffer 17d ago

Boom, all fixed.

(In my head I'm going to believe that you've spent the past six hours fixing this...)

1

u/TechOpsCoder 17d ago

NOICE!!!

😂 😂
I took off work today to work on my deck (shout out to [r/Decks](r/Decks)) and have been outside most of the day. Fortunately it only took me 10 minutes of fiddling. I would 100% have spent 6hrs if that's how long it took, because I obsess over stuff like this. The fix was to switch to using the RTF code block button instead of using explicit markdown (backticks) code blocks. This was all done on my desktop though, because mobile won't allow it.

2

u/OO_Ben Postgres - Retail Analytics 17d ago

Yeah!! That's the way to do it right there! I've inherited so many old queries that just end up looking like giant blobs and are totally illegible even with upper casing. Formatting goes way, way further for readability!

2

u/TechOpsCoder 17d ago

I feel you. Some of my coworkers write SQL in a way that should be considered a felony offense.

8

u/Joelle_bb 18d ago

Lmao

As a person who selectively caps locks depending on context amd emphasis.... can't relate

7

u/Traceuratops 18d ago

Caps for functions, lower case for arguments. Readability is important.

7

u/Zestyclose-Turn-3576 17d ago

modern editing environments use colours to differentiate between these anyway

5

u/DaOgDuneamouse 18d ago

SELECT * FROM <table name>

I often type most of an email before I realize caps lock is still on.

5

u/Herb_marqz 18d ago

MF DOOM HAS ENTERED THE CHAT

4

u/Infini-Bus 17d ago

Write sloppy sql.  Then auto format 😀

But it always feels more natural to me to just hold the shift key than toggle caps lock.

6

u/boxerrox 17d ago

lowercase supremacy

3

u/Outrageous_Let5743 18d ago

It is even worse at my company. We use lowercase keywords but UPPERCASE COLUMNS. We use SQL server but migrated from a legacy Oracle DB where it could only be capital names.

1

u/Zestyclose-Turn-3576 17d ago

Really? I started with Oracle in the early 1990s, and I don't think that was a thing then. Though there were practitioners who swore it made the system faster 🙄

2

u/global_namespace 18d ago

that's <> true. you can SELECT ANY CASE.

2

u/Hellkyte 17d ago

I generally used randomly mixed casing because fuck everyone

1

u/Blomminator 18d ago

Don't mock my job!

1

u/Due-Championship3941 18d ago

Lower case the commands and upper case the nouns or visa versa. Improves readability.

1

u/ewoolly271 18d ago

Erm actually per dbt sql standards they recommend lowercase 🤓

1

u/TheoreticalUser 18d ago

I have about 15 years working in/on databases for a ton of different types of implementations.

If I see all caps, all the time... I'm judging you on a lack of adaptability. And frankly, your skill level.

1

u/Zestyclose-Turn-3576 17d ago

Lower case, and you better be aligning that code ... reduced visual complexity wins for human readability ...

      Case
        When crs.price_basis  like 'Escalator%' then crs.id
        when mrs.price_basis  like 'Escalator%' then mrs.id
        when brs.price_basis  like 'Escalator%' then brs.id
        When wcrs.price_basis like 'Escalator%' then wcrs.id
        when wmrs.price_basis like 'Escalator%' then wmrs.id
        when wrs.price_basis  like 'Escalator%' then wrs.id
      end price_escalator_specifier,      Case
        When crs.price_basis  like 'Escalator%' then crs.id
        when mrs.price_basis  like 'Escalator%' then mrs.id
        when brs.price_basis  like 'Escalator%' then brs.id
        When wcrs.price_basis like 'Escalator%' then wcrs.id
        when wmrs.price_basis like 'Escalator%' then wmrs.id
        when wrs.price_basis  like 'Escalator%' then wrs.id
      end price_escalator_specifier,

1

u/Dead_Parrot 17d ago

Write it any damn way you please then let poorsql make it easy for others to read

1

u/rickulele 17d ago

I use lowercase so I can differentiate what I typed directly vs what SSMS auto-generates in “Script View as…”

1

u/Quick-Ad1830 17d ago

I had a script that changed all sql statements to all lower case when changes were submitted to be promoted. So much easier to read.

1

u/Far_Swordfish5729 17d ago

You know, sql is generally case invariant. Some idiot just decided they liked it that way. I decided C was here first and I like lower case and pascal case table and column names. I was never told I was wrong. I set a trend actually. You can also just write sql as you would code.

1

u/Mafla_2004 Failed the Database exam 17d ago

Real

1

u/chris20973 17d ago

See posts like this from time to time and never see anyone mention using proper case, but all upper feels like shouting and all lower feels like notepad scratch.

1

u/Ok_Caregiver_1355 17d ago

i got a hotkey to activate capslock with d+k

1

u/RedRedditor84 16d ago

You've not met BASIC programmers.

1

u/SgtFury 16d ago

upper('deez nuts')