PTA Query Course Score Highest Duo Score 10

This topic requires writing SQL statements,

Retrieve the student number, name and grade of the two students with the highest grades in course C002

Table Structure:

Please write the SQL statement that defines the table structure here. For example:

CREATE TABLE `stu` (
  `sno` char(4) NOT NULL,
  `sname` char(8) NOT NULL,
  `sex` tinyint(1) DEFAULT NULL,
  `mno` char(2) DEFAULT NULL,
  `birdate` datetime DEFAULT NULL,
  `memo` text,
  PRIMARY KEY (`sno`),
);

CREATE TABLE `sc` (
  `sno` char(4) NOT NULL,
  `cno` char(4) NOT NULL,
  `grade` decimal(6,1) DEFAULT NULL,
  PRIMARY KEY (`sno`,`cno`),
  CONSTRAINT `fk_sc_sno` FOREIGN KEY (`sno`) REFERENCES `stu` (`sno`)
);

table sample

Please give a table sample corresponding to the above table structure here. For example

Stu table:

sc table:

Sample output:

Please give a sample output here. For example:

Code length limit 16 KB

Time limit 400 ms

DatabaseMySQL

The result output requires a strict comparison of the order and data

select stu.sno,stu.sname,sc.grade from stu 
join sc on
stu.sno=sc.sno
and sc.cno='C002'
group by sno
order by grade desc
limit 2

Guess you like

Origin blog.csdn.net/weixin_70206677/article/details/129360330