Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Associated Entities

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL for Associated Entities. Start with the requirements and the two tables on the right.

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.

Problem

Citadel's data catalog maintains relationships between datasets, strategies, and other tracked entities. Write a PostgreSQL query that returns the active entities associated with the requested entity IDs 101 and 104.

Requirements

  1. Consider only associations originating from entity IDs 101 and 104.
  2. Exclude inactive associations.
  3. Return each associated entity once, including its name, distinct association types, number of distinct requesting entities connected to it, and total active association rows.
  4. Treat a missing association type as Unclassified, and order results by association count descending, then associated entity ID ascending.

Schema

entities
ColumnTypeDescription
entity_idPKINTEGERUnique catalog entity identifier
entity_nameVARCHAR(100)Display name of the entity
entity_typeVARCHAR(30)Classification such as strategy, dataset, or document
entity_associations
ColumnTypeDescription
association_idPKINTEGERUnique association identifier
entity_idINTEGERSource entity identifier
associated_entity_idINTEGERRelated entity identifier
association_typeVARCHAR(40)Relationship classification
is_activeBOOLEANIndicates whether the relationship is current
Tablesentitiesentity_associations
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results