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.
contact_id, contact_name, interaction_count, and last_interaction_date.contact_id ascending.| Column | Type | Description |
|---|---|---|
| contact_idPK | INT | Unique contact identifier |
| contact_name | VARCHAR(100) | Contact display name |
| VARCHAR(150) | Contact email address |
| Column | Type | Description |
|---|---|---|
| interaction_idPK | INT | Unique interaction identifier |
| contact_id | INT | References contacts.contact_id |
| interaction_date | DATE | Date of the interaction |
| channel | VARCHAR(30) | Interaction channel |
| status | VARCHAR(30) | Interaction status |