#C295. Performing an Inner Join on Two Tables

    ID: 46322 Type: Default 1000ms 256MiB

Performing an Inner Join on Two Tables

Performing an Inner Join on Two Tables

You are given two tables represented as JSON arrays of objects (dictionaries) along with two column names. The task is to perform an inner join on the two tables. In other words, for every row in the first table (table1), find all rows in the second table (table2) such that the value in table1 for the column join_col1 equals the value in table2 for the column join_col2 (i.e. (table1[join_col1] = table2[join_col2])). For each matching pair, merge the two rows (the keys from both objects) into a single JSON object. If no matches are found, return an empty JSON array.

The input consists of four lines from standard input. The first line is a JSON array representing table1, the second line is a string representing the join column in table1, the third line is a JSON array representing table2, and the fourth line is a string representing the join column in table2. The output is the resulting JSON array printed to standard output.

inputFormat

The input is provided via standard input (stdin) and consists of four lines:

  1. A JSON array representing table1. Each element is a JSON object with key-value pairs.
  2. A string representing the join column name in table1 (join_col1).
  3. A JSON array representing table2. Each element is a JSON object with key-value pairs.
  4. A string representing the join column name in table2 (join_col2).

For example:

[{"id":1,"name":"Alice"}, {"id":2,"name":"Bob"}] "id" [{"user_id":1,"age":25}, {"user_id":2,"age":30}] "user_id"

outputFormat

The output should be printed to standard output (stdout) and is a JSON array representing the result of the inner join. Each element in the output array is a merged JSON object that combines the keys from both rows that match on the specified join columns. If no matching rows exist, output an empty JSON array.## sample

[{"id":1,"name":"Alice"}, {"id":2,"name":"Bob"}, {"id":3,"name":"Charlie"}]
"id"
[{"user_id":1,"age":25}, {"user_id":2,"age":30}, {"user_id":4,"age":40}]
"user_id"
[{"id":1,"name":"Alice","user_id":1,"age":25},{"id":2,"name":"Bob","user_id":2,"age":30}]