Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lIxwB-0003fD-QI for pgsql-novice@arkaria.postgresql.org; Sun, 07 Mar 2021 18:15:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lIxw9-0006Zr-8o for pgsql-novice@arkaria.postgresql.org; Sun, 07 Mar 2021 18:15:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lIxw8-0006Zj-Vf for pgsql-novice@lists.postgresql.org; Sun, 07 Mar 2021 18:15:29 +0000 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lIxw5-0007Sq-M5 for pgsql-novice@lists.postgresql.org; Sun, 07 Mar 2021 18:15:28 +0000 Received: by mail-ed1-x533.google.com with SMTP id b7so11370089edz.8 for ; Sun, 07 Mar 2021 10:15:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-transfer-encoding; bh=h8g2zHJDW7o8I+MNqDbNjBb+RzAeb7xvJFopXe0GldI=; b=WORJUc3qvdsiYNe3GLUpk3Q2Oal0Xw5icq/FoaxkH0u1iR2T7rSowDb7vwoqsp4B5i bODIBO3R1uxTd4KlZG/u1/vC/5XVdBYmUpIg07WNGUs84xPcBxBN5d4x3dovlnpAgJgT Pi5NdIlDH6hruCZyLxiBcauzJL4MU5JYIjyvgLmaei7/8LhHB+viAKdJrNKtZWIDDMB6 Ajbvm7xQ3tQyk8EUSdRtq31nfK+MQD9NLQnSKEFubUKhrfEeRvkF8aTf6YK5l2rdlOrk 6F5uYwvisgccg6Ic7b0YTPFE95Ay3dkzwafrXTiKxIxjYsbj01TlFFF+KfBwvy2PFddg /KCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=h8g2zHJDW7o8I+MNqDbNjBb+RzAeb7xvJFopXe0GldI=; b=heF/H7AMum1DZx2mPhxVsJoZSx7c8hyQteW/uvRcIxw9RoY/vUeKFjq/BJkyEAbrmV M+rRqx4+Lc3f4iUNVN0bizc6U+fgNM+yQDBMfYMZwVGT10PUmJJpMQs42TcBFiShfZDs RrYaZPxOj1+yNZ9CZwcB+GtWiUBbSR6OE+uGNgdTcHgkIDKiQthHRoGAEgYMXvNGj0LD T//jRwT5fEOtKU+2dhSrYT3NqoY84/u+hWMB3qooeQH32oQoyRSZs6YC2KLmaFy9KVnU leta0+BZUZe1X54U+wUZx5A9JIk3VlW64mroqpkH2Q8i1CRE/teTRUW3rN8Th0WHgcFB l7kg== X-Gm-Message-State: AOAM531Y270hWSLeJhZf9bFMiy9Bg6Osfj0Btg7MYdpmnrU0y44e/dB6 eNwhfxIXxetkXtpDr5UUmTmgkCwDWFL5 X-Google-Smtp-Source: ABdhPJzv3yntvAnQkZVh3Zl8jeCG2JKRVjLaXlEerPggmHelI61bjNR3sMx6dw6Zc3q6sL5ylW+Mlg== X-Received: by 2002:a50:eb97:: with SMTP id y23mr18893872edr.170.1615140924118; Sun, 07 Mar 2021 10:15:24 -0800 (PST) Received: from derhulk ([2a02:8109:b640:2344::e563]) by smtp.gmail.com with ESMTPSA id i10sm5198274ejv.106.2021.03.07.10.15.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Mar 2021 10:15:23 -0800 (PST) From: Narendra Joshi To: =?utf-8?Q?P=C3=B3l?= Ua =?utf-8?B?TGFvw61uZWNow6Fpbg==?= Cc: pgsql-novice@lists.postgresql.org Subject: Re: LEFT or RIGHT JOIN - can't see where I'm going wrong? References: Date: Sun, 07 Mar 2021 19:15:18 +0100 In-Reply-To: (=?utf-8?Q?=22P=C3=B3l?= Ua =?utf-8?Q?Lao=C3=ADnech=C3=A1in=22's?= message of "Sat, 23 Jan 2021 08:23:16 +0000") Message-ID: <87im623k5l.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk P=C3=B3l Ua Lao=C3=ADnech=C3=A1in writes: > Hi all, > > I have a problem which *_should_* be very simple, but I'm stuck! > > I have 3 tables (DDL and DML shown below and in the fiddle here): > > https://dbfiddle.uk/?rdbms=3Dpostgres_12&fiddle=3Db4e069919fc2052938196bc= afe47a043 > > student, quiz and student_score > > student PK =3D (s_id), quiz (PK =3D q_id), and student_score is a JOINing > table (Associative Entity) with a PK of (ss_s_id, ss_q_id). > > Now, I have 3 students: > > INSERT INTO student VALUES > (12345678, 'Student1_name'), > (40204123, 'Student2_name'), > (40213894, 'Student3_name'); > > and three quizzes: > > INSERT INTO quiz (q_id, q_title) > VALUES > (1, 'Quiz 1'), > (2, 'Quiz 2'), > (3, 'Quiz 3'), > (4, 'Quiz 4'); As long as there is at least on entry in `student_score` for a given Quiz, there won't be any NULL values for the quiz row in the final query that you have. I think what you are trying to achieve can be done with a query like THIS:=20 #v+ SELECT q.*, s.*, ss.* FROM quiz q CROSS JOIN student s LEFT JOIN student_score ss ON ss.ss_q_id =3D q.q_id AND ss.ss_s_id =3D s.s_id; #v- Best, > And for 1 student - no. 40204123, I have results for 3 quizzes: > > INSERT INTO student_score (ss_s_id, ss_q_id, points, ss_ts) VALUES > (40204123, 1, 80, '2021-01-12 15:37:11'), > (40204123, 2, 75, '2021-01-12 15:38:06'), > (40204123, 3, 30, '2021-01-13 22:13:13'); > > Now, I have a query: > > SELECT q.*, ss.*, s.* > FROM quiz q > LEFT JOIN student_score ss > ON q.q_id =3D ss.ss_q_id > LEFT JOIN student s > ON ss.ss_s_id =3D s.s_id > WHERE s.s_id =3D 40204123 > ORDER BY q.q_id; > > And the result is (hope alignement is OK - or see the fiddle): > > q_id q_titles s_s_id ss_q_id pointsss_ts > s_id s_name > 1 Quiz 1 40204123 1 80 2021-01-12 15:37:11 > 40204123 Student2_name > 2 Quiz 2 40204123 2 75 2021-01-12 15:38:06 > 40204123 Student2_name > 3 Quiz 3 40204123 3 30 2021-01-13 22:13:13 > 40204123 Student2_name > > What I want is a result for quiz 4 with NULL for the student_points > fields but including the student_id and esp. the name - I'll put > COALESCE in for the NULL grade for quiz 4. > > Fiddle: > > https://dbfiddle.uk/?rdbms=3Dpostgres_12&fiddle=3Db4e069919fc2052938196bc= afe47a043 > > I'm beating my head off a wall here - I"m sure that it's quite easy - > just one of those days... cabin-fever maybe... :-) > > If there's a better way of formulating the schema, I'm all ears - but > I'd also like a solution - with an explanation just in case - I'll > probably have a Homer moment, but just in > case. > > If you require any further information, please don't hesitate to > contact me on-list. > > TIA and rgs, > > P=C3=B3l... > --=20 Narendra Joshi