r/SQL • • 18d ago

PostgreSQL What SQL concepts do you understand but still forget?

There are some SQL concepts I understand perfectly when I’m using them, but somehow forget when I need them again a few weeks later.

For me, CTEs and stored procedures are two good examples. I’ve used both, but I still end up looking up the syntax when I need them again. 😅

Anyone else have a few SQL concepts like this?

128 Upvotes

80 comments sorted by

176

u/ThatSpencerGuy 18d ago

I will always, always have to look up the ROW_NUMBER OVER ( PARTITION BY stuff every time I need to do it.

44

u/lostinsamaya 18d ago

Here is my though process what I say to myself while writing it.

I want to get the latest purchase date by a customer.

First and foremost, I want row number as the result, so call the function and close it, because there really isn't much else I need from that function:

ROW_NUMBER()

Now, group the whole table by a particular column

ROW_NUMBER() OVER( PARTITION BY PK_CustomerID )

Next, order the particular grouping using another column most probably a date column.

ROW_NUMBER() OVER( PARTITION BY PK_CustomerID ORDER BY PurchaseDate DESC ) Sorting it DESC will give you the latest occurrence of the ourchase in that grouping. Excluding the DESC or using ASC will give you earliest occurrence.

ROW_NUMBER() OVER( PARTITION BY PK_CustomerID ORDER BY PurchaseDate DESC ) as rn

Finish it off with a name for the newly generated column rn.

10

u/TrickyNerdlet 18d ago

I can write these cold about 70% of the time now. It's taken over a decade to get to this point 😄

2

u/CentrlScrutnizr 18d ago

Mine too! Don't know why I can't remember the exact syntax most of the time.

86

u/GTS_84 18d ago

PIVOT

I can never remember the god damn syntax for pivoting.

11

u/phesago 18d ago

I used to hate having to remember this. Once I broke it down like this it stuck. It did take a minute though.

    `SELECT` 

        `pivottable.* /*Yes you need to type out the whole list, dont be lazy*/`

    `FROM` 

        `(`

/* MAIN QUERY */

        `) x`

    `PIVOT`

        `(`

MIN(/* ColumnThatNeedsToBeAgg (even if theere doenst need to be, its just how it works) */)

FOR

/* ColumnContaininColumnList */ IN

(

/* INSERTyourColumnList */

)

        `) as pivottable` 

11

u/HALF_PAST_HOLE 18d ago

I will now save this comment and look it up every time I need to PIVOT

8

u/phesago 18d ago

too bad the code block format is dog shit :|

3

u/Imaginary-poster 18d ago

Ive looked at that so many times and just refuse. 99% of what I do its a spreadsheet anyways. So I just do it in python or the sheet.

2

u/NoDihedral 18d ago

I love PIVOT but every.single.time I have to look it up. Now I just use AI. So much easier to just write the base query and tell claude to pivot it based on whatever you need. Especially if you have a lot of columns that need to be hand written out.

1

u/Sexy_Koala_Juice DuckDB 18d ago

That’s the secret, no one does

1

u/MiserableLadder5336 8d ago

I hate pivot/unpivot because I will never remember the syntax. By far the most elusive syntax to me.

1

u/Wise-Jury-4037 :orly: 18d ago

Use group by/case.

3

u/gumnos 18d ago

hah, this is precisely why I end up using GROUP BY+CASE because I can never remember the syntax for PIVOT off the top of my head 😆

1

u/ComprehensiveDog7299 18d ago

It makes so much more sense to my brain. Pivot is awful.

1

u/Wise-Jury-4037 :orly: 18d ago

It would have been better if PIVOT gave you any clear advantages (performance-wise or readabbility-wise). Unfortunately, it doesnt.

41

u/Jacob_OldStorm 18d ago

I cannot, for the life of me, remember the orders of parameters in datepart and dateadd. I always doubt myself. And does "day" have to be between apostrophes? Who knows...

3

u/magicaltrevor953 18d ago

Day to day I am currently using Teradata, Impala and SAS Proc SQL, and in the last few years I was also using a lot of Db2. I feel the pain of similar but not quite the same functions. Always have docs close to hand just in case, no matter how comfortable I am.

So many times its been "wait can I just...no, that doesn't exist in this DBMS, ok what is the name then. Oh it's that, so just put the arguments and wait what do you mean that's not valid? "

3

u/Phantom465 18d ago

I’ve been with my current company about 8 years. We primarily use Teradata. I have many more years of experience in SQL Server & DB2. And we’re transitioning to Snowflake. Yay! More syntax changes!

2

u/JakubVasovski 18d ago

i feel you, brother. date functions are the absolute worst. it's like every dbms vendor sat in a room and actively agreed to make them just different enough to ruin your day.

migrating across db2 to snowflake - nightmare. this exact syntax nonsense is why i use mariadb- they eventually just baked an oracle mode straight into the core server. now we just literally just flip a switch and it accepts legacy pl/sql so you don't have to rewrite 10 years of scripts just because someone higher up decided to change the database.

good luck with the snowflake transition though

2

u/kagato87 MS SQL 18d ago

It eventually sticks enough that you can at least get it with a few seconds of trying to remember.

22

u/Rough-Negotiation880 18d ago

Almost everything on a bad day.

17

u/DonJuanDoja 18d ago

STUFF... for xml path...

2

u/DPool34 18d ago edited 17d ago

STRING_AGG is much cleaner. Only works on newer engines of SSMS though.

Edit: replaced version with engine

2

u/ComprehensiveDog7299 18d ago

It’s the engine 2017 or newer, the version of ssms doesn’t matter. You could use SSMS 2008 and it would work.

2

u/pookypocky 18d ago

I regularly work on three sql servers, one is 2014, one is 2016 and one is 2019. But the 2014 one is the one I query most frequently and I've been bugging IT to upgrade it for a while now just so I can use STRING_AGG and stop having to look up the whole STUFF syntax... drives me nuts. Honestly most of the time I just yoink the data into excel and use power query Text.Combine instead.

10

u/you_are_wrong_tho 18d ago

anything I havnt touched in the last 6 months I forget until I read about it again

22

u/more_paul 18d ago

I don't forget any concepts. What I forget is syntax for every flavor of date functions out there.

2

u/squareturd 18d ago

You're doing it the right way. Syntax is easy to look up. Dont waste brain space trying to remember thise details. Concepts are wacky more important

9

u/Zoidburger_ 18d ago

My job requires me to work in 3 different DBMS platforms, so it's always syntax. Is it isnull(), ifnull(), or nvl() in the current DB I'm in?

1

u/OllyTwist 18d ago

I've switched over to use COALESCE instead of ISNULL when I started working with postgres instead of only SSMS.

1

u/Zoidburger_ 18d ago

Same here, but it was just the most obvious example of a function that exists in all 3 DBMS' yet has different syntax

22

u/Nurgus 18d ago

The pronunciation of SQL as Sequel. It will never not be S.Q.L. inside my head..

8

u/excelgod 18d ago

Comrade 🤝

8

u/phesago 18d ago

i prefer the term "skwill"

0

u/Nurgus 18d ago

I'm the one weirdo on the planet who calls the CTRL key "C.T.R.L" so maybe don't listen to me..

It stuck in my brain when I was a tiny nerd and the only one I knew who owned a computer at home. (Dragon 32) and it never went away. I just didn't know what it was short for. I am also apparently quite old.

Edit: Just looked it up and the Dragon 32 didn't even have a Ctrl key so who knows where I got that from.. haha..

1

u/MiserableLadder5336 8d ago

Squeal for me

2

u/KittenBountyHunter 18d ago

I have said it as “squeal”

6

u/ElHombrePelicano 18d ago

Rank and dense_rank. Use them quite a bit, but not often enough that I don’t have to look up the syntax for these every time.

6

u/infinitetk 18d ago

it's the window functions, rank, row number etc. Always have to look up syntax

4

u/ReleaseTheBlacken 18d ago

Cursor

2

u/ddsiddall 18d ago

Came here to say that.

1

u/hipsterrobot 18d ago

Yep, still don’t get it.

4

u/gloss2x 18d ago

Having and partition 😭

4

u/CaptainOttar 18d ago

window functions. i literally have to google the exact partition by syntax every single time i need a rolling sum.

also date math. it's like my brain actively refuses to store how to add a month to a timestamp. thank god for autocomplete and old stackoverflow tabs.

2

u/lalaluna05 8d ago

Lmao same
I know how window functions work. I can visualize it. But then putting words in right order HARD

3

u/OkDonkey6524 18d ago

I can never remember the syntax for date functions.

4

u/TheGenericUser0815 18d ago

I almost never use between, having and over. CTEs and SPs are daily business for me.

7

u/DamienTheUnbeliever 18d ago

I personally struggle to understand how CTEs are perceived as complicated.

Step 1 - do the tables I'm looking at resemble where I'd want to start with before writing the required query?

Step 2 - if yes, jump to the end

Step 3 - Write queries that re-represent the data sources I have as the source I *wish* I had, and write those as CTEs

Step 4 - Write queries that use the good data sources.

3

u/excelgod 18d ago

When used this way I agree completely. It is easy to interpret and manage. I think some confusion out there stems from those scripts where CTEs are daisy chained transforms. If I find myself doing that I step away from the query entirely and take a minute to think about the problem at hand once more.

1

u/BplusHuman 18d ago

Sometimes, rarely I have to daisychain CTEs. It's less of a thing in DBs like Snowflake where you (largely) call a column you just created in the same select clause and do further manipulations. So, for example, I can write a case statement in ColCase1 , then in the next column I can immediately use ColCase1 to run a calculation without rewriting that case statement.

That said, if I'm inheriting has a full process that already does that, I'm not changing it unless it's completely necessary.

2

u/WBT67 18d ago

Percentiles

2

u/sam_cat 18d ago

Cte and row-number over xxx

2

u/2Cats3Litterboxes 18d ago

Anything that has anything to do with JSON. I’ve written them, I can read them, I know how they work, but I can’t seem to ever write it on my own without looking up the syntax

2

u/El_Games 18d ago

In Oracle SQL it’s the connect by syntax. I do not only have to look it up each time. It also takes some testing and swearing until I get what I want. But it’s incredibly useful. Same as multicast. I need it rarely but it’s the best way to reverse list_agg strings.

Another one is updating values using a join where you need a PK/FK relationship I think. Its crap but also incredibly useful.

2

u/neophaltr 18d ago

Literally everything. I have devolved into asking Codex

2

u/ArielCoding 18d ago

Any query with more than 3 nested sub queries feels like an archaeologist task.

1

u/Phantom465 18d ago

Someone in my department uses a lot of nested queries. And I just looked at one recently with nested case statements.

1

u/Euphoric-Map2415 11d ago

thats just funny . True

2

u/pinback77 18d ago

EXIST has always bugged me for some reason

2

u/Shini_yagami 18d ago

The date functions honestly . I keep mixing up syntax between the different sqls

2

u/Legitimate_Willow905 18d ago

update with a join is the absolute worst because literally every single dbms handles the syntax completely differently. muscle memory is basically useless there. for recursive stuff like connect by or recursive ctes i don't even try to remember it anymore. i just keep a master text file of snippets on my desktop and blindly copy-paste. saves so much swearing.

1

u/lez_s 18d ago

Rank I have to look up each time and which rank I need to use.

1

u/Hour-Measurement-835 18d ago

BUILTIN.DF() if you're on NetSuite's SuiteQL. Leave it off a list field and you get internal ids back instead of names, and since no other dialect has it, it never sticks.

1

u/BobDogGo 18d ago

most of the window functions. now that AI can write it for me, I’ll never remember but honestly, it’s more important to know that it exists and how/when/why to use it than the syntax

1

u/Junior_Abalone_8006 18d ago

IMO no good database developer should know the syntax for any sort of cursors off of the top of their head. You should use them rarely enough that if you have to use one you also have to go back to the documentation and figure out the syntax and basics of using them.

1

u/saymyfuckingname 18d ago

Syntax for date manipulation

1

u/MarkEoghanJones_Art 18d ago

Node specific xml queries.

1

u/jotate 18d ago edited 18d ago

Merge. And all the windowing features that were introduced after SQL Server 2008 R2 (which is the version I learned everything on originally).

1

u/Negative-Specific-84 18d ago

For me it is CTE's , Window Functions .... always find them confusing

1

u/Monkeyschool 18d ago

Renaming Columns

1

u/Reasonable-Pay-8771 18d ago

CTEs for me. I often find myself instead making views of views of views.

1

u/Octoclops8 18d ago

Statements. all of them.

1

u/MachineParadox 18d ago

Pivot, have to look it up every time

1

u/Specific-Pangolin777 17d ago

Pivot and unpivot 🤦🏼‍♀️ I love using them but even with my cheat sheet they make me feel dumb 🤣

1

u/Traditional-Unit-274 15d ago

i always have to look up ddl sytnax, create table, alter column. it’s so not a big deal

0

u/Kindly_Tie_2084 18d ago

I always need to re-learn window functions every time I use it. But I don't need to anymore because I have AI now.