[{"id":"sqlintermediate-001","courseId":"sql-intermediate","domain":"querying","topic":"IS DISTINCT FROM comparison","prompt":"In PostgreSQL, the expression NULL IS DISTINCT FROM NULL returns false.","hint":"Think about how = and <> handle NULL, and how the DISTINCT FROM predicates differ from them.","explain":"Ordinary comparisons with NULL yield unknown rather than true or false. The IS [NOT] DISTINCT FROM predicates are built for NULL inputs and always return true or false.","source":"https://www.postgresql.org/docs/current/functions-comparison.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"Both inputs are NULL, which DISTINCT FROM treats as the same value, so they are not distinct and the result is false."},{"id":"b","text":"False","whyNot":"The predicate never returns unknown here; with two NULLs it returns false, so the statement is right."}],"answer":"a","explanation":"IS DISTINCT FROM treats NULL as a normal value: two NULLs are not distinct, and NULL against a non-null value is distinct.","concept":"IS DISTINCT FROM treats NULL as a normal value: two NULLs are not distinct, and NULL against a non-null value is distinct.","rev":1,"kind":"scenario","takeaways":["Use IS [NOT] DISTINCT FROM when two NULLs should count as equal instead of unknown."]},{"id":"sqlintermediate-002","courseId":"sql-intermediate","domain":"querying","topic":"Output alias in WHERE","prompt":"In PostgreSQL, the WHERE clause of this query may refer to the output alias total defined in the SELECT list.","hint":"Think about which clauses can see the names that the SELECT list gives its output columns.","explain":"Each clause of a SELECT works on what the steps before it produced. Output column names come from the select list, and not every clause is allowed to use them, so check the rules for each clause.","source":"https://www.postgresql.org/docs/current/sql-select.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"WHERE can't see output names, so total is not defined there; the expression has to be written out."},{"id":"b","text":"False","why":"Output aliases aren't visible in WHERE; the query has to repeat price * qty instead."}],"answer":"b","explanation":"Output names from the SELECT list work in ORDER BY and GROUP BY, but not in WHERE or HAVING, where the expression must be written out.","concept":"Output names from the SELECT list work in ORDER BY and GROUP BY, but not in WHERE or HAVING, where the expression must be written out.","rev":1,"kind":"scenario","code":"SELECT price * qty AS total\nFROM order_lines\nWHERE total > 100;","takeaways":["An alias from the SELECT list won't filter rows in WHERE; repeat the expression there."]},{"id":"sqlintermediate-003","courseId":"sql-intermediate","domain":"querying","topic":"Logical order of SELECT","prompt":"A query uses WHERE, GROUP BY, a window function in its SELECT list, and ORDER BY. Put these steps in the order PostgreSQL logically runs them, first to last.","hint":"Ask which steps need individual rows, which need groups, and which need the finished output.","explain":"A query is not evaluated all at once. Each step works on the output of the step before it, and some steps need individual rows while others need groups or finished output.","source":"https://www.postgresql.org/docs/current/tutorial-window.html","interaction":{"type":"sort","items":[{"text":"WHERE","why":"Filters individual rows before any grouping or window calculation happens."},{"text":"GROUP BY","why":"Forms the groups that aggregates use; window functions logically run only after this step."},{"text":"Window function in the SELECT list","why":"Logically executes after WHERE and GROUP BY have been processed, and after regular aggregates."},{"text":"ORDER BY","why":"Sorts the output after the select list has been processed, so it comes last."}]},"options":[],"answer":"o:abcd","explanation":"WHERE and GROUP BY are processed before window functions, which are allowed only in the SELECT list and ORDER BY; ORDER BY sorts the output after the select list is processed.","concept":"WHERE and GROUP BY are processed before window functions, which are allowed only in the SELECT list and ORDER BY; ORDER BY sorts the output after the select list is processed.","rev":1,"kind":"scenario","takeaways":["Window functions can't appear in WHERE or GROUP BY, because they run after those clauses."]},{"id":"sqlintermediate-004","courseId":"sql-intermediate","domain":"querying","topic":"DISTINCT ON first rows","prompt":"A teammate wants the latest report for each location and writes the query below with no ORDER BY. Which row does each location get back?","hint":"DISTINCT ON keeps one row per group; ask what decides which row of the group survives.","explain":"DISTINCT ON keeps one row from each set of rows that match on the listed expressions. Which row survives inside a set depends on whether the query tells the database how to order that set.","source":"https://www.postgresql.org/docs/current/sql-select.html","interaction":{"type":"single"},"options":[{"id":"a","text":"The row with the latest time, since DISTINCT ON keeps the newest row","whyNot":"Nothing in the query ranks rows by time, so no row is picked as the newest."},{"id":"b","text":"Whichever row comes first for that location, which is unpredictable","why":"With no ORDER BY, the first row of each set is unpredictable, so the latest report isn't guaranteed."},{"id":"c","text":"The earliest row, because DISTINCT ON keeps the first row inserted","whyNot":"DISTINCT ON doesn't use insertion order; without ORDER BY the kept row is unspecified."},{"id":"d","text":"An error, because DISTINCT ON requires an ORDER BY clause","whyNot":"The query is valid as written; it simply leaves the kept row unspecified."}],"answer":"b","explanation":"DISTINCT ON keeps one row per set of matching expressions; without ORDER BY, which row is kept is unpredictable.","concept":"DISTINCT ON keeps one row per set of matching expressions; without ORDER BY, which row is kept is unpredictable.","rev":1,"kind":"scenario","code":"SELECT DISTINCT ON (location)\n       location, time, report\nFROM weather_reports;","takeaways":["Put the DISTINCT ON expressions first in ORDER BY, followed by the column that picks the row you want."]},{"id":"sqlintermediate-005","courseId":"sql-intermediate","domain":"querying","topic":"ORDER BY output alias","prompt":"Table1 has columns a, b and c. A query selects a + b AS sum, c and then ends with ORDER BY sum + c. What does PostgreSQL do?","hint":"Check which forms of a name ORDER BY accepts for an output column.","explain":"ORDER BY can name an output column or use an expression over the input columns. Output names follow their own rules, which are narrower than the rules for expressions.","source":"https://www.postgresql.org/docs/current/queries-order.html","interaction":{"type":"single"},"options":[{"id":"a","text":"It sorts by a + b + c, expanding the alias into its expression","whyNot":"Aliases aren't macros; PostgreSQL does not expand sum back into a + b."},{"id":"b","text":"It raises an error, since an output name can't be used inside an expression here","why":"A name from the select list must stand alone in ORDER BY, and sum + c is rejected."},{"id":"c","text":"It sorts by c alone and quietly drops the sum term","whyNot":"PostgreSQL doesn't silently discard part of an ORDER BY expression."},{"id":"d","text":"It ignores the expression and returns the rows unsorted","whyNot":"ORDER BY does sort; this query is rejected for a different reason."}],"answer":"b","explanation":"An output column name in ORDER BY must stand alone; it cannot be combined with other terms in an expression.","concept":"An output column name in ORDER BY must stand alone; it cannot be combined with other terms in an expression.","rev":1,"kind":"scenario","code":"SELECT a + b AS sum, c\nFROM table1\nORDER BY sum + c;","takeaways":["To sort by a computed value, repeat the full expression in ORDER BY instead of its alias."]}]