[{"id":"sqlserverintermediate-001","courseId":"sql-server-intermediate","domain":"querying","topic":"INNER and LEFT joins","prompt":"A report starts FROM Production.Product and joins Purchasing.ProductVendor on ProductID. Products with no vendor row vanish from the result. Which join type keeps them?","hint":"Think about which kind of join keeps rows that found no partner in the other table.","explain":"When two tables are combined, a row with no partner on the other side can be dropped or kept. A kept row gets NULLs in the other table's columns; the kind of join chosen decides which happens.","source":"https://learn.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql","interaction":{"type":"single"},"options":[{"id":"a","text":"LEFT OUTER JOIN","why":"Keeps every row of the left table, here the products, and sets the vendor columns to NULL when nothing matches."},{"id":"b","text":"INNER JOIN","whyNot":"Keeps only rows that match on both sides, so products with no vendor row are discarded."},{"id":"c","text":"RIGHT OUTER JOIN","whyNot":"Keeps unmatched rows of the right table, which are vendor rows here, so the products are still lost."},{"id":"d","text":"CROSS JOIN","whyNot":"Returns the full cross-product of the two tables, pairing every product with every vendor row rather than keeping unmatched ones."}],"answer":"a","explanation":"An inner join keeps only rows that match on both sides; a left outer join also keeps unmatched rows of the left table, with NULLs in the right table's columns.","concept":"An inner join keeps only rows that match on both sides; a left outer join also keeps unmatched rows of the left table, with NULLs in the right table's columns.","rev":1,"updatedAt":"2026-10-27","kind":"scenario","code":"SELECT p.ProductID, v.BusinessEntityID\nFROM Production.Product AS p\n??? Purchasing.ProductVendor AS v\n  ON p.ProductID = v.ProductID;","takeaways":["Missing rows after a join often mean an unmatched row was dropped; a left outer join keeps the left table's rows."]},{"id":"sqlserverintermediate-002","courseId":"sql-server-intermediate","domain":"querying","topic":"GROUP BY and HAVING","prompt":"A query totals TotalDue by year and must return only years whose total exceeds 1000000. Where does the condition on SUM(TotalDue) belong?","hint":"The condition tests a total computed over each year's rows, not a single row.","explain":"A total is computed across a set of rows, so it exists only after those rows are collected. Clauses run in a fixed sequence, and the place a condition is written decides which values it can see.","source":"https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql","interaction":{"type":"single"},"options":[{"id":"a","text":"HAVING, placed after GROUP BY","why":"HAVING filters the groups that GROUP BY produces, so it can test a total computed over each group."},{"id":"b","text":"WHERE, placed before GROUP BY","whyNot":"WHERE filters individual rows before grouping, so it has no group totals to test."},{"id":"c","text":"ORDER BY, placed at the end","whyNot":"ORDER BY only sorts the finished result; it can't drop a year from it."},{"id":"d","text":"A second GROUP BY that repeats the SUM","whyNot":"GROUP BY defines the groups; an aggregate can't be used there to filter them."}],"answer":"a","explanation":"HAVING filters the groups produced by GROUP BY, so it can test aggregate values such as totals; WHERE filters individual rows before grouping.","concept":"HAVING filters the groups produced by GROUP BY, so it can test aggregate values such as totals; WHERE filters individual rows before grouping.","rev":1,"updatedAt":"2026-10-27","kind":"scenario","code":"SELECT DATEPART(yyyy, OrderDate)\n       AS OrderYear,\n       SUM(TotalDue) AS YearTotal\nFROM Sales.SalesOrderHeader\nGROUP BY DATEPART(yyyy, OrderDate)\n-- condition goes here","takeaways":["Put conditions on single-row values in WHERE and conditions on aggregates such as SUM in HAVING."]},{"id":"sqlserverintermediate-003","courseId":"sql-server-intermediate","domain":"querying","topic":"Correlated subqueries","prompt":"A WHERE clause contains a subquery that reads e.ID from the outer query. How often does the engine evaluate that subquery?","hint":"Ask whether the inner query can run without reading anything from the outer row.","explain":"A subquery can be self-contained, or it can read columns from the query around it. Consider how the engine could produce one answer that works for every outer value at once.","source":"https://learn.microsoft.com/en-us/sql/relational-databases/performance/subqueries?view=sql-server-ver17","interaction":{"type":"single"},"options":[{"id":"a","text":"Once for each outer row considered, using that row's value","why":"The inner query depends on the outer row's value, so it is evaluated again for each row that might be selected."},{"id":"b","text":"Once, before the outer query starts, with its result reused for every row","whyNot":"A subquery that reads outer values can't be settled in advance, because the value changes from row to row."},{"id":"c","text":"Once per distinct outer value, since repeated values are skipped","whyNot":"The engine doesn't skip repeats; the subquery is run once for each outer row examined."},{"id":"d","text":"Only when the outer query returns no rows, as a fallback","whyNot":"The subquery is part of the WHERE test for each row, so it isn't a fallback that runs only on an empty result."}],"answer":"a","explanation":"A correlated subquery references the outer query, so it is executed repeatedly, once for each row that might be selected by the outer query.","concept":"A correlated subquery references the outer query, so it is executed repeatedly, once for each row that might be selected by the outer query.","rev":1,"updatedAt":"2026-10-27","kind":"scenario","code":"SELECT e.Name\nFROM Employee AS e\nWHERE 5000 IN\n  (SELECT s.Bonus\n   FROM SalesPerson AS s\n   WHERE s.EmpID = e.ID);","takeaways":["A correlated subquery depends on the outer query, so it runs once per outer row, not once in total."]},{"id":"sqlserverintermediate-004","courseId":"sql-server-intermediate","domain":"querying","topic":"Common table expressions","prompt":"A report uses a common table expression named order_totals twice in its outer SELECT. The engine runs the CTE's defining query again for each reference.","hint":"Think about whether the engine keeps a CTE's rows between uses in one statement.","explain":"A named query in a WITH clause can be read several times by the outer statement. Between reads, the rows it produced may be kept for reuse, or the query may simply be evaluated again.","source":"https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"CTE results aren't materialized, so each outer reference to the named result set causes the defining query to run again."},{"id":"b","text":"False","whyNot":"The second reference doesn't reuse stored rows; the defining query is run again, so the statement holds."}],"answer":"a","explanation":"Common table expression results aren't materialized, so each outer reference re-executes the defining query; a temporary object is the alternative for reused rows.","concept":"Common table expression results aren't materialized, so each outer reference re-executes the defining query; a temporary object is the alternative for reused rows.","rev":1,"updatedAt":"2026-10-27","kind":"scenario","takeaways":["A CTE used several times in one statement runs its definition several times; store the rows in a temporary object when reuse matters."]},{"id":"sqlserverintermediate-005","courseId":"sql-server-intermediate","domain":"querying","topic":"Recursive CTEs","prompt":"A recursive common table expression is executed by the engine in stages. Put these stages of its execution in the order the engine runs them.","hint":"Work out what must exist before the first pass of recursion can start.","explain":"A recursive query works in repeated passes, where each pass feeds on what the previous pass produced. Its definition has parts that start the process and parts that continue it.","source":"https://learn.microsoft.com/en-us/sql/t-sql/queries/recursive-common-table-expression-transact-sql","interaction":{"type":"sort","items":[{"text":"Split the CTE expression into anchor and recursive members","why":"The engine must know which parts start the recursion and which continue it before running either."},{"text":"Run the anchor members to produce the base result set T0","why":"The first set of rows must exist before any pass can build on it."},{"text":"Run the recursive members on each new set, repeating until an empty set comes back","why":"Each pass takes the previous output as input, and the loop stops when a pass returns no rows."},{"text":"Return the UNION ALL of T0 through Tn","why":"The final result is every pass's rows combined, which happens only after the passes have finished."}]},"options":[],"answer":"o:abcd","explanation":"A recursive CTE runs its anchor member once to get T0, then runs its recursive members repeatedly until an empty set, returning the UNION ALL of all results.","concept":"A recursive CTE runs its anchor member once to get T0, then runs its recursive members repeatedly until an empty set, returning the UNION ALL of all results.","rev":1,"updatedAt":"2026-10-27","kind":"scenario","takeaways":["Recursion stops when a pass returns no rows; a faulty recursive member can loop forever, so MAXRECURSION caps the depth."]}]