DIFF TABLE
Description
The DIFF TABLE statement compares two structurally identical tables and returns a result set of data differences. Use it to review edits in a copy, validate expected versus actual data, or preview differences before a merge.
Privileges
Executing DIFF TABLE requires read access to both incoming_table and current_table.
Syntax
DIFF TABLE incoming_db.incoming_table AGAINST current_db.current_table;
Parameters
| Parameter | Description |
|---|---|
incoming_table | The source table to compare. Supports db.table or table. |
current_table | The current or baseline table. Supports db.table or table. |
Behavior
DIFF TABLEis read-only; it does not modify either table.- The system aligns rows by primary key and compares row by row.
- The result set contains three kinds of rows:
- Conflict rows: Same primary key but different non–primary key values—the key exists in both tables but non–primary key columns differ. The result includes two diff rows, one for
current_tableand one forincoming_table. - Exclusive rows: The primary key exists in only one table. The result includes that single row from that table.
- Equal rows: Same primary key and all compared column values are equal under null-safe equality. Those rows do not appear in the result.
- Conflict rows: Same primary key but different non–primary key values—the key exists in both tables but non–primary key columns differ. The result includes two diff rows, one for
- Results are ordered by primary key; for a given key, the
current_tablerow comes first, then theincoming_tablerow. - When a column value is
NULL, comparison uses null-safe equality:NULLequalsNULL.
Limitations and considerations
incoming_tableandcurrent_tablemust have identical column definitions, including names, types, and order.- Both tables must have a primary key.
- Tables that include vector, RoaringBitmap, or UDT columns are not supported.
DIFF TABLEonly returns differences; it does not insert, update, or delete data.- The current implementation fully scans both tables. Use caution on large data sets.
DIFF TABLEis a seekdb extension and is not compatible with standard MySQL or Oracle SQL syntax.
Examples
The examples below create a baseline table, load data, fork a copy with FORK TABLE, modify the copy, then compare against the baseline.
Example 1: Compare two tables with conflicts and new rows
DROP TABLE IF EXISTS demo_current;
DROP TABLE IF EXISTS demo_incoming;
CREATE TABLE demo_current (
id INT PRIMARY KEY,
name VARCHAR(20),
score INT
);
INSERT INTO demo_current VALUES
(1, 'Alice', 80),
(2, 'Bob', 90),
(3, 'Charlie', 70);
-- Fork a copy from the baseline and make it differ from the baseline
FORK TABLE demo_current TO demo_incoming;
UPDATE demo_incoming SET score = 85 WHERE id = 1;
DELETE FROM demo_incoming WHERE id = 2;
INSERT INTO demo_incoming VALUES (4, 'David', 95);
DIFF TABLE demo_incoming AGAINST demo_current;
In this example:
id = 1exists in both tables with differentscorevalues.id = 2exists only indemo_current.id = 4exists only indemo_incoming.id = 3is identical in both tables, so it does not appear in the result.
Expected output for DIFF TABLE demo_incoming AGAINST demo_current;:
__table __flag id name score
demo_current INSERT 1 Alice 80
demo_incoming INSERT 1 Alice 85
demo_current INSERT 2 Bob 90
demo_incoming INSERT 4 David 95
Example 2: Compare two fully identical tables
DROP TABLE IF EXISTS demo_a;
DROP TABLE IF EXISTS demo_b;
CREATE TABLE demo_a (
id INT PRIMARY KEY,
name VARCHAR(20)
);
INSERT INTO demo_a VALUES (1, 'Alice');
FORK TABLE demo_a TO demo_b;
DIFF TABLE demo_b AGAINST demo_a;
The expected result is an empty result set.
Example 3: Compare data that includes NULL
DROP TABLE IF EXISTS demo_left;
DROP TABLE IF EXISTS demo_right;
CREATE TABLE demo_left (
id INT PRIMARY KEY,
note VARCHAR(20)
);
INSERT INTO demo_left VALUES
(1, NULL),
(2, 'A');
FORK TABLE demo_left TO demo_right;
UPDATE demo_right SET note = 'B' WHERE id = 2;
DIFF TABLE demo_right AGAINST demo_left;
Here, note is NULL for id = 1 in both tables, so that is not treated as a difference; id = 2 is a conflict.
Expected output for DIFF TABLE demo_right AGAINST demo_left;:
__table __flag id note
demo_left INSERT 2 A
demo_right INSERT 2 B
FAQ
Q1: Does DIFF TABLE modify table data?
No. DIFF TABLE only returns a diff result set.
Q2: Why are some rows missing from the result?
Because those rows are identical in both tables. DIFF TABLE only returns differences.
Q3: Do both tables need a primary key?
Yes. DIFF TABLE relies on primary keys to align rows between the two tables.
Q4: Can I compare tables with different structures?
No. Column names, types, and order must match exactly.