In this SQL problem-solving session, we focus on pairing adults with children such that the oldest adult is paired with the youngest child. In cases where ages are the same, the person with the highest ID is given priority. This is achieved using window functions, ranking, and FULL OUTER JOIN.
This video is perfect for SQL learners, data analysts, and professionals looking to enhance their SQL skills with real-world ranking and pairing problems.
📌 Table creation & insertion script:
CREATE TABLE person_info(
person VARCHAR(10),
type VARCHAR(10),
age INT
);
INSERT INTO person_info(person,type,age) VALUES
('A1','ADULT',54),
('A2','ADULT',53),
('A3','ADULT',52),
('A4','ADULT',58),
('A5','ADULT',54),
('C1','CHILD',20),
('C2','CHILD',19),
('C3','CHILD',22),
('C4','CHILD',15);
📌 Topics Covered:
✔ Using ROW_NUMBER() for ranking
✔ Sorting data with ORDER BY
✔ Applying FULL OUTER JOIN for matching pairs
#sql #windowfunctions #row_number #ranking #database #sqlquery #dataanalysis #joins
Download
0 formats
No download links available.
SQL Query to Pair Adults with Children Based on Age | SQL Window Functions & Ranking | NatokHD