Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Ranking
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
--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 top half of the students arranged by their average marks must be given a rank of 1 --and the remaining students must be given a rank of 2. Which Transact-SQL query should you use? create table student(StudentCode int, SubjectCode int, Marks int) insert into student values (1,1,10),(1,2,20),(1,3,30) ,(2,1,30),(2,2,40),(2,3,50) ,(3,1,40),(3,2,50),(3,3,50) ,(4,1,40),(4,2,50),(4,3,50) go select StudentCode,dense_rank() over(order by avg(marks) desc) as rank from student group by studentcode SELECT StudentCode as Code, DENSE_RANK() OVER (ORDER BY AVG (Marks) DESC) AS Value FROM student GROUP BY StudentCode SELECT StudentCode as Code, NTILE (2) OVER (ORDER BY AVG (Marks) DESC) AS Value FROM student GROUP BY StudentCode -- get subjectcode,studentcode, marks of those who have maximum marks in each subject select StudentCode , SubjectCode , Marks from( select StudentCode , SubjectCode , Marks,rank() over (partition by subjectcode order by marks desc ) as rank from student) tmp where tmp.rank=1
View schema
Execution time: 0 sec, rows selected: 12, rows affected: 12, absolute service time: 0.14 sec
fork mode
|
history
|
discussion
StudentCode
rank
1
3
1
2
4
1
3
2
2
4
1
3
Code
Value
1
3
1
2
4
1
3
2
2
4
1
3
Code
Value
1
3
1
2
4
1
3
2
2
4
1
2