Z
Z
zlodiak2019-03-27 16:14:14
SQL
zlodiak, 2019-03-27 16:14:14

Why doesn't left join work?

there is a code:

CREATE TABLE NAMES(pk integer PRIMARY KEY, Name text);
CREATE TABLE CITIES(fk integer, Name text);

INSERT INTO NAMES VALUES(1,'Tom');
INSERT INTO NAMES VALUES(2,'Lucy');
INSERT INTO NAMES VALUES(3,'Frank');

INSERT INTO CITIES VALUES(1,'Moscow');
INSERT INTO CITIES VALUES(3,'Saransk');
INSERT INTO CITIES VALUES(4,'Penza');
INSERT INTO CITIES VALUES(1,'Samara');

SELECT * FROM NAMES LEFT JOIN CITIES WHERE pk = fk

as I understand it, there should be records with NULL in the selection because the where condition is not met for name.lucy. the problem is that the service does not output these values ​​with NULL.
1|Tom|1|Moscow
1|Tom|1|Samara
3|Frank|3|Saransk

Please tell me, is it a matter of the implementation of sql specifically in this service, or do I not understand how left join works?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Talyan, 2019-03-27
@zlodiak

Firstly, LEFT JOIN Cities ON citiec.fk=Names.pk
Secondly, your tables are strange, in the Cities table, where?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question