Skip to content

Commit 8d0045e

Browse files
Added Child - Parent - Grandparent Hierarchy
1 parent 1dd9910 commit 8d0045e

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Child - Parent - Grandparent Hierarchy
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
[![View Main Folder](https://img.shields.io/badge/View-Main_Folder-971901?)](https://github.com/thecoddiwompler/SQL-Practice-Questions/tree/main)
4+
[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/thecoddiwompler?tab=repositories)
5+
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/thecoddiwompler)
6+
7+
---
8+
9+
## 🛠️ Problem Statement
10+
11+
12+
<b>Table Name : persons</b>
13+
</br>
14+
| Column Name |Type |
15+
| ------------- | ------------- |
16+
| person | varchar(10) |
17+
| parent | varchar(10) |
18+
| person_status | varchar(10) |
19+
20+
21+
This table provides Person's Name, their Parent's name, and the Person's Status (If they are Alive or not).
22+
<br/>
23+
</br>
24+
<b>write an SQL query to return count of people whose grandparent is alive.</b>
25+
</br>
26+
</br>
27+
<b>The query result format is in the following example: </b>
28+
</br>
29+
</br>
30+
31+
<details>
32+
<summary>
33+
Input
34+
</summary>
35+
<br>
36+
<b>Table Name : persons</b>
37+
<br><br>
38+
39+
| person | parent | person_status |
40+
|--------|--------|---------------|
41+
| A | X | Alive |
42+
| B | Y | Dead |
43+
| X | X1 | Alive |
44+
| Y | Y1 | Alive |
45+
| X1 | X2 | Alive |
46+
| Y1 | Y2 | Dead |
47+
48+
49+
50+
51+
</details>
52+
53+
<details>
54+
<summary>
55+
Output
56+
</summary>
57+
<br>
58+
59+
| person_count |
60+
| ---- |
61+
| 1 |
62+
</details>
63+
64+
---
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE TABLE persons (
2+
person varchar(10),
3+
parent varchar(10),
4+
person_status varchar(10)
5+
);
6+
7+
INSERT INTO
8+
persons (person, parent, person_status)
9+
VALUES
10+
('A', 'X', 'Alive'),
11+
('B', 'Y', 'Dead'),
12+
('X', 'X1', 'Alive'),
13+
('Y', 'Y1', 'Alive'),
14+
('X1', 'X2', 'Alive'),
15+
('Y1', 'Y2', 'Dead');
16+
17+
COMMIT;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT
2+
COUNT(a.person) person_count
3+
FROM
4+
persons a
5+
INNER JOIN persons b ON a.parent = b.person
6+
INNER JOIN persons c ON b.parent = c.person
7+
WHERE
8+
c.person_status = 'Alive';

0 commit comments

Comments
 (0)