Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.