r/SQL • • Sep 24 '26

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?

125 Upvotes

80 comments sorted by

176

u/ThatSpencerGuy Sep 24 '26

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 Sep 24 '26

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 Sep 24 '26

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 Sep 24 '26

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

88

u/GTS_84 Sep 24 '26

PIVOT

I can never remember the god damn syntax for pivoting.

12

u/phesago Sep 24 '26

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 Sep 24 '26

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

8

u/phesago Sep 24 '26

too bad the code block format is dog shit :|

3

u/Imaginary-poster Sep 24 '26

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 Sep 24 '26

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 Sep 25 '26

That’s the secret, no one does

1

u/MiserableLadder5336 Oct 04 '26

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

0

u/Wise-Jury-4037 :orly: Sep 24 '26

Use group by/case.

3

u/gumnos Sep 24 '26

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 Sep 25 '26

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

1

u/Wise-Jury-4037 :orly: Sep 25 '26

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

41

u/Jacob_OldStorm Sep 24 '26

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...

4

u/magicaltrevor953 Sep 24 '26

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 Sep 25 '26

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 Sep 25 '26

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 Sep 24 '26

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

22

u/Rough-Negotiation880 Sep 24 '26

Almost everything on a bad day.

17

u/DonJuanDoja Sep 24 '26

STUFF... for xml path...

2

u/DPool34 Sep 25 '26 edited Sep 25 '26

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

Edit: replaced version with engine

2

u/ComprehensiveDog7299 Sep 25 '26

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 Sep 25 '26

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.

22

u/more_paul Sep 24 '26

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

2

u/squareturd Sep 24 '26

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

11

u/you_are_wrong_tho Sep 24 '26

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

9

u/Zoidburger_ Sep 24 '26

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 Sep 24 '26

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

1

u/Zoidburger_ Sep 24 '26

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

1

u/OllyTwist Oct 17 '26

Date functions are the most annoying differences between systems for me.

22

u/Nurgus Sep 24 '26

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

9

u/excelgod Sep 24 '26

Comrade 🤝

7

u/phesago Sep 24 '26

i prefer the term "skwill"

0

u/Nurgus Sep 24 '26

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..

2

u/KittenBountyHunter Sep 25 '26

I have said it as “squeal”

7

u/ElHombrePelicano Sep 24 '26

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 Sep 24 '26

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

5

u/ReleaseTheBlacken Sep 24 '26

Cursor

2

u/ddsiddall Sep 24 '26

Came here to say that.

1

u/hipsterrobot Sep 25 '26

Yep, still don’t get it.

4

u/gloss2x Sep 24 '26

Having and partition 😭

4

u/CaptainOttar Sep 25 '26

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 Oct 04 '26

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

3

u/OkDonkey6524 Sep 24 '26

I can never remember the syntax for date functions.

5

u/TheGenericUser0815 Sep 24 '26

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

6

u/DamienTheUnbeliever Sep 24 '26

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 Sep 24 '26

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 Sep 24 '26

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 Sep 24 '26

Percentiles

2

u/sam_cat Sep 24 '26

Cte and row-number over xxx

2

u/2Cats3Litterboxes Sep 24 '26

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 Sep 24 '26

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 Sep 24 '26

Literally everything. I have devolved into asking Codex

2

u/ArielCoding Sep 25 '26

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

1

u/Phantom465 Sep 25 '26

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

2

u/pinback77 Sep 25 '26

EXIST has always bugged me for some reason

2

u/Shini_yagami Sep 25 '26

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

2

u/Legitimate_Willow905 Sep 25 '26

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 Sep 24 '26

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

1

u/Hour-Measurement-835 Sep 24 '26

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 Sep 24 '26

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 Sep 24 '26

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 Sep 25 '26

Syntax for date manipulation

1

u/MarkEoghanJones_Art Sep 25 '26

Node specific xml queries.

1

u/jotate Sep 25 '26 edited Sep 25 '26

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 Sep 25 '26

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

1

u/Monkeyschool Sep 25 '26

Renaming Columns

1

u/Reasonable-Pay-8771 Sep 25 '26

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

1

u/Octoclops8 Sep 25 '26

Statements. all of them.

1

u/MachineParadox Sep 25 '26

Pivot, have to look it up every time

1

u/Specific-Pangolin777 Sep 26 '26

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

1

u/Traditional-Unit-274 Sep 28 '26

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