CS FundamentalsHardGATE CS
Sa(m, amt) holds monthly sales (2024-01, 100), (2024-02, 130), (2024-03, 130), (2024-04, 90), (2024-05, 150), (2024-06, 150), (2024-07, 120) (m is a sortable month string). How many rows does SELECT amt FROM (SELECT amt, LAG(amt) OVER (ORDER BY m) AS p FROM Sa) WHERE amt > p return?
- A. 3
- B. 2
- C. 5
- D. 4
Correct answer: B. 2
LAG gives the previous month's amount (NULL for the first row, so its comparison is UNKNOWN and the row is dropped). Only months 02 and 05 show a strict increase, so 2 rows are returned; using >= would add the repeated months 03 and 06 (4 rows).
More DBMS and SQL questions
- Table T(x) holds the values 1, NULL, 3, NULL, 3. What does SELECT COUNT(), COUNT(x), COUNT(DISTINCT x) FROM T return?
- Emp(n, dept, sal) has rows (A, d1, 40), (B, d1, 70), (C, d2, 45), (D, d2, 50), (E, d3, 80), (F, d3, 30), (G, d4, 60). How many rows does…
- Dept(d) has rows 1, 2, 3 and Emp(e, d) has rows (a, 1), (b, 1), (c, 2). How many rows does SELECT D.d FROM Dept D LEFT JOIN Emp E ON D.d =…
- R(A, B, C, D) has the functional dependencies AB -> C, C -> D and D -> A. How many candidate keys does R have?
- S(v) has rows 1, 2, 3 and U(v) has rows 2, NULL. What does SELECT COUNT() FROM S WHERE v NOT IN (SELECT v FROM U) return?
- A(x) holds 1, 2, 2, 3 and B(x) holds 2, 3, 3, 4. How many rows does SELECT x FROM A UNION SELECT x FROM B return?