SQL Server 8: SQL queries the two courses with the highest score per student

Source: Internet
Author: User

This is an interview question. If you have time to write it down today, you may encounter such problems as a reference! When I first saw this question, many people may think of keywords.TopIn fact, the keyword used here isPartition,

Now, let's take a look at the table structure and data!

 

 

Next, let's take a look at the Partition Function and execute the statement.

 

 
Select C. *, row_number () over (partition by C. ID order by C. sorce DESC) rank from DBO. courcer C

The result is as follows:

 

The final result is clearly displayed here!

 

View code

 Select S. * , C. *  From  DBO. Student s  Left   Join  (  Select C. * , Row_number () Over (Partition By C. ID Order   By C. sorce Desc ) Rank From  DBO. courcer c) c  On S. ID =  C. ID  Where C. Rank <=  2 

 

 

 

Related Article

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.