Skip to content
CampusEduX

Classify incoming identities before a proposed synchronization

Easysql
Learn the Concept: MERGE Concept

For each incoming itemid, report action=update when targetitems already has that identity and action=insert otherwise. Do not write any rows. Target rows absent from the incoming batch are outside this report, not candidates for deletion.

MySQL dialect.

sql
CREATE TABLE target_items (item_id INT PRIMARY KEY,label VARCHAR(40) NOT NULL); CREATE TABLE incoming (item_id INT PRIMARY KEY,label VARCHAR(40) NOT NULL); INSERT INTO target_items VALUES (1,'Old'),(2,'Keep'); INSERT INTO incoming VALUES (1,'New'),(3,'Add');

Required ordering: Ascending incoming item_id.

Edge cases:

  • An empty target classifies every supplied input as insert.
  • Empty incoming input produces no action row.

Related Problems

  • Apply a narrow MySQL insert-or-replace-quantity policy
    Medium · sql
    Solve Problem
  • Select one student occurrence by identity
    Easy · sql
    Solve Problem
  • Create a disposable database with explicit text defaults
    Easy · sql
    Solve Problem

Input (stdin)

Output

Run your code to see output here...