pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SQL queries as sets: was The tragedy of SQL
6+ messages / 6 participants
[nested] [flat]

* SQL queries as sets: was The tragedy of SQL
@ 2021-09-15 04:54  Steve Litt <slitt@troubleshooters.com>
  0 siblings, 5 replies; 6+ messages in thread

From: Steve Litt @ 2021-09-15 04:54 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Rich Shepard said on Tue, 14 Sep 2021 05:49:07 -0700 (PDT)

>On Mon, 13 Sep 2021, Guyren Howe wrote:
>
>> They are making a decent decision. SQL is a *fucking terrible*
>> language, which I don’t blame them for not wanting to learn.  
>
>>> SQL is not the problem. Problem are the devs. I love SQL. I hate
>>> orms. The problem with databases is people refuse to treat it as
>>> the entity it is and want to use their beautiful OO system. Problem
>>> is databases are not OO. We need to recognize that and treat
>>> databases as databases.  
>
>Guyren/Hemil,
>
>As a non-SQL expert who's used postgres since 1997 I've come to
>believe the basic issue is that SQL is based on sets, neither
>procedural or object oriented. Few people think in sets so they try to
>fit SQL into what they know rather than understand the how sets work.

Rich, could you please elaborate on SQL queries being based on sets? I
never thought of it that way, and would like to hear your related
thoughts.

SteveT

Steve Litt 
Spring 2021 featured book: Troubleshooting Techniques of the Successful
Technologist http://www.troubleshooters.com/techniques





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: SQL queries as sets: was The tragedy of SQL
@ 2021-09-15 05:31  Guyren Howe <guyren@gmail.com>
  parent: Steve Litt <slitt@troubleshooters.com>
  4 siblings, 0 replies; 6+ messages in thread

From: Guyren Howe @ 2021-09-15 05:31 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org, Steve Litt <slitt@troubleshooters.com>

Oh, yeah, wow. Big topic.

My original post in the series is in significant part about how SQL hides this sort of thing from you.

A table is a set:  a set of true facts, all having the same structure, so you can operate on all of them with any operation on the individual rows.

Multiple tables, multiples facts. Sometimes about the same things, which is what a join does: the join key is the identifier of the things a bunch of statements are all about. A user has a name, and a login, and a creation date. When the identifier is the primary key, there is only such statement. When it’s a foreign key, there are multiple statements.

As an aside: it would be perfectly reasonable to have multiple tables with the same primary key. Or with the same foreign key. You could have only “foreign keys” on a bunch of different tables.

When you do a join, you’re finding all the “x and y” statements that the system knows to be true. That’s why a 1:many joins produces duplicate 1-side values in its results.

When you do a where, you’re reducing the number of rows by only finding the rows with a certain property. The joins and wheres together give you a very expressive logic engine.

There are two equivalent ways of looking at what the database does: it stores logical statements and acts as a logic engine; or it stores sets of tuples and gives you set operations on them.

Go read up on the basics of Datalog: it makes all of this beautifully obvious.

My original post was about how different the computer industry would be if only we’d made relations as easy as Datalog does. The entire industry would look different, just as garbage collection made the entire industry different.
On Sep 14, 2021, 21:55 -0700, Steve Litt <slitt@troubleshooters.com>, wrote:
> Rich Shepard said on Tue, 14 Sep 2021 05:49:07 -0700 (PDT)
>
> > On Mon, 13 Sep 2021, Guyren Howe wrote:
> >
> > > They are making a decent decision. SQL is a *fucking terrible*
> > > language, which I don’t blame them for not wanting to learn.
> >
> > > > SQL is not the problem. Problem are the devs. I love SQL. I hate
> > > > orms. The problem with databases is people refuse to treat it as
> > > > the entity it is and want to use their beautiful OO system. Problem
> > > > is databases are not OO. We need to recognize that and treat
> > > > databases as databases.
> >
> > Guyren/Hemil,
> >
> > As a non-SQL expert who's used postgres since 1997 I've come to
> > believe the basic issue is that SQL is based on sets, neither
> > procedural or object oriented. Few people think in sets so they try to
> > fit SQL into what they know rather than understand the how sets work.
>
> Rich, could you please elaborate on SQL queries being based on sets? I
> never thought of it that way, and would like to hear your related
> thoughts.
>
> SteveT
>
> Steve Litt
> Spring 2021 featured book: Troubleshooting Techniques of the Successful
> Technologist http://www.troubleshooters.com/techniques
>
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: SQL queries as sets: was The tragedy of SQL
@ 2021-09-15 06:04  Brent Wood <Brent.Wood@niwa.co.nz>
  parent: Steve Litt <slitt@troubleshooters.com>
  4 siblings, 0 replies; 6+ messages in thread

From: Brent Wood @ 2021-09-15 06:04 UTC (permalink / raw)
  To: Steve Litt <slitt@troubleshooters.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

I agree.

I've always thought SQL was a great example of set theory put into practice. You specify the elements (select items) and conditions (where clause) to return the (sub)sets you want.

Spatial data is also about sets - a polygon is theoretically defined as the set of points contained by a perimeter. A line is the set of points between specified points (complicated somewhat by coordinate systems). I have found the set based approach of SQL & relational databases is a very comfortable fit with spatial data queries - Postgres + Postgis is an awesome combination, then add hstore, jsonb & timescale to the mix and I have no need for a NoSQL db for key/value rather than normalised data.

I also suggest that as a simple language to retrieve stored data, SQL works well. But as we try to do more complex retrievals, the difference between a query to ask for data and a query to ask for a somewhat complex analysis of the data becomes very apparent. Yet these extensions, as is so often the case, are where the true power and the limitations of SQL can be found.

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: SQL queries as sets: was The tragedy of SQL
@ 2021-09-15 08:51  rob stone <floriparob@gmail.com>
  parent: Steve Litt <slitt@troubleshooters.com>
  4 siblings, 0 replies; 6+ messages in thread

From: rob stone @ 2021-09-15 08:51 UTC (permalink / raw)
  To: Steve Litt <slitt@troubleshooters.com>; pgsql-general@lists.postgresql.org


> 
> Rich, could you please elaborate on SQL queries being based on sets?
> I
> never thought of it that way, and would like to hear your related
> thoughts.
> 
When Codd & Date elaborated the relational model, it was based on set
theory.
You have sets of data. Is there a relationship between them? If so, how
do you express it? SQL came to exist.

My two cents.


> SteveT
> 
> Steve Litt 
> Spring 2021 featured book: Troubleshooting Techniques of the
> Successful
> Technologist http://www.troubleshooters.com/techniques
> 
> 






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: SQL queries as sets: was The tragedy of SQL
@ 2021-09-15 13:01  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Steve Litt <slitt@troubleshooters.com>
  4 siblings, 0 replies; 6+ messages in thread

From: Rich Shepard @ 2021-09-15 13:01 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Wed, 15 Sep 2021, Steve Litt wrote:

> Rich, could you please elaborate on SQL queries being based on sets? I
> never thought of it that way, and would like to hear your related
> thoughts.

SteveT,

In the 1980s, when there were computer magazines such as Byte and Database
Administrator (among many others), Joe Celko wrote a monthly database column
in one (or more, I forget these details). This lead me to buy and read his
books, including 'SQL for Smarties' and its many editions, 'SQL
Programming', and 'Thinking in Sets: Auxilliary, Temporal, and Virtual
Tables in SQL'.

I reccomend his books to answer your questions and give you a sound
foundation.

Regards,

Rich





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: SQL queries as sets: was The tragedy of SQL
@ 2021-09-15 15:29  Raymond Brinzer <ray.brinzer@gmail.com>
  parent: Steve Litt <slitt@troubleshooters.com>
  4 siblings, 0 replies; 6+ messages in thread

From: Raymond Brinzer @ 2021-09-15 15:29 UTC (permalink / raw)
  To: Steve Litt <slitt@troubleshooters.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Wed, Sep 15, 2021 at 12:55 AM Steve Litt <slitt@troubleshooters.com> wrote:
> Rich, could you please elaborate on SQL queries being based on sets? I
> never thought of it that way, and would like to hear your related
> thoughts.

I'll take a crack at this.  Going through the setup will require a
little patience, but I think the underlying idea is neat enough to be
worth it.

A set is an unordered collection of unique elements.  This means:

{ 1, 2, 3 } = { 3, 2, 1 }
{ 2, 3, 3 } is not a set.

(The elements don't have to be numbers, but I'm using them for convenience.)

Take two sets, A and B:

A = { 1, 2, 3 }
B = { 4, 5, 6 }

The Cartesian product A x B is the complete set of ordered pairs (a,
b) you can make from them:

(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)

These pairs are called tuples.  Tuples don't have to be pairs; if you
were using A x B x C, for instance, each one would have three
elements.

A relation is a subset of the Cartesian product of the sets.  For instance:

(1, 4)
(2, 5)
(3, 6)

(In math, some relations are functions; that one happens to be a
function which follows the rule f(a) = a + 3.  But that's not really a
database matter.)

Anyway, now: for "relation", say "table", and for "tuple", say "row".
This is (in theory) is what a relational database is about: storing
relations.

In math, tuples are ordered, but with databases we give the elements
names, instead:

(a: 1, b: 4)

It doesn't really matter, because the names uniquely identify the
elements, just as ordering does.  You can go back and forth between
orders and names, so the different representations have the same
structure.

So, let's say you do this:

CREATE TABLE whatever (a INTEGER, b STRING, UNIQUE(a,b));

What you are saying, in a sense, is:  "Consider the set of tuples
which pair every possible integer with every possible string.  I'm
going to be storing a subset of that."

What's interesting about this is:  because you're working with sets,
all the operations you can perform on sets work here.  Tables X and Y
are both sets of tuples, so you can find their cartesian product,
intersection, union, and so forth.  Proofs about sets apply, and so
on.

That is, if the tables are actually relations... which means they have
to be sets.  This is where things get controversial.

SQL allows tables to have duplicate records.  It also has NULL, which
allows duplicate records even in a table like the one above.  Although
we declared UNIQUE(a,b), we can still say:

INSERT INTO whatever VALUES (NULL,NULL), (NULL,NULL);

So, arguably SQL isn't actually relational.  To some, that's a matter
of it being practical, so that people can actually get work done.  To
others, it's a travesty which robs databases of power and clarity.

Anyway, that's my summary.

-- 
Ray Brinzer





^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2021-09-15 15:29 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-09-15 04:54 SQL queries as sets: was The tragedy of SQL Steve Litt <slitt@troubleshooters.com>
2021-09-15 05:31 ` Guyren Howe <guyren@gmail.com>
2021-09-15 06:04 ` Brent Wood <Brent.Wood@niwa.co.nz>
2021-09-15 08:51 ` rob stone <floriparob@gmail.com>
2021-09-15 13:01 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-09-15 15:29 ` Raymond Brinzer <ray.brinzer@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox