Skip to content

Instantly share code, notes, and snippets.

@kayvank
Last active March 21, 2023 23:56
Show Gist options
  • Save kayvank/ea0b10d4d6ef165498119b578e9d7338 to your computer and use it in GitHub Desktop.
Save kayvank/ea0b10d4d6ef165498119b578e9d7338 to your computer and use it in GitHub Desktop.
left sql join
-- Sample left join
-- see for more detail, https://www.tutorialspoint.com/sql/sql-left-joins.htm
-- to execute:
-- sqlite3 ./db < leftjoin.sql
--
drop table if exists U;
drop table if exists S;
create table if not exists U (address text, txid text, txix int , unique (txid, txix) );
create table if not exists S (txid text, txix int, unique (txid, txix));
insert into U (address, txid, txix) values ("a", "id-0", 0);
insert into U (address, txid, txix) values ("b", "id-0", 1);
insert into U (address, txid, txix) values ("c", "id-0", 2);
insert into U (address, txid, txix) values ("a", "id-0", 3);
insert into U (address, txid, txix) values ("a", "id-0", 4);
insert into S (txid, txix) values ("id-0", 4);
insert into S (txid, txix) values ("id-0", 5);
insert into S (txid, txix) values ("id-0", 6);
--
-- represents U - S, all elements in U that are not in S
--
select u.address, u.txid, u.txix
from U u LEFT JOIN S s
on u.txid = s.txid AND u.txix = s.txix
where
u.address = 'a' AND
S.txid is null AND S.txix is null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment