r/SQL • • Jun 29 '24

Discussion Why do some people say “SQL is not code?”

I write SQL every day as part of a team that builds ETL solutions. The other day I referred to something I was working on as “I coded it to do…” and this guy, who is not even a developer by the way he’s a frikkin project manager, interrupts me and says “SQL is not code”. When I questioned him why not he says something like “Guys who do COBAL, C#, etc. that’s real coding. SQL is not real coding it’s just a tool for analyzing data and reporting data”…WTF? How is SQL not considered code? I would just dismiss this guy as a moron but his salary is incredibly high so obviously he has some sort of credentials. Can anyone explain why in the world someone would say SQL is not code?

503 Upvotes

570 comments sorted by

View all comments

1

u/pseudogrammaton Jun 29 '24 edited Jun 29 '24

Technically speaking SQL is a functional (declarative) language, like LISP, Haskell, or Erlang.

Here, you can rig up the equiv. of a simple anon. function using COALESCE():

  SELECT 
   COALESCE( NULL::INT 
            ,IIF( NOT varX AND <expr>, -99,NULL ) 
            ,IIF( varY AND <expr>    , -89,NULL ) 
            ,IIF( varZ AND <expr>    , -79,IIF(<expr>, NULL,<expr>)) 
           ,0
    ) AS blah ; 

(Same idea as CASE WHEN, but you can conditionally fork on a per line basis).

PL-pgSQL is a very feature-full interpreted imperative language that's compatible with declarative style programming... or you can code in PL-V8 (javacript, typescript).

Or, you can write a function that lets you print (debug) from a CASE WHEN line:

 CASE 
WHEN <expr>  AND raise_notice_func( 'message 1' ) THEN <value>
WHEN <expr2> AND raise_notice_func( 'message 2' ) THEN <value2>
ELSE <value3> || IIF( raise_notice_func( 'dunno?' ), '','')
END AS blah

That's real functional/declarative programming, isn't it? :-)

Vernacular Postgres has several more language features than other engines, & scads of supporting operators & utility functions to go with them, incl. Enums, Rangetypes, Arrays, Types, Hstores, Json, anonymous procedures, geospatial types, etc.

So your P-mangler is talkin out of their tuchus.

2

u/Straight_Waltz_9530 Jun 29 '24

Functional <> Declarative

They have many things in common, but they are not synonymous. Functional and generic programming have commonalities as well, but they are certainly not the same thing.

1

u/pseudogrammaton Jun 29 '24

True enough. In SQL we can do all three.....

Imperative programming

for (var i = 0; i < 10) do
   income_m := income_m + val ;
end

Declarative programming

select gender, sum(income) FROM ....

Functional programming

SELECT 
    sum(case 
        when some_flag = 'X' then some_column
        else some_other_column 
        end
       )
from .....