Re: Proper relational database?

From: Geoff Winkless <pgsqladmin(at)geoff(dot)dj>
To: Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com>, Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Proper relational database?
Date: 2016-04-24 11:29:37
Message-ID: CAEzk6fcTC-MGmcG86ZRAfwuyy5X6uC8noDy1W+zAHE0RhLHtFw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 24 April 2016 at 08:36, Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com> wrote:
> For example, say we want all students who have one or more exam today:
>
> SELECT s.student_id, s.name
> FROM student s
> WHERE EXISTS (SELECT 1
> FROM exam e
> WHERE e.student_id = s.student_id
> AND e.exam_date = CURRENT_DATE)

To be fair to SQL a far simpler version would be

SELECT DISTINCT student_id, name
FROM student
INNER JOIN exam USING(student_id)
WHERE exam_date=CURRENT_DATE;

To find students with no exam today (the other point of your argument):

SELECT student_id, name
FROM student
LEFT JOIN exam USING(student_id)
WHERE exam_date=CURRENT_DATE AND exam.student_id IS NULL;

Not really sure what the issue is with either of those. Once learned,
they're both very easy and straightforward.

Geoff

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message david 2016-04-24 12:10:20 Re: Proper relational database?
Previous Message Tomas J Stehlik 2016-04-24 10:54:09 Re: Is it possible to recover the schema from the raw files?