Select one row from child table using join

SELECT pID, lastname
FROM parent
LEFT JOIN (
SELECT pID,
row_number() over (partition by pid order by cid) as rn
FROM child
) as child
ON parent.pID = child.pID and child.rn = 1

By using the above syntax you will get the row count equal to parent row count

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s