Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Union With Mismatched Columns
00:00
5 left

Union With Mismatched Columns

MediumSQL · PostgreSQL

Problem

How would you union two tables when table A has 5 columns and table B has 8 columns?

Write a PostgreSQL query that combines the two supplied event tables while preserving all rows and returning a consistent eight-column result. Align the shared fields and represent unavailable fields from the five-column table as NULL values.

Output

  1. One row per source event, with columns event_id, customer_id, event_date, amount, status, channel, region, and notes
  2. Include every row from both tables
  3. Order by event_date ascending, then event_id ascending, with NULL dates last

Schema

event_records_a
ColumnTypeDescription
event_idPKINTUnique event identifier
customer_idINTCustomer associated with the event
event_dateDATEDate on which the event occurred
amountNUMERIC(10,2)Signed monetary amount
statusVARCHAR(20)Processing status
event_records_b
ColumnTypeDescription
event_idPKINTUnique event identifier
customer_idINTCustomer associated with the event
event_dateDATEDate on which the event occurred
amountNUMERIC(10,2)Signed monetary amount
statusVARCHAR(20)Processing status
channelVARCHAR(20)Originating interaction channel
regionVARCHAR(30)Customer or processing region
notesTEXTFree-form event notes
Tablesevent_records_aevent_records_b
Interviewer

Your question is Union With Mismatched Columns. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.