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;
Related
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
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
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)
I have 3 tables. Two tables (par1, par1) both refer (child_id) to an id in the 3rd table (child).
I like to find orphans in the 3rd table, i.e. records that are not referenced by either of the other two tables.
If I had only once referencing table, I could write:
SELECT * FROM child WHERE id NOT IN (SELECT child_id FROM par1)
But how do I solve this for two referencing tables?
I'm using sqlite.
SELECT *
FROM child
WHERE id NOT IN (SELECT child_id FROM par1) AND
id NOT IN (SELECT child_id FROM par2)
An alternative is to use LEFT OUTER JOIN:
SELECT child.*
FROM child LEFT OUTER JOIN par1 ON (child.id = par1.child_id)
LEFT OUTER JOIN par2 ON (child.id = par2.child_id)
WHERE par1.child_id IS NULL AND par2.child_id IS NULL
Executing subqueries may or may not find the desired optimization paths in the SQLite index decision engine.
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;