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
Join Operations and Alternatives
00:00
5 left

Join Operations and Alternatives

MediumSQL · PostgreSQL

Problem

Explain the foreign key and demonstrate how to combine data from two tables using joins, and is there any alternate way apart from joins?

Asked in the managerial and technical interview stage. A reported follow-up asked about the difference between SQL and NoSQL databases and the scenarios where each is used.

Write a PostgreSQL query that combines contacts with their interactions and includes contacts with no interactions. Also explain the foreign-key relationship and provide an alternative query that does not use a join.

Output

  1. One row per contact with contact_id, contact_name, interaction_count, and last_interaction_date.
  2. Include contacts with zero interactions, ordered by contact_id ascending.

Schema

contacts
ColumnTypeDescription
contact_idPKINTUnique contact identifier
contact_nameVARCHAR(100)Contact display name
emailVARCHAR(150)Contact email address
interactions
ColumnTypeDescription
interaction_idPKINTUnique interaction identifier
contact_idINTReferences contacts.contact_id
interaction_dateDATEDate of the interaction
channelVARCHAR(30)Interaction channel
statusVARCHAR(30)Interaction status
Tablescontactsinteractions
Interviewer

Your question is Join Operations and Alternatives. 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.