News for hackers. Stuff that matters.
Re:SQL Design Patterns by sgarland on Saturday August 30, 2025 @06:44PM (#45078627)
Frankly, this is terrible advice. If you’re not designing your data model around the language it’s going to be queried in, how do you expect to get decent performance out of the database?

Also, in no way does SQL hide anything - it’s a declarative language, and will produce exactly what you tell it to, provided you understand what it is you asked it to do. The query engine is somewhat of a black box, but that is completely orthogonal.

Re:SQL Design Patterns by paulddraper on Saturday August 30, 2025 @07:02PM (#45078761)
SQL is a declarative language so it —- by definition —- hides the execution.

Not really sure what you’re trying to argue here.

Re:SQL Design Patterns by sgarland on Saturday August 30, 2025 @08:28PM (#45079245)
Parent made it sound - to me - that you put an input in and hope for the best. If you understand the operators, you can quite confidently predict an output given an input.
Re:SQL Design Patterns by halfcat on Saturday August 30, 2025 @09:12PM (#45079453)
> If you understand the operators

That’s the point. In an imperative language if you don’t yet understand (or make a typo, or whatever), you can just print/console.log and find out.

I’ve seen junior devs, data analysts, and LLMs spin their wheels trying to figure out why adding a join isn’t producing the output they want. I don’t think they would figure it out using SQL alone if you gave them a month.

Re:SQL Design Patterns by SkiFire13 on Sunday August 31, 2025 @03:44AM (#45081238)
The equivalent of `print`/`console.log` in SQL would be using subqueries/CTE and run them to see the intermediate result (just like `print`/`console.log` show you intermediate results of the executions in an imperative language).
Re:SQL Design Patterns by sgarland on Sunday August 31, 2025 @10:20AM (#45083340)
Then you back off, and go back to first principles. Create the minimum example of the problem, and as a sibling comment mentioned, break it down to its constituent parts and observe what happens in each.
Re:SQL Design Patterns by crazygringo on Saturday August 30, 2025 @10:16PM (#45079749)
You missed the "performance" part.

Depending on how you write your query and how you structure your data, a query can take 0.005 seconds or 500 seconds.

SQL hiding the execution is an extremely leaky abstraction. To get the performance you need, you have to plan your possible queries in advance together with how to structure the data.

I mean, it doesn't matter if you only have 100 rows per table, but once you're dealing with multiple tables with millions of rows each it's everything.

Re:SQL Design Patterns by greekrich92 on Sunday August 31, 2025 @10:49AM (#45083574)
If you use bubblesort instead of quicksort it will take longer as well. Knowing the language and understanding the schema solves this.
Re:SQL Design Patterns by teh_klev on Sunday August 31, 2025 @07:25AM (#45082341)
>hides the execution.

But you're not prevented from finding out how your query was executed. For example EXPLAIN (MySQL, Postgres) or query analyser for MSSQL.

Re:SQL Design Patterns by chasil on Saturday August 30, 2025 @10:37PM (#45079856)
> Also, in no way does SQL hide anything - it’s a declarative language, and will produce exactly what you tell it to.

Ha ha, no, SQL implementations can conform to the standard in unexpected ways.

NULL = NULL

Is that true or false? We didn't know until 2003.

https://en.wikipedia.org/wiki/Null_(SQL)#Criticisms

Re:SQL Design Patterns by gfody on Saturday August 30, 2025 @11:45PM (#45080197)
that's not true or false it's null
Re:SQL Design Patterns by sgarland on Sunday August 31, 2025 @10:18AM (#45083323)
Considering that all implementations I’m aware of have evaluated that expression to NULL, this isn’t a very pragmatic example. NULL is unknown, and every type has it as a possibility.

The SQL2003 changes were more about how constraints should treat NULL than anything intrinsic about NULL itself.