1. 程式人生 > >一道面試題:請寫sql查詢出,成績小於60的同學的姓名和平均分,並按平均分排序

一道面試題:請寫sql查詢出,成績小於60的同學的姓名和平均分,並按平均分排序

給出如下3張表,stu表、sc表和course表:

/*

Navicat MySQL Data Transfer

Source Server         : db_fightLandlor
Source Server Version : 50520
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50520
File Encoding         : 65001

Date: 2013-02-22 22:37:23
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `cs_id` int(6) NOT NULL AUTO_INCREMENT,
  `cs_name` varchar(20) DEFAULT NULL,
  `cs_desc` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`cs_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('1', '語文', 'xxxx');
INSERT INTO `course` VALUES ('2', '計算機導論', '考研必考課');
INSERT INTO `course` VALUES ('3', '軟體測試', '選修課');

-- ----------------------------
-- Table structure for `sc`
-- ----------------------------
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (
  `stu_id` int(6) DEFAULT NULL,
  `cs_id` int(6) DEFAULT '0',
  `score` int(6) DEFAULT NULL,
  KEY `xxxxx` (`stu_id`),
  KEY `xxxx` (`cs_id`),
  CONSTRAINT `xxxx` FOREIGN KEY (`cs_id`) REFERENCES `course` (`cs_id`),
  CONSTRAINT `xxxxx` FOREIGN KEY (`stu_id`) REFERENCES `stu` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records of sc
-- ----------------------------
INSERT INTO `sc` VALUES ('1', '1', '56');
INSERT INTO `sc` VALUES ('1', '1', '64');
INSERT INTO `sc` VALUES ('1', '2', '64');
INSERT INTO `sc` VALUES ('1', '3', '45');
INSERT INTO `sc` VALUES ('2', '3', '85');
INSERT INTO `sc` VALUES ('2', '2', '58');
INSERT INTO `sc` VALUES ('2', '1', '95');
INSERT INTO `sc` VALUES ('3', '3', '67');
INSERT INTO `sc` VALUES ('3', '2', '57');
INSERT INTO `sc` VALUES ('3', '1', '77');
INSERT INTO `sc` VALUES ('4', '1', '47');
INSERT INTO `sc` VALUES ('4', '2', '27');
INSERT INTO `sc` VALUES ('4', '3', '97');

-- ----------------------------
-- Table structure for `stu`
-- ----------------------------
DROP TABLE IF EXISTS `stu`;
CREATE TABLE `stu` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `nianji` varchar(10) DEFAULT NULL,
  `sex` varchar(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records of stu
-- ----------------------------
INSERT INTO `stu` VALUES ('1', '張三', '大二', '男');
INSERT INTO `stu` VALUES ('2', '王麗麗', '大三', '女');
INSERT INTO `stu` VALUES ('3', '王五', '大二', '女');
INSERT INTO `stu` VALUES ('4', '李四', '大一', '女');

請寫sql查詢出,成績小於60的同學的姓名和平均分,並按平均分排序


答案:select stu_id,score ,avg(score) ,name from sc,stu where score <60 and stu_id=id group by stu_id;

////////////////////////////////////////////////////////////////////////&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

desc是有小到大排列的

sql語句:select name as 姓名,avg(score) as 平均分 from sc,stu where score <60 and stu_id=id group by stu_id order by 平均分 asc;

結果: