Join with null values - sql

I have two tables Parent and child table, child table contains foreign key constraint.
Now I want to get data from child table with join to parent table and also want the records where parent table id is null from child table.

select *
from child c
left join parent p on c.id = c.parent_id;

Related

Having trouble self joining a table with NULL values

I have a single table that has the columns: personID, name, parentID1 and parentID2
Essentially I am looking to use this table to find a childs parent(s)
Not every child has both parents, so these values are NULL in the table. I have tried using self joins but it just is not working out for me.
TABLE
Here is what the table should look like:
Need to use LEFT joins so each row is returned even if the there is no match in the right table
List Parents for Each Person Even if 1 Parent is NULL
SELECT A.[name] AS Child
,B.[name] AS Parent1
,C.[name] AS Parent2
FROM YourTable AS A
LEFT JOIN YourTable AS B
ON A.ParentID1 = B.PersonID
LEFT JOIN YourTable AS C
ON A.ParentID2 = C.PersonID
WHERE A.ParentID1 IS NOT NULL
OR A.ParentID2 IS NOT NULL

Join SQL output of a N-M relationship table [duplicate]

I want to join a table which has multiple rows and need to merge one of the column from all the rows into single column.
select a.parent_id,a.parent_name,concat(b.child_name) from parent a
join children b on (a.parent_id=b.parent_id);
This should return all the parent rows and each parent row should have all its children's.
i am thinking to group with parent_id but getting multiple records (one record per child). What logic i can implement here apart from grouping to get all child's for a parent in single row.
SELECT a.parent_id, a.parent_name, STRING_AGG(b.child_name, ',') as Children
FROM
Parent a
INNER JOIN children b
ON a.Id = b.ParentId
GROUP BY
a.parent_id
,a.parent_name

How to create view with select two relation table one to one, and fill all null value with parent value - postgreSQL

I have 2 tables, structured like this:
table A table B
- id - id
- name - name
- parent
- fk_b
so in table A there are parent and fk_b. for fk_b it can be null.
so if in table A (child) the fk_b is null will refer to table A (parent), for example on of the row in the table A (parent) has fk_b.
Note:
This parent contains the id of table A
my question is, how can we select table A join table B. But every table A with null fk_b will retrieve fk_b from its parent?
Thank you
You can self-join the table to retrieve the parent row, and implement conditional logic in the on clause of the join on tableb, like so:
select a.*, b.name
from tablea a
left join tablea p on p.id = a.parent
left join tableb b on b.id = coalesce(a.fk_b, p.fk_b)

Deleting rows from parent table with Foreign Key in SQL

I have a Parent Table with Foreign Key and a Child Table with Primary Key. I want to delete only those rows of Parent Table which have no corresponding rows in Child table. I want to implement this in SQL Stored Procedure.
Normally, you would just use not exists:
delete parent
where not exists (select 1
from child
where child.parentid = parent.id
);
You can also use joins (i.e. left join)
delete p from parent p
left join child c on c.parentid = p.parentid
where c.parentid is null;

Pairing two records using Grandparent record? SQL

All,
I am trying to pair two or more records linked by a common Grandparent record.
At the time I run my query I will only know one of the records in the tuple / pair. Therefore I need to use this child record to navigate to the Grandparent record.
From the grandparent record I will then need to find all other Grandchildren records.
Here is a rather crude drawing of the structure
So far in my query I can find the Grandparent record but I do not know what to do from that point to find all other grand children
SELECT * FROM TABLE A
WHERE CREATED_DATE = sysdate
JOIN TABLE B
ON TABLE A.parent_row = TABLE B.row_id
JOIN TABLE C
//Grandparent table
ON TABLE B.parent_row = TABLE C.row_id
If there is a better way to do this? My only main filter in this query is that I am searching for records created today in Table A.
Thanks
First, your query is invalid. The where clause comes after the joins:
SELECT *
FROM TABLE A
JOIN TABLE B ON TABLE A.parent_row = TABLE B.row_id
JOIN TABLE C ON TABLE B.parent_row = TABLE C.row_id
WHERE CREATED_DATE = sysdate;
Secondly, you didn't filter by the grand-child you apparently have already. In this query, I use the YOUR_ID variable to represent it:
SELECT *
FROM TABLE A
JOIN TABLE B ON TABLE A.parent_row = TABLE B.row_id
JOIN TABLE C ON TABLE B.parent_row = TABLE C.row_id
WHERE
CREATED_DATE = sysdate
AND TABLE A.row_id = YOUR_ID;
Thirdly, now that we retrieved the grand parents (table C.row_id), we may follow the same logic to join over again with table B (BB) and A (AA) to get all grand children:
SELECT TABLE AA.row_id /* ids of all grand children. YOUR_ID should be included */
FROM TABLE A
JOIN TABLE B ON TABLE A.parent_row = TABLE B.row_id
JOIN TABLE C ON TABLE B.parent_row = TABLE C.row_id
/* join over on table B then table A to get all grand children */
JOIN TABLE BB ON TABLE BB.parent_row = TABLE C.row_id
JOIN TABLE AA ON TABLE AA.parent_row = TABLE BB.row_id
WHERE
CREATED_DATE = sysdate
AND TABLE A.row_id = YOUR_ID;