SQL get Parent where Children have specific values
You can do two joins and look for your specific records. This example shows that parent 1 will return with both kiddos, but not parent 2 that only has a Mike.
DECLARE @parent TABLE (ID INT)
DECLARE @child TABLE (ID INT, parentID INT, name VARCHAR(100))
INSERT INTO @parent
VALUES
(1),
(2),
(3),
(4),
(5),
(6)
INSERT INTO @child (ID, parentID, name)
VALUES
(1, 1, 'Mike'),
(2, 1, 'John'),
(3, 2, 'Mike'),
(4, 2, 'Bill'),
(5, 3, 'Dave'),
(6, 4, 'Sam')
SELECT p.*
FROM @parent p
INNER JOIN @child c1
ON c1.parentID = p.id
AND c1.name = 'Mike'
INNER JOIN @child c2
ON c2.parentID = p.ID
AND c2.name = 'John'
Query to get parent where all the children have a certain value
Your query is on the right track in getting the results, it just needs a further filter to see there are no other records other than Test1 for the records you selected from CHILD.
We can do that using a NOT EXISTS condition
select distinct p.*
from PARENT p
join CHILD c1
on p.p_id = c1.p_id
where c1.category = 'Test1'
and not exists(select c2.id
from CHILD c2
where c2.p_id=c1.p_id
and c2.category <> 'Test1')
Select all parents having a child with specific property
You can use exists
:
select p.*
from parent p
where exists (select 1
from child c
where c.parent_id = p.id and c.property = ?
);
Select parent records where ALL children records have a specific status
You can check that no not-closed record exists using NOT EXISTS query:
SELECT *
FROM TableA
WHERE NOT EXISTS (
SELECT 1
FROM TableB
WHERE TableB.ParentID = TableA.ID
AND TableB.Status <> 'closed'
)
SQL Query to select parent that has child records with different values in the same column
You need to leverage subqueries (or union) which flavor of sql are you using?
SELECT *
FROM CUSTOMER c
WHERE EXISTS (SELECT 1 FROM ORDERS o WHERE c.id = o.id and type = 'couch')
and EXISTS (SELECT 1 FROM ORDERS o WHERE c.id = o.id and type = 'tv')
Find parents where all children have the same value
You can try grouping by the company name and retaining only those companies whose address is the same across all locations (this is what the inner query does):
WITH the_cte (companyName, locationName, address)
AS
(
SELECT c.name, l.name, a.name
FROM company AS c
INNER JOIN location AS l
ON l.company = c.id
LEFT JOIN address AS a
ON a.location = l.id
)
SELECT companyName, locationName, address
FROM the_cte
INNER JOIN
(
SELECT companyName, COUNT(DISTINCT CASE WHEN address IS NULL THEN 0 ELSE 1 END)
FROM the_cte
GROUP BY companyName
HAVING COUNT(DISTINCT CASE WHEN address IS NULL THEN 0 ELSE 1 END) = 1
) t
ON the_cte.companyName = t.companyName
SQL: Get parent if and only if children have a certain property
Use a NOT EXISTS clause, like this:
SELECT id
FROM asset A
INNER JOIN resource R ON A.id = R.asset_id
WHERE NOT EXISTS (
/* Only return results if the asset has resources of all status = '1' */
SELECT 1
FROM Resources R2
WHERE A.asset_id = R2.asset_id
AND R2.status <> '1'
)
Related Topics
Exclude a Column Using Select * [Except Columna] from Tablea
Mysql - Left Join Takes Too Long, How to Optimize Query
Sql Query to Get the Employee Name and Their Manager Name from the Same Table
Inserting Date Value into Date Field Using Laravel
How to Close Idle Connections in Postgresql Automatically
Nodejs, MySQL - Json Stringify - Advanced Query
Mysql Workbench Edit Table Data Is Read Only
How to Convert a Timestamp (Date Format) to Bigint in SQL
Daily Report by Date With Mssql for Mutiple Column
How to Find Multiple Occurrence of Particular String and Fetch Value in SQL Server
How to Check If More Than One Column Is Not Null Per Record
Postgres: Insert If Does Not Exist Already
Count All Records Per Day in a Specific Month
How to Count Number of Digits After a Decimal Place
How to Select the Last Record from MySQL Table Using SQL Syntax
Sql Select Last N Rows, Sort Them Reversed
Update Only Time from My Datetime Field in SQL
Sqlstate[Hy000] [2002] Php_Network_Getaddresses: Getaddrinfo Failed: Name or Service Not Known