[{"id":"sqlbeginner-001","courseId":"sql-beginner","domain":"querying","topic":"Equals comparison with NULL","prompt":"In PostgreSQL, the query below returns true, because two null values are equal to each other.","hint":"Null stands for an unknown value; think about what a comparison with an unknown gives.","explain":"A null marks an unknown value. Ordinary comparison operators do not treat two unknowns as equal, and a WHERE clause keeps only rows whose condition is true.","source":"https://www.postgresql.org/docs/current/functions-comparison.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Two unknown values are not known to be equal, so the comparison yields null, not true."},{"id":"b","text":"False","why":"An ordinary comparison with a null yields null, which is neither true nor false, so the result is not true."}],"answer":"b","explanation":"Ordinary comparison operators yield null (signifying unknown), not true or false, when either input is null.","concept":"Ordinary comparison operators yield null (signifying unknown), not true or false, when either input is null.","rev":1,"kind":"scenario","code":"SELECT NULL = NULL AS same;","takeaways":["Test for missing values with IS NULL, never with = NULL."]},{"id":"sqlbeginner-002","courseId":"sql-beginner","domain":"querying","topic":"BETWEEN range bounds","prompt":"A PostgreSQL query filters a table with WHERE price BETWEEN 10 AND 20. Which rows does it return?","hint":"Think about whether the two boundary values belong to the range.","explain":"BETWEEN tests whether a value lies within a range set by two bounds. It is shorthand for comparisons built from ordinary operators, so look closely at which operators it uses at each bound.","source":"https://www.postgresql.org/docs/current/functions-comparison.html","interaction":{"type":"single"},"options":[{"id":"a","text":"Prices strictly between 10 and 20, excluding both 10 and 20","whyNot":"That would need > and <. BETWEEN treats its endpoints as part of the range."},{"id":"b","text":"Only the rows priced exactly 10 or exactly 20","whyNot":"BETWEEN returns every value in the range, not just the two endpoints."},{"id":"c","text":"Prices from 10 to 20, including both 10 and 20","why":"BETWEEN includes both endpoints, so it is the same as price >= 10 AND price <= 20."},{"id":"d","text":"Prices from 10 to 20, including 10 but not 20","whyNot":"BETWEEN includes the upper endpoint as well as the lower one."}],"answer":"c","explanation":"BETWEEN treats its endpoint values as included in the range, so a BETWEEN x AND y is equivalent to a >= x AND a <= y.","concept":"BETWEEN treats its endpoint values as included in the range, so a BETWEEN x AND y is equivalent to a >= x AND a <= y.","rev":1,"kind":"scenario","code":"SELECT item, price\nFROM products\nWHERE price BETWEEN 10 AND 20;","takeaways":["BETWEEN includes both bounds; write > and < for an exclusive range."]},{"id":"sqlbeginner-003","courseId":"sql-beginner","domain":"querying","topic":"Default NULL sort position","prompt":"A PostgreSQL table players has scores 9, 4 and 1, plus one row with a NULL score. Put these rows in the order that ORDER BY score DESC returns them, top to bottom.","hint":"Nulls are not ordinary values, so the database needs a rule for where they go.","explain":"Sort direction and NULL placement are separate choices. Without an explicit NULLS clause, the database uses a default for where nulls go, and that default depends on the sort direction.","source":"https://www.postgresql.org/docs/current/queries-order.html","interaction":{"type":"sort","items":[{"text":"Row with a NULL score","why":"In a descending sort, nulls come first by default, because they count as larger than any value."},{"text":"Row with score 9","why":"The largest non-null value leads the non-null rows in a descending sort."},{"text":"Row with score 4","why":"The middle non-null value follows 9 in descending order."},{"text":"Row with score 1","why":"The smallest value comes last in a descending sort."}]},"options":[],"answer":"o:abcd","explanation":"By default null values sort as if larger than any non-null value, so NULLS FIRST is the default for DESC order and NULLS LAST otherwise.","concept":"By default null values sort as if larger than any non-null value, so NULLS FIRST is the default for DESC order and NULLS LAST otherwise.","rev":1,"kind":"scenario","takeaways":["Add NULLS LAST to a DESC sort to push nulls to the bottom."]},{"id":"sqlbeginner-004","courseId":"sql-beginner","domain":"querying","topic":"Row order without ORDER BY","prompt":"In PostgreSQL, SELECT name FROM users; with no ORDER BY always returns the rows in the order they were inserted.","hint":"Only a sort step can guarantee the order of output rows.","explain":"A query result is a table, and a table has no built-in row order. Only an explicit sort step fixes the order; without one, the database may return rows in whatever order its plan produces.","source":"https://www.postgresql.org/docs/current/queries-order.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"The order depends on the scan plan and how rows sit on disk, so insertion order is not guaranteed."},{"id":"b","text":"False","why":"Without ORDER BY the order is unspecified and must not be relied on, so insertion order is not guaranteed."}],"answer":"b","explanation":"Without ORDER BY, rows come back in an unspecified order that depends on the plan and disk layout, and must not be relied on.","concept":"Without ORDER BY, rows come back in an unspecified order that depends on the plan and disk layout, and must not be relied on.","rev":1,"kind":"scenario","takeaways":["Only ORDER BY guarantees row order; never assume insertion order."]},{"id":"sqlbeginner-005","courseId":"sql-beginner","domain":"querying","topic":"LIMIT and OFFSET","prompt":"In PostgreSQL, which TWO statements about LIMIT and OFFSET are true?","hint":"Think about the order in which OFFSET and LIMIT act, and what happens without a sort.","explain":"LIMIT caps how many rows a query returns, and OFFSET sets how many rows to pass over first. Both work on the result after the rest of the query, so which rows they pick depends on that result's order.","source":"https://www.postgresql.org/docs/current/queries-limit.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"OFFSET skips that many rows first, and then LIMIT counts the rows it returns.","why":"OFFSET rows are skipped before LIMIT starts counting the rows that are returned."},{"id":"b","text":"Without ORDER BY, LIMIT and OFFSET pages may not line up consistently across queries.","why":"With no ordering, different LIMIT and OFFSET values can select inconsistent subsets of the rows."},{"id":"c","text":"LIMIT 10 always returns exactly ten rows, even when the query itself yields fewer.","whyNot":"LIMIT caps the count: it returns no more than that many rows, and possibly fewer."},{"id":"d","text":"OFFSET 0 is an error, because a skip count must be at least one.","whyNot":"OFFSET 0 is the same as omitting the OFFSET clause, so it is valid."},{"id":"e","text":"A large OFFSET costs nothing, because the skipped rows are never computed.","whyNot":"Skipped rows still have to be computed inside the server, so a large OFFSET can be slow."},{"id":"f","text":"LIMIT with a NULL argument returns no rows at all.","whyNot":"LIMIT with a NULL argument behaves the same as omitting the LIMIT clause."}],"answer":"ab","explanation":"OFFSET skips rows before LIMIT counts the rows it returns, and without ORDER BY the rows chosen are unpredictable.","concept":"OFFSET skips rows before LIMIT counts the rows it returns, and without ORDER BY the rows chosen are unpredictable.","rev":1,"kind":"scenario","takeaways":["LIMIT without ORDER BY gives an arbitrary subset; always sort before paging."]}]