MySQL Student Stats Exercise

Source: Internet
Author: User
Tags mul

Mysql> Show CREATE TABLE SC \g
1. Row ***************************
Table:sc
Create table:create Table ' SC ' (
' Sid ' varchar () DEFAULT NULL,
' Cid ' varchar DEFAULT NULL,
' Score ' decimal (18,1) DEFAULT NULL,
KEY ' Sid ' (' Sid '),
KEY ' CID ' (' CID '),
CONSTRAINT ' Sc_ibfk_1 ' FOREIGN KEY (' Sid ') REFERENCES ' Student ' (' Sid '),
CONSTRAINT ' sc_ibfk_2 ' FOREIGN KEY (' cid ') REFERENCES ' Course ' (' CID ')
) Engine=innodb DEFAULT Charset=utf8
1 row in Set (0.05 sec)

Mysql> desc SC
;
+-------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| Sid | varchar (10) | YES | MUL |       NULL | |
| Cid | varchar (10) | YES | MUL |       NULL | |
| Score | Decimal (18,1) |     YES | |       NULL | |
+-------+---------------+------+-----+---------+-------+
3 rows in Set (0.48 sec)

Mysql> select * from SC;
+------+------+-------+
| Sid | Cid | Score |
+------+------+-------+
| 01 |  01 | 80.0 |
| 01 |  02 | 90.0 |
| 01 |  03 | 99.0 |
| 02 |  01 | 70.0 |
| 02 |  02 | 60.0 |
| 02 |  03 | 80.0 |
| 03 |  01 | 80.0 |
| 03 |  02 | 80.0 |
| 03 |  03 | 80.0 |
| 04 |  01 | 50.0 |
| 04 |  02 | 30.0 |
| 04 |  03 | 20.0 |
| 05 |  01 | 76.0 |
| 05 |  02 | 87.0 |
| 06 |  01 | 31.0 |
| 06 |  03 | 34.0 |
| 07 |  02 | 89.0 |
| 07 |  03 | 98.0 |
+------+------+-------+
Rows in Set (0.00 sec)


The above is my scoure table data

Count each student's total

Select Sid,sum (Score) from the SC group by SID;

To count the number of failed and passed subjects per student

Select Sid,sum (Score <60), SUM (score >=) from the SC group by SID;

Average scores of students with a statistical failing subject greater than 2

Select Sid,avg (Score) from SC where score < GROUP by SID have Count (*) >2;

Statistics of passing and failing numbers and passing rates in each section

Select Cid,sum (Score < 60) failed, sum (score >= 60) passed, sum (case when score >= 1 Else 0 end)/count (distinct s ID) pass rate from the SC group by CID;

This article from "Yun Weibang" blog, reproduced please contact the author!

MySQL Student Stats Exercise

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.