Hacker News new | ask | show | jobs
by zepolen 746 days ago
I love SQL, I think it's fantastic, it's also the one skill I learned 30 years ago that still applies today and that I, personally, have used across at least six different databases.

The critiques in that link show a fundamental lack of understanding, eg. it poses that the following query should be allowed and wonders why sql complains about fetching more than one row since avg requires more than one row, but the reality is that what they are describing is avg(rows of rows) rather than avg(rows) which obviously doesn't make sense:

   SELECT
     AVG(
       SELECT SUM(amount) FROM purchases GROUP BY customer
     )
But even worse, the article says that the only solution is using a CTE(!?) and doesn't mention the obvious use of a subquery:

  SELECT
    AVG(total)
  FROM (
    SELECT
      SUM(amount) total_per_customer
    FROM
      purchases
    GROUP BY customer
  );
In my experience I've seen that usually the people that find it difficult to understand SQL are also the people that will be writing .fetch_by_id functions instead of .fetch_by_filters, ie. they consider database tables and data in general as 1d arrays in imperative programming and only think in those terms rather than the 2d sets they are. I'd wonder how their mind would melt if they'd tried to understand querying for 3d data, eg. temporal databases.

Also I fail to see how KQL is any different to SQL, it's pretty much the same thing with the exception that the selection comes afterwards, is that the factor that makes you hate SQL?

From the link you posted:

  StormEvents 
  | where StartTime between (datetime(2007-11-01) .. datetime(2007-12-01))
  | where State == "FLORIDA"  
  | count 
vs

  FROM   StormEvents
  WHERE  StartTime BETWEEN '2007-11-01' AND '2007-12-01'
  AND    State = 'FLORIDA'
  SELECT count(*)
and

  StormEvents
  | where DamageCrops > 0
  | summarize
      MaxCropDamage=max(DamageCrops), 
      MinCropDamage=min(DamageCrops), 
      AvgCropDamage=avg(DamageCrops)
      by EventType
  | sort by AvgCropDamage
vs

  FROM     StormEvents
  WHERE    DamageCrops > 0
  SELECT   max(DamageCrops) MaxCropDamage, 
           min(DamageCrops) MinCropDamage, 
           avg(DamageCrops) AvgCropDamage
  GROUP BY EventType
  ORDER BY AvgCropDamage
1 comments

To be clear, I don't find SQL difficult to understand. I've used it for 20+ years and I can always get the query to generate my desired output. But I often find that the language is a hindrance, and that I can more efficiently reach my desired output using modern languages.

for example, here's the KQL equivalent to the 'average of sums' query:

  purchases
  | summarize total_per_customer=sum(amount) by customer
  | summarize avg(total_per_customer)
I find this more elegant, and I'd prefer authoring it over any of the equivalent SQL solutions previously mentioned.