SQL Implementation Group sorting-instances get the first three students ' names for male and female grades

Source: Internet
Author: User

Table Scores (Name,sex,score), respectively, to obtain the first three names of male and female students.

Create a table:

CREATE TABLE Scores (
Name VARCHAR2 (8), sex varchar2 (1), score number (3,0)
)

Insert Student Scores

INSERT into scores (Name,sex,score) VALUES (' Male a ', ' 1 ', 100);
INSERT into scores (Name,sex,score) values (' Male B ', ' 1 ', 90);
INSERT into scores (Name,sex,score) VALUES (' Male C ', ' 1 ', 80);
INSERT into scores (Name,sex,score) VALUES (' Male d ', ' 1 ', 70);
INSERT into scores (Name,sex,score) values (' Female a ', ' 2 ', 100);
INSERT into scores (Name,sex,score) values (' Female B ', ' 2 ', 90);
INSERT into scores (Name,sex,score) values (' Female C ', ' 2 ', 80);
INSERT into scores (Name,sex,score) values (' Female d ', ' 2 ', 70);

Inquire:

SELECT name, Sex,score
From (SELECT name,
Sex
Score,
Row_number () over (PARTITION by sex ORDER by score DESC) RW
From scores)
WHERE RW < 4

Operation Result:

1 Male a 1 100
2 Male B 1 90
3 Male C 1 80
4 Female a 2 100
5 Female B 2 90
6 Female C 2 80

SQL Implementation Group sorting-instances get the first three students ' names for male and female grades

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.