[{"id":"sqladvanced-001","courseId":"sql-advanced","domain":"querying","topic":"CASE constant folding errors","prompt":"A teammate adds a CASE branch whose result is the constant expression 1/0, even though no row can ever take that branch. What usually happens in PostgreSQL?","hint":"Think about when a constant expression gets evaluated, compared with when a CASE branch is picked.","explain":"CASE skips subexpressions it does not need, so a guarded division is safe for column values. Evaluation timing is not always that simple, especially for constant expressions.","source":"https://www.postgresql.org/docs/current/functions-conditional.html","interaction":{"type":"single"},"options":[{"id":"a","text":"The query fails with a division-by-zero error while it is being planned","why":"A constant subexpression is usually evaluated at planning time, so the error appears even though the branch never runs."},{"id":"b","text":"The branch is skipped at run time, and the query runs normally","whyNot":"CASE does skip unneeded subexpressions at run time, but constant expressions can be evaluated earlier, during planning."},{"id":"c","text":"The query is rejected as a syntax error before planning","whyNot":"1/0 is valid syntax; the problem is evaluating it, not parsing it."},{"id":"d","text":"The query returns NULL for every row","whyNot":"CASE returns NULL only when no WHEN matches and there is no ELSE; an unreachable branch does not cause that."}],"answer":"a","explanation":"CASE evaluates only the subexpressions it needs at run time, but a constant such as 1/0 is usually evaluated at planning time, so it can fail even in an arm that never runs.","concept":"CASE evaluates only the subexpressions it needs at run time, but a constant such as 1/0 is usually evaluated at planning time, so it can fail even in an arm that never runs.","rev":1,"kind":"scenario","takeaways":["Guard divisions by column values with CASE, but keep constant expressions like 1/0 out of branches, since they are evaluated early."]},{"id":"sqladvanced-002","courseId":"sql-advanced","domain":"querying","topic":"Output expressions after LIMIT","prompt":"A query's SELECT list calls a set-returning function g(n) and a volatile function f(x). It sorts with ORDER BY on another column, ends with LIMIT 10, and neither function appears in ORDER BY, GROUP BY or DISTINCT. Put these steps in the order PostgreSQL effectively runs them.","hint":"Ask which steps can cut rows, and which output expressions have to wait until the rows are cut.","explain":"Output expressions are the SELECT-list items. Where the database evaluates them can shift relative to sorting and row limiting, depending on the kind of expression and on how the rest of the query uses it.","source":"https://www.postgresql.org/docs/current/sql-select.html","interaction":{"type":"sort","items":[{"text":"ORDER BY sorts the rows","why":"Output expressions with set-returning functions are evaluated only after sorting, so the sort comes first."},{"text":"g(n) expands into its output rows","why":"A set-returning function is evaluated after sorting but before LIMIT, so LIMIT can cut off its output."},{"text":"LIMIT 10 keeps the first ten rows","why":"Rows cut here never reach the output expressions that wait until after limiting."},{"text":"f(x) is computed for the rows that remain","why":"Volatile output expressions are evaluated after sorting and limiting, so no call is made for rows that LIMIT dropped."}]},"options":[],"answer":"o:abcd","explanation":"Output expressions are evaluated after sorting and LIMIT unless DISTINCT, ORDER BY or GROUP BY uses them, while set-returning functions are expanded after sorting but before LIMIT, so LIMIT cuts their output.","concept":"Output expressions are evaluated after sorting and LIMIT unless DISTINCT, ORDER BY or GROUP BY uses them, while set-returning functions are expanded after sorting but before LIMIT, so LIMIT cuts their output.","rev":1,"kind":"scenario","takeaways":["A volatile SELECT-list function runs only for rows that reach the output, unless DISTINCT, ORDER BY or GROUP BY references it."]},{"id":"sqladvanced-003","courseId":"sql-advanced","domain":"querying","topic":"LIKE default escape","prompt":"Your table has codes such as _temp_build. You need the codes that start with a literal underscore followed by temp. Which LIKE pattern does that in PostgreSQL with default settings?","hint":"Some pattern characters are wildcards, unless a marker placed before them turns that off.","explain":"In LIKE, the underscore and percent sign are wildcards. A marker placed before one makes it match only the literal character. A pattern must match the whole string.","source":"https://www.postgresql.org/docs/current/functions-matching.html","interaction":{"type":"single"},"options":[{"id":"a","text":"LIKE '\\_temp%'","why":"The backslash escapes the underscore so it matches a real underscore, and the percent sign covers the rest."},{"id":"b","text":"LIKE '_temp%'","whyNot":"An unescaped underscore matches any single character, so this pattern would also match xtemp_build."},{"id":"c","text":"LIKE '/_temp%'","whyNot":"A slash is not the default escape character; LIKE uses the backslash unless ESCAPE names another."},{"id":"d","text":"LIKE '\\\\_temp%'","whyNot":"A doubled backslash matches one literal backslash, and the underscore then becomes a wildcard again."}],"answer":"a","explanation":"LIKE treats _ and % as wildcards. The default escape character is the backslash, so \\_ matches a literal underscore, and the pattern must cover the whole string.","concept":"LIKE treats _ and % as wildcards. The default escape character is the backslash, so \\_ matches a literal underscore, and the pattern must cover the whole string.","rev":1,"kind":"scenario","takeaways":["An unescaped underscore in LIKE matches any single character; escape it when you mean a literal underscore."]},{"id":"sqladvanced-004","courseId":"sql-advanced","domain":"querying","topic":"jsonpath predicate operators","prompt":"A query needs the true, false or null result of the predicate check '$.a > 3' on a jsonb document. jsonb_path_exists is the right function for that job.","hint":"Consider which kind of jsonpath each function is built for: selecting items or checking a condition.","explain":"A jsonpath can select items from a document, or it can be a predicate that yields true, false or null. The helper functions are split by which of these kinds they handle.","source":"https://www.postgresql.org/docs/current/functions-json.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"jsonb_path_exists is only useful for SQL-standard path expressions, because a predicate check always returns a value."},{"id":"b","text":"False","why":"For a predicate check, jsonb_path_match returns the SQL boolean result, so it is the function to use."}],"answer":"b","explanation":"jsonb_path_exists suits SQL-standard path expressions; predicate check expressions always return a value, and jsonb_path_match returns their SQL boolean result.","concept":"jsonb_path_exists suits SQL-standard path expressions; predicate check expressions always return a value, and jsonb_path_match returns their SQL boolean result.","rev":1,"kind":"scenario","takeaways":["Use jsonb_path_exists to test whether a path selects anything, and jsonb_path_match for a predicate's true/false result."]},{"id":"sqladvanced-005","courseId":"sql-advanced","domain":"querying","topic":"now() vs clock_timestamp()","prompt":"You call a time function once per row of a long UPDATE and want the actual clock time at each call, even inside one statement. Which function returns that?","hint":"Some clocks freeze when a transaction or statement begins; look for the one that does not.","explain":"Some time functions return a value fixed at the start of the current transaction or statement. Others read the clock at the moment they are called. Pick by when you need the value to reflect.","source":"https://www.postgresql.org/docs/current/functions-datetime.html","interaction":{"type":"single"},"options":[{"id":"a","text":"now()","whyNot":"now() is the transaction start time, so it stays the same for the whole transaction."},{"id":"b","text":"clock_timestamp()","why":"clock_timestamp() returns the actual current time at the instant it is called, so it changes within a statement."},{"id":"c","text":"statement_timestamp()","whyNot":"It returns the start time of the current statement, which does not change while that statement runs."},{"id":"d","text":"CURRENT_TIMESTAMP","whyNot":"It is the SQL-standard form of the transaction start time, so it matches now()."}],"answer":"b","explanation":"now() and CURRENT_TIMESTAMP return the transaction start time, statement_timestamp() the statement start time, and clock_timestamp() the actual time at each call.","concept":"now() and CURRENT_TIMESTAMP return the transaction start time, statement_timestamp() the statement start time, and clock_timestamp() the actual time at each call.","rev":1,"kind":"scenario","takeaways":["now() stays fixed for the whole transaction; use clock_timestamp() only when the value must change within one statement."]}]