Only two fields in a SQL statement to query the name of a student, the results and the table in the rankings _ database other

Source: Internet
Author: User

Yesterday I went to an interview with one of these questions:

There is a score sheet with only two fields, names and grades. How do you query a student's name, score, and rank in a table with an SQL statement?
I can not think of a specific implementation, I have two ideas: one is to associate a query through the join of his ranking of the SQL statement, which is implemented through group by.
Answer to even feel a little guilty. How do you realize this?
Suppose: The table name is course, and two fields are named and score, respectively.

Implementation statement:

Select students, scores, 
(select COUNT (*) from table WHERE A. Score <=) as rank from 
table as a

Method Two:

Declare @Course table (name varchar, CJ Int) 
INSERT into @Course 
Select ' A ', the 
union ALL 
Select ' B ', 
UNION ALL 
Select ' C ', the Select 
* FROM (select 
1+count (name) from @Course where CJ>T.CJ) m C,NAME,CJ from 
@Course t 
) tem 
where name= ' B '

If there is a record of the same score, Goodspeed's statement does have some flaws, but I made a slight change according to his statement is perfect.

Select Name,score, 
(select COUNT (*) +1 from course WHERE a.score<score) as sort from course as an order by 
sort

More please come here to view the comments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.