write query where two table relation and people just relate them and child_name,father_name and mother_name for each child in table in sql

write query where two table relation and people just relate them and child_name,father_name and mother_name for each child in table in sql


CREATE TABLE relation (

    c_id INT NOT NULL,

    p_id INT NOT NULL

);

create a table named "relation" with two columns:

1. "c_id" with a data type of INT and a constraint of NOT NULL, which means the column cannot contain NULL values.

2. "p_id" with a data type of INT and a constraint of NOT NULL, which means the column cannot contain NULL values.

INSERT INTO relation (c_id, p_id) VALUES

(145, 202),

(145, 107),

(278, 305),

(278, 155),

(329, 425),

(329, 227),

(534, 586),

(534, 878),

(618, 747),

(618, 904);


Insert data into "relation" table.

CREATE TABLE people (
    id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    gender CHAR(1) NOT NULL
);

 create a table named "people" with three columns:

1. "id" with a data type of INT and a constraint of NOT NULL, which means the column cannot contain NULL values.

2. "name" with a data type of VARCHAR(255) and a constraint of NOT NULL, which means the column cannot contain NULL values.

3. "gender" with a data type of CHAR(1) and a constraint of NOT NULL, which means the column cannot contain NULL values.

INSERT INTO people (id, name, gender) VALUES

(107, 'Days', 'F'),

(145, 'Hawbaker', 'M'),

(155, 'Hansel', 'F'),

(202, 'Blackstone', 'M'),

(227, 'Criss', 'F'),

(278, 'Keffer', 'M'),

(305, 'Canty', 'M'),

(329, 'Mozingo', 'M'),

(425, 'Nolf', 'M'),

(534, 'Waugh', 'M'),

(586, 'Tong', 'M'),

(618, 'Dimartino', 'M'),

(747, 'Beane', 'M'),

(878, 'Chatmon', 'F'),

(904, 'Hansard', 'M');

Insert data into "people" table.

SELECT people1.name AS child, people2.name AS father, people3.name AS mother
FROM relation
JOIN people people1 ON relation.c_id = people1.id
JOIN people people2 ON relation.p_id = people2.id
JOIN people people3 ON relation.p_id = people3.id
ORDER BY child;

query to retrieve the names of children and their fathers and mothers from three tables: "relation", "people1", and "people2". The query performs the following operations:

1. Selects three columns from the result set: the name of the child, the name of the father, and the name of the mother.

2. Joins the "relation" table with the "people1" table on the "c_id" column of the "relation" table and the "id" column of the "people1" table to retrieve the name of the child.

3. Joins the "relation" table with the "people2" table on the "p_id" column of the "relation" table and the "id" column of the "people2" table to retrieve the name of the father.

4. Joins the "relation" table with the "people3" table on the "p_id" column of the "relation" table and the "id" column of the "people3" table to retrieve the name of the mother.

5. Orders the results by the "child" column in ascending order.

The result of the query will be a table with three columns: "child", "father", and "mother", showing the names of children and their fathers and mothers. The table will be ordered by the name of the child in ascending order.



Comments