How to write a single SQL query that lists master and detail records in hierarchy form

Posted on 2009-04-30
Last Modified: 2012-05-06

I am trying to create a query that will generate a master/detail report in MS SQL 2005.  Here are my two tables:

EmployeeID int PK
Firstname varchar
Lastname varchar
Birthday smalldatetime

DependentID int PK
EmployeeID int  (links to Employees.EmployeeID)
Firstname varchar
lastname varchar

Let's assume that the following records are in there:

EmployeeID / Firstname / lastname /birthday
1                 / John         / Doe         / 1/1/1980
2                 / Porky        / Pig          / 1/1/1982

DependentID / EmployeeID / Firstname / lastname /birthday
1                   / 1                  / Jane         / Doe        / 2/2/1980
2                   / 1                  / Baby        / Doe        / 2/2/2009
3                   / 2                  / Babe        / Pig          / 2/2/1990

What kind of query could I use that would generate these results?

EmployeeID / Dependent / Firstname / lastname / birthday
1                 /  False         / John         / Doe          / 1/1/1980
1                 /  True          / Jane         / Doe          / 2/2/1980
1                 /  True          / Baby        / Doe          / 2/2/2009
2                 /  False         / Porky       / Pig            / 1/1/1982

I would appreciate any guidance you can give me.



Question by:pmodiano1
    LVL 25

    Accepted Solution

    Something like this should do -
    SELECT *
    	(SELECT EmployeeId, 'False' as Dependent, FirstName, LastName, BirthDay
    	 FROM Employee
    	 SELECT EmployeeId, 'True' as Dependent, FirstName, LastName, Birthday
    	 FROM Dependents
    	) AS Tmp
    ORDER BY EmployeeId, Dependent

    Open in new window


    Author Closing Comment

    Well done! Thanks so much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
    APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now