Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 295049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:25:22+00:00 2026-05-12T06:25:22+00:00

Given a database schema with a parent table and two or more child tables.

  • 0

Given a database schema with a parent table and two or more child tables. For example:

Database Diagram

Is it possible to create a query, using the for xml statement, that outputs the following XML:

<?xml version="1.0"?>
<person>
    <name>Joe Bloggs</name>
    <age>25</age>
    <address>
        <streetAddress>123 Test Street</streetAddress>
        <town>Example Town</town>
        <postcode>A12 3BC</postcode>
    </address>
    <address>
        <streetAddress>124 Test Street</streetAddress>
        <town>Example Town</town>
        <postcode>A12 3BC</postcode>
    </address>
    <contact>
        <type>Home Phone</type>
        <value>01234 567890</value>
    </contact>
    <contact>
        <type>Work Phone</type>
        <value>01987 654321</value>
    </contact>
    <contact>
        <type>Email Address</type>
        <value>joe@example.com</value>
    </contact>
</person>

My initial attempt:

select person.name, person.age,
        address.streetAddress, address.town, address.postcode,
        contact.type, contact.value
    from Person as person
        left join Address as address on person.PersonID = address.PersonID
        left join Contact as contact on person.PersonID = contact.PersonID
    where person.PersonID = 1
    for xml auto, elements

Yielded this result in which all the combinations of contact and address are output:

<?xml version="1.0" encoding="utf-8"?>
<person>
    <name>Joe Bloggs</name>
    <age>25</age>
    <address>
        <streetAddress>123 Test Street</streetAddress>
        <town>Example Town</town>
        <postcode>A12 3BC</postcode>
        <contact>
            <type>Home Phone</type>
            <value>01234 567890</value>
        </contact>
        <contact>
            <type>Work Phone</type>
            <value>01987 654321</value>
        </contact>
        <contact>
            <type>Email Address</type>
            <value>joe@example.com</value>
        </contact>
    </address>
    <address>
        <streetAddress>124 Test Street</streetAddress>
        <town>Example Town</town>
        <postcode>A32 1BC</postcode>
        <contact>
            <type>Home Phone</type>
            <value>01234 567890</value>
        </contact>
        <contact>
            <type>Work Phone</type>
            <value>01987 654321</value>
        </contact>
        <contact>
            <type>Email Address</type>
            <value>joe@example.com</value>
        </contact>
    </address>
</person>

Using a single left join to either the Contact or Address table produces part of what I’m after, but after adding the second join it starts to go wrong. Is there another technique that could be used to achieve the result I’m looking for?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:25:23+00:00Added an answer on May 12, 2026 at 6:25 am
    SELECT  person.name, person.age,
            (
            SELECT  address.streetAddress, address.town, address.postcode
            FROM    Address as address
            WHERE   person.PersonID = address.PersonID
            FOR XML PATH('ADDRESS'), TYPE
            ),
            (
            SELECT  contact.type, contact.value
            FROM    Contact as contact
            WHERE   person.PersonID = contact.PersonID
            FOR XML PATH('CONTACT'), TYPE
            ),
    FROM    Person as person
    WHERE   person.PersonID = 1
    FOR XML AUTO, ELEMENTS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 172k
  • Answers 172k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's pretty complicated, so if you're not sure what you're… May 12, 2026 at 2:25 pm
  • Editorial Team
    Editorial Team added an answer Does your concrete implementation of IDatabaseScope have any constructor dependencies?… May 12, 2026 at 2:25 pm
  • Editorial Team
    Editorial Team added an answer This still sounds like a path issue. If you have… May 12, 2026 at 2:25 pm

Related Questions

I've got a database in production for nearly 3 years, on Sql 2008 (was
I am writing a report to return details about an object ('files') in my
I have a large set of NUnit tests; I need to import the results
My employer, a small office supply company, is switching suppliers and I am looking

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.