Use an SQL statement to find out how to achieve a student's name of more than 80 points per course

Source: Internet
Author: User

Build table:

1 Create TableScore (2Namevarchar( -),3Coursevarchar( -),    4Scoreint)5 6 Go7 8 Insertscore9 Select 'Hanmeimei','Chinese',Bayi Union  AllTen Select 'Hanmeimei','Mathematics', the Union  All  One Select 'Hanmeimei',' History',Bayi Union  All  A Select 'Hanmeimei','Geography', the Union  All  - Select 'Lilei','Chinese', the Union  All  - Select 'Lilei','Mathematics', - Union  All  the Select 'Jimgreen','Chinese',Bayi Union  All  - Select 'Jimgreen','Mathematics', - Union  All  - Select 'Jimgreen','中文版', -

Method 1: Find the name that has the subject not reaching 80 points, and then filter

 1  select  distinct   name  2   Score a  3  where  not  exists  (select  1  from  score where  A.name=  name and  score<  80 ) 
1 Select distinct    2 from 3 where not in (select  from where score<)

Method 2: Filter by group after the way

1 Selectname2  fromscore3 Group  byname4  having COUNT(1)=SUM( Case  whenScore>= the  Then 1 Else 0 End)
1 Select  2from score3Group by 4  havingMIN(score)>=

Results:

Name

Jimgreen
Lilei

In the case of fewer records (9), the ratio of the execution plan (SQL2008) of the above four methods is

14:14:11:11

Use an SQL statement to find out how to achieve a student's name of more than 80 points per course

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.