Comparing Two Tables in Sqlite

I have two tables and want to compare rows on sqlite like this

table1           table2
field1           field1

a                   a
b                   d
c                   f
d                   g
e
f
g
h
i

and I want to produce result like this

result_table
field1

b
c
e
h
i

How is the syntax in sqlite? Thanks

1

2 Answers

SELECT DISTINCT Field1
FROM Table1 
WHERE Field1 Not IN 
    (SELECT DISTINCT Field1 FROM Table2)
1
SELECT columns1 FROM table1 EXCEPT SELECT columns2 FROM table2;

The SQLite EXCEPT clause returns all rows from the left SELECT statement that are not in the result of the second SELECT statement. The number of columns selected must be the same in both SELECT statements.

This works fine for small to medium size tables. Avoid for tables with millions of lines.

See Compound Select Statements and the documentation of the SQLite SELECT statement.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest