Which Transact-SQL query should you use?

You create a table that has the StudentCode, SubjectCode, and Marks columns to record
mid-year marks for students. The table has marks obtained by 50 students for various
subjects.
You need to ensure that the following requirements are met:
Students must be ranked based on their average marks.
If one or more students have the same average, the same rank must be given to these
students.
Consecutive ranks must be skipped when the same rank is assigned.
Which Transact-SQL query should you use?

You create a table that has the StudentCode, SubjectCode, and Marks columns to record
mid-year marks for students. The table has marks obtained by 50 students for various
subjects.
You need to ensure that the following requirements are met:
Students must be ranked based on their average marks.
If one or more students have the same average, the same rank must be given to these
students.
Consecutive ranks must be skipped when the same rank is assigned.
Which Transact-SQL query should you use?

A.
SELECT StudentCode as Code,
RANK() OVER(ORDER BY AVG (Marks) DESC) AS Value
FROM StudentMarks
GROUP BY StudentCode

B.
SELECT Id, Name, Marks,
DENSE_RANK() OVER(ORDER BY Marks DESC) AS Rank
FROM StudentMarks

C.
SELECT StudentCode as Code,
DENSE_RANK() OVER(ORDER BY AVG (Marks) DESC) AS Value
FROM StudentMarks
GROUP BY StudentCode

D.
SELECT StudentCode as Code,
NTILE(2) OVER(ORDER BY AVG (Marks) DESC) AS Value
FROM StudentMarks
GROUP BY StudentCode

E.
SELECT StudentCode AS Code,Marks AS Value FROM (
SELECT StudentCode, Marks AS Marks,
RANK() OVER(PARTITION BY SubjectCode ORDER BY Marks ASC) AS Rank
FROM StudentMarks) tmp
WHERE Rank = 1

F.
SELECT StudentCode AS Code,Marks AS Value FROM (
SELECT StudentCode, Marks AS Marks,
RANK() OVER(PARTITION BY SubjectCode ORDER BY Marks DESC) AS Rank
FROM StudentMarks) tmp
WHERE Rank = 1

G.
SELECT StudentCode AS Code,Marks AS Value FROM (
SELECT StudentCode, Marks AS Marks,
RANK() OVER(PARTITION BY StudentCode ORDER BY Marks ASC) AS Rank
FROM StudentMarks) tmp
WHERE Rank = 1

H.
SELECT StudentCode AS Code,Marks AS Value FROM (
SELECT StudentCode, Marks AS Marks,
RANXO OVER(PARTITION BY StudentCode ORDER BY Marks DESC) AS Rank
FROM StudentMarks) tmp
WHERE Rank = 1

Explanation:
Reference:
http://msdn.microsoft.com/en-us/library/ms189798.aspx



Leave a Reply 5

Your email address will not be published. Required fields are marked *


ritesh

ritesh

I think correct answer is “C”

Edouard

Edouard

No, the right answer is A because of this requirement “Consecutive ranks must be skipped…”.

The RANK() function skips ranks when the same rank is assigned several times, but DENSE_RANK() does not.

sqlninja

sqlninja

A is the correct answer

Yommy O.

Yommy O.

The correct answer is absolutely A. First off, the question says “Consecutive ranks must be skipped when the same rank is assigned.” That knocks off Options B,C,D, and H.

Secondly, “Students must be ranked based on their average marks”; of all the answer options comprising a RANK() function, only option A has an ‘ORDER BY AVG(Marks) DESC’ clause.

JosefTheGreat

JosefTheGreat

A fo sho! cause Rank() is skipping the ranking when duplicates exist.