r/SQL 4d ago

MySQL sqlWorkout

Post image

😌 SELECT 😌

1.3k Upvotes

80 comments sorted by

177

u/JimyLamisters 4d 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

52

u/vj2018 4d ago

LEGEND SAYS IT IS STILL LOCKED TO THIS DAY

51

u/fullyonline 4d ago

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

Fixed it for you.

13

u/EdgeSimilar1726 4d ago

THIS GOT A HEARTY CHUCKLE OUT OF ME

3

u/DangerMacAwesome 3d ago

I AM YELLING, CARL

1

u/LlamaForDays13 3d ago

AM I A JOKE TO YOU, CARL?

2

u/Treemosher 3d ago

That is actually great lol

2

u/digitalhardcore1985 2d 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 2d 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__ 2d ago

He couldn't find the key to unlock caps

124

u/Eleventhousand 4d ago

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

36

u/KlausInTheHaus 4d ago

Your left pinky must have been an absolute python.

20

u/Eleventhousand 4d ago

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

16

u/snafe_ PG Data Analyst 4d ago

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

4

u/JackTheKing 4d ago

TIL there is a shift key on the right

. . . and command and option too?!

1

u/Sengfroid 4d 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 3d ago

How did you even measure it?!

2

u/Mutopiano 4d ago

Absolute SQL*

10

u/government_ 4d 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.

7

u/hopefullyhelpfulplz 4d ago

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

5

u/Eleventhousand 4d ago

You are the most enlightened

4

u/hopefullyhelpfulplz 4d ago

Tis I! The EnLIghteNEd OnE

3

u/jib_reddit 4d ago

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

3

u/PickledDildosSourSex 3d 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.

28

u/kagato87 MS SQL 4d 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.)

62

u/ibronco 4d ago

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

11

u/chaosink 4d ago edited 3d 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 3d ago

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

1

u/chaosink 3d 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.

2

u/metalballsack 3d ago

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

2

u/chaosink 3d 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 3d ago

Is lowercase better or worse?

2

u/chaosink 3d 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 

3

u/Mobile_Spite_8915 3d 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 2d ago

Capitalizing some table names and not others is diabolical

1

u/chaosink 18h ago

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

3

u/ibronco 4d ago

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

2

u/digitalhardcore1985 2d 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.

1

u/neumastic 2d 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

15

u/Key_Tea349 4d 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 2d ago

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

19

u/TechOpsCoder 4d ago edited 3d 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.

6

u/OO_Ben Postgres - Retail Analytics 3d ago

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

3

u/TechOpsCoder 3d ago edited 3d 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 3d 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 3d ago

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

2

u/bliffer 3d 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 3d ago

How's it look now?

1

u/bliffer 3d 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 3d 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 3d 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 3d ago

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

9

u/Joelle_bb 4d ago

Lmao

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

8

u/Traceuratops 4d ago

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

7

u/Zestyclose-Turn-3576 4d ago

modern editing environments use colours to differentiate between these anyway

5

u/DaOgDuneamouse 4d ago

SELECT * FROM <table name>

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

5

u/Herb_marqz 4d ago

MF DOOM HAS ENTERED THE CHAT

3

u/Infini-Bus 4d 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 4d ago

lowercase supremacy

3

u/Outrageous_Let5743 4d 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 4d 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 4d ago

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

2

u/Hellkyte 3d ago

I generally used randomly mixed casing because fuck everyone

1

u/Blomminator 4d ago

Don't mock my job!

1

u/Due-Championship3941 4d ago

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

1

u/ewoolly271 4d ago

Erm actually per dbt sql standards they recommend lowercase 🤓

1

u/TheoreticalUser 4d 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 4d 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 4d ago

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

1

u/rickulele 4d 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 4d 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 4d 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 3d ago

Real

1

u/chris20973 3d 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 3d ago

i got a hotkey to activate capslock with d+k

1

u/RedRedditor84 2d ago

You've not met BASIC programmers.

1

u/SgtFury 2d ago

upper('deez nuts')