Move the filter, change the rows

HTML for this example

Select the code and use your copy shortcut. Changes in this preview are temporary.

<!doctype html>
<html lang="en-US"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Move the filter, change the rows</title><style>
:root{color-scheme:light;--accent:#3d5977;--wash:#e6eff7;--ink:#202d3b;--paper:#fffdf9}*{box-sizing:border-box}body{margin:0;color:var(--ink);background:#eeeae2;font:17px/1.65 system-ui,-apple-system,"Segoe UI",sans-serif}main{max-width:980px;margin:auto;padding:28px 24px 40px}header{border-top:6px solid var(--accent);padding:20px 0 24px}.kicker{color:var(--accent);font-weight:750;letter-spacing:.06em;margin:0 0 8px}h1{font:700 clamp(30px,5vw,46px)/1.15 Georgia,serif;margin:0 0 14px;max-width:780px}h2{font-size:23px;line-height:1.3;margin:0 0 14px}h3{font-size:19px;margin:12px 0 8px}p{margin:0 0 16px}button,input,textarea,select{font:inherit}button{background:white;border:1px solid #87949b;color:var(--ink);border-radius:8px;padding:11px 16px;min-height:48px;cursor:pointer;font-weight:650}button:hover{background:var(--wash)}button[aria-pressed=true],button.primary{background:var(--accent);color:white;border-color:var(--accent)}:focus-visible{outline:3px solid #b45a1e;outline-offset:3px}label{display:block;font-weight:650;margin:14px 0 6px}input:not([type=checkbox]):not([type=radio]),textarea,select{display:block;width:100%;min-width:0;max-width:100%;border:1px solid #80909b;border-radius:7px;padding:11px;background:white;color:var(--ink)}input[type=checkbox],input[type=radio]{width:21px;height:21px;accent-color:var(--accent)}textarea{resize:vertical}.panel{background:var(--paper);border:1px solid #ccd2d1;border-radius:12px;padding:24px;margin-bottom:20px}.two{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px}.two>.panel{margin:0}.two{margin-bottom:20px}.actions{display:flex;flex-wrap:wrap;gap:10px;margin:18px 0}.result{padding:18px;background:var(--wash);border-left:5px solid var(--accent);margin-top:18px;white-space:pre-line;overflow-wrap:anywhere}.note{color:#465565;font-size:16px}.strip{padding:18px;background:var(--wash);border-radius:8px;margin-bottom:18px}table{border-collapse:collapse;width:100%;font-size:16px;table-layout:fixed}th,td{text-align:left;padding:10px 8px;border-bottom:1px solid #cbd0d1;overflow-wrap:anywhere;vertical-align:top}th{background:var(--wash)}ol,ul{padding-left:24px}li+li{margin-top:8px}pre{font-size:16px;white-space:pre-wrap;overflow-wrap:anywhere}code{font-size:16px}.grid{display:grid;gap:5px;max-width:440px}.grid button{padding:5px;min-width:0}.grid input{padding:5px!important;text-align:center;font-weight:750}blockquote{font:500 26px/1.5 Georgia,serif;margin:16px 0}footer{font-size:16px;color:#465565;margin-top:22px}[hidden]{display:none!important}svg{display:block;max-width:100%;height:auto}mark{background:#e0efad;color:var(--ink)}@media(max-width:560px){main{padding:18px 12px 28px}.panel{padding:20px 16px}.two{grid-template-columns:minmax(0,1fr)}h1{font-size:32px}.actions button{flex:1 1 135px}th,td{padding:8px 5px}.grid button{min-height:40px}blockquote{font-size:24px}}
</style></head><body><main><header><p class="kicker">SQL SEMANTICS · TWO REVIEWED STATEMENTS</p><h1>Move the filter, change the rows</h1><p>Three students, three tasks, two reviewed queries. Logical model only: no database connection, optimizer plan, or arbitrary SQL parser.</p></header><div class="actions"><button type="button" id="a">A · Status in ON</button><button type="button" id="b">B · Status in WHERE</button><button type="button" id="inspect">Inspect supported query</button><button type="button" id="reset">Reset A</button></div><section class="panel"><h2>Query and source</h2><label for="sql">Reviewed SQL</label><textarea id="sql" rows="4">SELECT s.id AS student_id, t.id AS task_id FROM students AS s LEFT JOIN tasks AS t ON t.student_id = s.id AND t.status = 'done' ORDER BY s.id, t.id;</textarea><p>Students:1,2,3. Tasks:(11,1,done),(12,1,todo),(13,2,todo).</p></section><div class="two"><section class="panel"><h2>Logical clause map</h2><div id="map" class="result" role="status" aria-live="polite"></div></section><section class="panel"><h2>Computed expected rows</h2><div id="count" class="result" role="status" aria-live="polite"></div><div id="rows"></div></section></div><div id="status" class="result" role="status" aria-live="polite"></div><footer>These two logical models explain ON versus WHERE filtering; test your own SQL in its intended database.</footer><noscript>JavaScript is required for the local controls. The accompanying article contains worked examples.</noscript></main><script>
(()=>{'use strict';const $=id=>document.getElementById(id);
const variants=["SELECT s.id AS student_id, t.id AS task_id FROM students AS s LEFT JOIN tasks AS t ON t.student_id = s.id AND t.status = 'done' ORDER BY s.id, t.id;","SELECT s.id AS student_id, t.id AS task_id FROM students AS s LEFT JOIN tasks AS t ON t.student_id = s.id WHERE t.status = 'done' ORDER BY s.id, t.id;"],students=[1,2,3],tasks=[{id:11,student:1,status:'done'},{id:12,student:1,status:'todo'},{id:13,student:2,status:'todo'}];const norm=s=>s.trim().replace(/\s+/g,' ');function clear(){$('map').textContent='';$('rows').replaceChildren();$('count').textContent='';$('status').textContent='Query changed. Inspect to map a supported statement.';}function inspect(){const i=variants.findIndex(v=>norm(v)===norm($('sql').value));clear();if(i<0){$('status').textContent='Unsupported statement. Only the two reviewed variants are modeled; no accepted diagram/result.';return;}const rows=[];for(const id of students){const matched=tasks.filter(t=>t.student===id&&(i===0?t.status==='done':true));const joined=matched.length?matched:[null];for(const task of joined)if(i===0||task?.status==='done')rows.push([id,task?.id??null]);}$('map').textContent='students AS s → LEFT JOIN tasks AS t / Key ON: t.student_id = s.id / Status '+(i===0?'ON':'WHERE')+": t.status = 'done' / Projection: student_id, task_id / Order: s.id, t.id";$('count').textContent=rows.length+' expected rows in the logical model.';const table=document.createElement('table');table.innerHTML='<thead><tr><th>student_id</th><th>task_id</th></tr></thead>';const body=document.createElement('tbody');for(const row of rows){const tr=document.createElement('tr');for(const v of row){const td=document.createElement('td');td.textContent=v===null?'NULL':String(v);tr.append(td);}body.append(tr);}table.append(body);$('rows').append(table);$('status').textContent='Variant '+(i===0?'A':'B')+' modeled. Expected rows, not database execution or optimizer evidence.';}function show(i){$('sql').value=variants[i];inspect();}$('a').addEventListener('click',()=>show(0));$('b').addEventListener('click',()=>show(1));$('sql').addEventListener('input',clear);$('inspect').addEventListener('click',inspect);$('reset').addEventListener('click',()=>show(0));show(0);
})();
</script></body></html>