[{"data":1,"prerenderedAt":506},["ShallowReactive",2],{"post-\u002Fpost\u002F2021-04-23-sql-join-shorthand":3,"around-\u002Fpost\u002F2021-04-23-sql-join-shorthand":500},{"id":4,"title":5,"body":6,"date":489,"description":490,"extension":491,"meta":492,"navigation":493,"path":494,"seo":495,"stem":496,"subtitle":497,"tags":498,"__hash__":499},"posts\u002Fpost\u002F2021-04-23-sql-join-shorthand.md","Combining SQL outer joins with inner joins",{"type":7,"value":8,"toc":487},"minimark",[9,33,44,135,140,232,239,252,260,320,326,337,394,401,404,469,472,480,483],[10,11,12,13,17,18,21,22,25,26,21,29,32],"p",{},"When You have 3 tables in an SQL query, and You need all records from ",[14,15,16],"code",{},"table1 t1",",\nbut records from ",[14,19,20],{},"table2 t2"," and ",[14,23,24],{},"table3 t3"," only if there is a record on both tables ",[14,27,28],{},"t2",[14,30,31],{},"t3",", You have some options:",[34,35,36],"ul",{},[37,38,39,40,43],"li",{},"Write all outer joins with a ",[14,41,42],{},"WHERE"," clauses",[45,46,51],"pre",{"className":47,"code":48,"language":49,"meta":50,"style":50},"language-sql shiki shiki-themes min-light min-dark","select t1.field1, t2.field2, t3.field3\nfrom \n  table1 t1 \n  left outer join table2 t2 on t1.field1 = t2.field1\n  left outer join table3 t3 on t2.field2 = t3.field2\nwhere\n  t3.field3 is not null \n","sql","",[14,52,53,66,75,81,102,120,126],{"__ignoreMap":50},[54,55,58,62],"span",{"class":56,"line":57},"line",1,[54,59,61],{"class":60},"se1w0","select",[54,63,65],{"class":64},"sHHeI"," t1.field1, t2.field2, t3.field3\n",[54,67,69,72],{"class":56,"line":68},2,[54,70,71],{"class":60},"from",[54,73,74],{"class":64}," \n",[54,76,78],{"class":56,"line":77},3,[54,79,80],{"class":64},"  table1 t1 \n",[54,82,84,87,90,93,96,99],{"class":56,"line":83},4,[54,85,86],{"class":60},"  left outer join",[54,88,89],{"class":64}," table2 t2 ",[54,91,92],{"class":60},"on",[54,94,95],{"class":64}," t1.field1 ",[54,97,98],{"class":60},"=",[54,100,101],{"class":64}," t2.field1\n",[54,103,105,107,110,112,115,117],{"class":56,"line":104},5,[54,106,86],{"class":60},[54,108,109],{"class":64}," table3 t3 ",[54,111,92],{"class":60},[54,113,114],{"class":64}," t2.field2 ",[54,116,98],{"class":60},[54,118,119],{"class":64}," t3.field2\n",[54,121,123],{"class":56,"line":122},6,[54,124,125],{"class":60},"where\n",[54,127,129,132],{"class":56,"line":128},7,[54,130,131],{"class":64},"  t3.field3 ",[54,133,134],{"class":60},"is not null\n",[34,136,137],{},[37,138,139],{},"Write sub-query",[45,141,143],{"className":47,"code":142,"language":49,"meta":50,"style":50},"select t1.field1, t_.field2, t_.field3\nfrom\n  table1 t1\n  left outer join (\n    select t2.field1, t2.field2, t3.field3\n    from\n      table2 t2\n      join table3 t3 on t2.field2 = t3.field2\n    where\n      t3.field3 is not null\n  ) t_ on t1.field1 = t_.field1\n",[14,144,145,152,157,162,169,177,182,187,203,209,217],{"__ignoreMap":50},[54,146,147,149],{"class":56,"line":57},[54,148,61],{"class":60},[54,150,151],{"class":64}," t1.field1, t_.field2, t_.field3\n",[54,153,154],{"class":56,"line":68},[54,155,156],{"class":60},"from\n",[54,158,159],{"class":56,"line":77},[54,160,161],{"class":64},"  table1 t1\n",[54,163,164,166],{"class":56,"line":83},[54,165,86],{"class":60},[54,167,168],{"class":64}," (\n",[54,170,171,174],{"class":56,"line":104},[54,172,173],{"class":60},"    select",[54,175,176],{"class":64}," t2.field1, t2.field2, t3.field3\n",[54,178,179],{"class":56,"line":122},[54,180,181],{"class":60},"    from\n",[54,183,184],{"class":56,"line":128},[54,185,186],{"class":64},"      table2 t2\n",[54,188,190,193,195,197,199,201],{"class":56,"line":189},8,[54,191,192],{"class":60},"      join",[54,194,109],{"class":64},[54,196,92],{"class":60},[54,198,114],{"class":64},[54,200,98],{"class":60},[54,202,119],{"class":64},[54,204,206],{"class":56,"line":205},9,[54,207,208],{"class":60},"    where\n",[54,210,212,215],{"class":56,"line":211},10,[54,213,214],{"class":64},"      t3.field3 ",[54,216,134],{"class":60},[54,218,220,223,225,227,229],{"class":56,"line":219},11,[54,221,222],{"class":64},"  ) t_ ",[54,224,92],{"class":60},[54,226,95],{"class":64},[54,228,98],{"class":60},[54,230,231],{"class":64}," t_.field1\n",[10,233,234,235,238],{},"In my case, I had to have exactly 1 record returned, that was filtered on ",[14,236,237],{},"t1",".",[10,240,241,242,244,245,248,249,251],{},"So outer joins with a specific ",[14,243,42],{}," clause with ",[14,246,247],{},"NOT NULL"," expressions would not work, as it could return no rows, if no rows in ",[14,250,31],{}," matched my requirements.",[10,253,254,255,257,258],{},"I tried using outer join for ",[14,256,28],{}," but inner join for ",[14,259,31],{},[45,261,263],{"className":47,"code":262,"language":49,"meta":50,"style":50},"select t1.field1, t2.field2, t3.field3\nfrom \n  table1 t1 \n  left outer join table2 t2 on t1.field1 = t2.field1\n  join table3 t3 on t2.field2 = t3.field2\nwhere\n  t3.field3 is not null \n",[14,264,265,271,277,281,295,310,314],{"__ignoreMap":50},[54,266,267,269],{"class":56,"line":57},[54,268,61],{"class":60},[54,270,65],{"class":64},[54,272,273,275],{"class":56,"line":68},[54,274,71],{"class":60},[54,276,74],{"class":64},[54,278,279],{"class":56,"line":77},[54,280,80],{"class":64},[54,282,283,285,287,289,291,293],{"class":56,"line":83},[54,284,86],{"class":60},[54,286,89],{"class":64},[54,288,92],{"class":60},[54,290,95],{"class":64},[54,292,98],{"class":60},[54,294,101],{"class":64},[54,296,297,300,302,304,306,308],{"class":56,"line":104},[54,298,299],{"class":60},"  join",[54,301,109],{"class":64},[54,303,92],{"class":60},[54,305,114],{"class":64},[54,307,98],{"class":60},[54,309,119],{"class":64},[54,311,312],{"class":56,"line":122},[54,313,125],{"class":60},[54,315,316,318],{"class":56,"line":128},[54,317,131],{"class":64},[54,319,134],{"class":60},[10,321,322,323,325],{},"But this would still result in no rows if there were no matching rows in ",[14,324,31],{}," (silly me)",[10,327,328,329,336],{},"While searching, I stumbled upon a thread on ",[330,331,335],"a",{"href":332,"rel":333},"https:\u002F\u002Fstackoverflow.com\u002Fa\u002F9714564",[334],"nofollow","StackOverflow",", with a similar question, and I noticed a strange syntax for the joins:",[45,338,340],{"className":47,"code":339,"language":49,"meta":50,"style":50},"select t1.field1, t2.field2, t3.field3\nfrom \n  table1 t1 \n  left outer join (\n    table2 t2 join table3 t3 on t2.field2 = t3.field2\n  ) t_ on t1.field1 = t_.field1\n",[14,341,342,348,354,358,364,382],{"__ignoreMap":50},[54,343,344,346],{"class":56,"line":57},[54,345,61],{"class":60},[54,347,65],{"class":64},[54,349,350,352],{"class":56,"line":68},[54,351,71],{"class":60},[54,353,74],{"class":64},[54,355,356],{"class":56,"line":77},[54,357,80],{"class":64},[54,359,360,362],{"class":56,"line":83},[54,361,86],{"class":60},[54,363,168],{"class":64},[54,365,366,369,372,374,376,378,380],{"class":56,"line":104},[54,367,368],{"class":64},"    table2 t2 ",[54,370,371],{"class":60},"join",[54,373,109],{"class":64},[54,375,92],{"class":60},[54,377,114],{"class":64},[54,379,98],{"class":60},[54,381,119],{"class":64},[54,383,384,386,388,390,392],{"class":56,"line":122},[54,385,222],{"class":64},[54,387,92],{"class":60},[54,389,95],{"class":64},[54,391,98],{"class":60},[54,393,231],{"class":64},[10,395,396,397,400],{},"As it turns out, this is a shorthand syntax for a sub-query - it just omits the ",[14,398,399],{},"SELECT * FROM"," part.",[10,402,403],{},"If we test the same query, rewritten to use a sub-query",[45,405,407],{"className":47,"code":406,"language":49,"meta":50,"style":50},"select t1.field1, t2.field2, t3.field3\nfrom \n  table1 t1 \n  left outer join (\n    select *\n    from table2 t2 join table3 t3 on t2.field2 = t3.field2\n  ) t_ on t1.field1 = t_.field1\n",[14,408,409,415,421,425,431,438,457],{"__ignoreMap":50},[54,410,411,413],{"class":56,"line":57},[54,412,61],{"class":60},[54,414,65],{"class":64},[54,416,417,419],{"class":56,"line":68},[54,418,71],{"class":60},[54,420,74],{"class":64},[54,422,423],{"class":56,"line":77},[54,424,80],{"class":64},[54,426,427,429],{"class":56,"line":83},[54,428,86],{"class":60},[54,430,168],{"class":64},[54,432,433,435],{"class":56,"line":104},[54,434,173],{"class":60},[54,436,437],{"class":60}," *\n",[54,439,440,443,445,447,449,451,453,455],{"class":56,"line":122},[54,441,442],{"class":60},"    from",[54,444,89],{"class":64},[54,446,371],{"class":60},[54,448,109],{"class":64},[54,450,92],{"class":60},[54,452,114],{"class":64},[54,454,98],{"class":60},[54,456,119],{"class":64},[54,458,459,461,463,465,467],{"class":56,"line":128},[54,460,222],{"class":64},[54,462,92],{"class":60},[54,464,95],{"class":64},[54,466,98],{"class":60},[54,468,231],{"class":64},[10,470,471],{},"We get the same result.",[10,473,474,475,238],{},"When looking for a more detailed write up on this, I could not find any. All I could find is a mention of this in\nPostgreSQL (I use mostly PostgreSQL for all my projects) docs, when talking about ",[330,476,479],{"href":477,"rel":478},"https:\u002F\u002Fwww.postgresql.org\u002Fdocs\u002Fcurrent\u002Fqueries-table-expressions.html#QUERIES-TABLE-ALIASES",[334],"\"Table and Column Aliases\"",[10,481,482],{},"If You know where I can read more about this construct, please, let Me know.",[484,485,486],"style",{},"html pre.shiki code .se1w0, html code.shiki .se1w0{--shiki-default:#D32F2F;--shiki-dark:#F97583}html pre.shiki code .sHHeI, html code.shiki .sHHeI{--shiki-default:#24292EFF;--shiki-dark:#B392F0}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":50,"searchDepth":68,"depth":68,"links":488},[],"2021-04-23","When You have 3 tables in an SQL query, and You need all records from table1 t1,\nbut records from table2 t2 and table3 t3 only if there is a record on both tables t2 and t3, You have some options:","md",{},true,"\u002Fpost\u002F2021-04-23-sql-join-shorthand",{"title":5,"description":490},"post\u002F2021-04-23-sql-join-shorthand","For the occasions where You really don't want to write explicit sub-queries",[49],"jwsU4W6cLb3T1scgyKz4H8xTKDG-DnlqOFjuSyKCTXA",[501,502],null,{"title":503,"path":504,"stem":505,"children":-1},"Preparing map for orienteering events","\u002Fpost\u002F2021-04-22-first-post","post\u002F2021-04-22-first-post",1795442563854]