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

  • Home
  • SEARCH
  • 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 9126217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:59:16+00:00 2026-06-17T06:59:16+00:00

I need to create a query to combine the data from two tables, I

  • 0

I need to create a query to combine the data from two tables, I think possibly a combination of JOIN and UNION.

In this example, I need to list all names where the status is active, just once, with their wine, soda, dinner, dessert and fruit preferences combined, ordered by name.

I’m not sure if JOIN alone will work, since a name isn’t always in both tables, and UNION is tricky since they don’t have the same columns. I’ve tried using null values to make a UNION work, but that’s been giving me duplicate rows for each name instead of grouping them into one row per name.

DRINK
====================================
name         status   wine    soda
----------   ------   ------  ------
John Smith   active   red     cola
Mary Jones   active   white   lemonade
Tom Brown    old      red     fanta
Judy White   active   red     dr pepper
Sam Wing     old      red     cola

FOOD
=============================================
name         status   dinner  dessert  fruit
----------   ------   ------  ------   ------
John Smith   active   steak   muffin   apple
Mary Jones   active   fish    cake     kiwi
Walter Yu    active   pasta   cake     banana
Jim Adams    old      steak   candy    apple
Adam Sheers  active   pasta   candy    grapes

I need the query to generate a result like this –

RESULT
==================================================================  
name         status    wine     soda       dinner  dessert  fruit
----------   ------    ------   ------     ------  ------   ------
Adam Sheers  active    -        -          pasta   candy    grapes
John Smith   active    red      cola       steak   muffin   apple
Judy White   active    red      dr pepper  -       -        -
Mary Jones   active    white    lemonade   fish    cake     kiwi
Walter Yu    active    -        -          pasta   cake     banana

A huge thanks for any help with this!

  • 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-06-17T06:59:17+00:00Added an answer on June 17, 2026 at 6:59 am

    This should work:

    SELECT names.name, drink.wine, drink.soda, food.dinner, food.dessert, food.fruit
    FROM
        (SELECT name FROM food WHERE status = 'active'
        UNION
        SELECT name FROM drink WHERE status = 'active') names
    LEFT JOIN drink ON drink.name = names.name
    LEFT JOIN food ON food.name = names.name
    

    Result

    |        NAME |   WINE |      SODA | DINNER | DESSERT |  FRUIT |
    ----------------------------------------------------------------
    |  John Smith |    red |      cola |  steak |  muffin |  apple |
    |  Mary Jones |  white |  lemonade |   fish |    cake |   kiwi |
    |   Walter Yu | (null) |    (null) |  pasta |    cake | banana |
    | Adam Sheers | (null) |    (null) |  pasta |   candy | grapes |
    |  Judy White |    red | dr pepper | (null) |  (null) | (null) |

    See it in action

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a join query for hierarchical data across 2 tables. These
Trying to sort this out... I need a query that will combine both tables
I need to pull values in similar tables from two different databases, combine them
i need to change my query from native-query to ( named-query or create-query )
I need to create a query using the join on various parameters like: SELECT
I need to create a view, however the data is generated from an application
I need to create a query which groups by two columns and returns an
I need to create a query to sum day's hours and night's hours from
I just can't seem to get this query figured out. I need to combine
I need to create query like (i'm using PostgreSQL): (SELECT * FROM t1 WHERE

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.