Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Last active February 26, 2021 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pesterhazy/6d5011270f07177a5d922bc4f5252e54 to your computer and use it in GitHub Desktop.
Save pesterhazy/6d5011270f07177a5d922bc4f5252e54 to your computer and use it in GitHub Desktop.
You shouldn't be using USING
db=> create table a (x int);
CREATE TABLE
db=> create table b (x int, y int);
CREATE TABLE
db=> create table c (y int, z int);
CREATE TABLE

db=> select * from a join b using (x) join c using (y);
 y | x | z
---+---+---
(0 rows)

-- query works fine

-- now let's introduce a column seemingly unrelated to query
db=> alter table a add column y int;
ALTER TABLE
db=> select * from a join b using (x) join c using (y);
ERROR:  common column name "y" appears more than once in left table

-- ouch

db=> select * from a join b on a.x=b.x join c on b.y=c.y;
 x | y | x | y | y | z
---+---+---+---+---+---
(0 rows)

-- using ON explicitly avoids the issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment