CS FundamentalsModerateGATE CS (PYQ pattern)
LG(g, t, v): (a,1,10), (a,2,15), (a,3,12), (b,1,7), (b,2,7), (b,3,20). What does SELECT SUM(d) FROM (SELECT v - COALESCE(LAG(v) OVER (PARTITION BY g ORDER BY t), v) AS d FROM LG) x return?
- A. 10
- B. 22
- C. 15
- D. 0
Correct answer: C. 15
Within each partition the first row has no LAG, so COALESCE makes its difference 0. Group a: 0 + 5 - 3 = 2; group b: 0 + 0 + 13 = 13. Total 15. Without PARTITION BY the first b row would be compared with the last a row (giving 10).
More DBMS and SQL questions
- DD(id, k, v): (1,1,a), (2,1,b), (3,2,c), (4,2,d), (5,2,e), (6,3,f), (7,NULL,g), (8,NULL,h). A cleanup runs DELETE FROM DD WHERE id NOT IN…
- TP(g, id, s): (a,1,90), (a,2,90), (a,3,80), (a,4,70), (b,5,60), (b,6,60), (b,7,60), (c,8,50). Let n1, n2, n3 be the numbers of rows with…
- SP(id, s): (1,10), (2,10), (3,10), (4,20), (5,20), (6,30), (7,NULL), (8,NULL). How many rows does SELECT FROM SP a JOIN SP b ON a.s = b.s…
- Table T(x) has 4 rows. How many rows does each of these return? (a) SELECT COUNT() FROM T HAVING COUNT() > 5; (b) SELECT COUNT() FROM T…
- Which of the following statements about standard SQL are correct? I. A column alias defined in the SELECT list can be used in ORDER BY…
- X1(a): 1, 1, 2, 2, 3, 4 and Y1(a): 2, 4, 5. Let p = rows from SELECT a FROM X1 WHERE a NOT IN (SELECT a FROM Y1) and q = rows from SELECT…