1、mysql下建表及插入数据

/*
Navicat MySQL Data Transfer Source Server : mysql
Source Server Version : 50640
Source Host : localhost:3306
Source Database : test Target Server Type : MYSQL
Target Server Version : 50640
File Encoding : 65001 Date: 2018-11-22 18:24:08
*/ SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for cource
-- ----------------------------
DROP TABLE IF EXISTS `cource`;
CREATE TABLE `cource` (
`cno` int(3) unsigned NOT NULL AUTO_INCREMENT COMMENT '课程编号',
`cname` varchar(20) DEFAULT NULL COMMENT '课程名称',
PRIMARY KEY (`cno`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='课程表'; -- ----------------------------
-- Records of cource
-- ----------------------------
INSERT INTO `cource` VALUES ('', '语文');
INSERT INTO `cource` VALUES ('', '数学');
INSERT INTO `cource` VALUES ('', '英语'); -- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sno` int(3) unsigned DEFAULT NULL COMMENT '学生编号',
`cno` int(3) unsigned DEFAULT NULL COMMENT '课程编号',
`score` int(3) unsigned DEFAULT NULL COMMENT '考试成绩'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生成绩表'; -- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES ('', '', '');
INSERT INTO `score` VALUES ('', '', '');
INSERT INTO `score` VALUES ('', '', ''); -- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sno` int(3) unsigned NOT NULL AUTO_INCREMENT COMMENT '学生编号',
`sname` varchar(20) DEFAULT NULL COMMENT '学生姓名',
`sage` int(3) unsigned DEFAULT NULL COMMENT '学生年龄',
PRIMARY KEY (`sno`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='学生表'; -- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('', '周杰伦', '');
INSERT INTO `student` VALUES ('', '周润发', '');
INSERT INTO `student` VALUES ('', '吴孟达', '');
INSERT INTO `student` VALUES ('', '刘德华', '');
INSERT INTO `student` VALUES ('', '李连杰', '');

2、查询语文成绩比数学成绩高的所有学生的编号
分析思路:
(1)在学生表上添加两个字段,分别为数学成绩字段和语文成绩字段
(2)把成绩表分成两个表,语文表和数学表
书写过程:
(1)语文表
select sno, score from score where cno = 1
(2)数学表
select sno, score from score where cno = 2
(3)结果
select sno, sname, score_chinese, score_math from
    (
        select s.sno, s.sname,  score_chinese from student s
        left join (select sno sno_chinese, score score_chinese from score where cno = 1) chinese
        on s.sno = chinese.sno_chinese
    ) s_chinese
left join (select sno sno_math, score score_math from score  where cno = 2) math
on s_chinese.sno = math.sno_math
where score_chinese > score_math


3、查询所有学生的学号、姓名、选课数、总成绩
分析思路:
(1)在学生表上添加两个字段,分别为选课数和总成绩
(2)把成绩表按学生编号分组,并计算出count(*)和sum(score)
书写过程:
(1)分组并统计
select sno, count(*), sum(score) from score group by sno
(2)结果
select s.sno, s.sname, select_count, select_sum from student s
left join (select sno sno_count_sum, count(*) select_count, sum(score) select_sum from score group by sno) count_sum
on s.sno = count_sum.sno_count_sum


4、查询没有学完所有课程的学生学号、姓名
分析思路:把成绩表按学生编号并count(*)
select sno, count(cno) cno_count from score group by sno having cno_count > 1

sql in interview for a job的更多相关文章

  1. SQL Server基础知识三十三问 (1-7)

    1. SQL Server运行在什么端口上? 可以被修改么? 答: 1433端口. 可以修改的, 在SQL Server Configuration Manager的SQL Server Networ ...

  2. SQL Interview Question

    面试的时候发现会问一些SQL的基本问题,在此总结一下. ProgramInterview/SQL 这个网站上的问题还比较全. 1. Join type INNER JOIN: Returns all ...

  3. the interview questions of sql server

    1.一道SQL语句面试题,关于group by 表内容: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-0 ...

  4. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  5. How to prevent SQL injection attacks?

    In our earlier tutorial on SQL Injection, one way to have prevented the SQL injection attack was by ...

  6. Google Interview University - 坚持完成这套学习手册,你就可以去 Google 面试了

    作者:Glowin链接:https://zhuanlan.zhihu.com/p/22881223来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文地址:Google ...

  7. UNIX command Questions Answers asked in Interview

    UNIX or Linux operating system has become default Server operating system and for whichever programm ...

  8. Core Java Interview Question Answer

    This is a new series of sharing core Java interview question and answer on Finance domain and mostly ...

  9. 115 Java Interview Questions and Answers – The ULTIMATE List--reference

    In this tutorial we will discuss about different types of questions that can be used in a Java inter ...

随机推荐

  1. android动态权限获取

    android动态权限获取 Android6.0采用新的权限模型,只有在需要权限的时候,才告知用户是否授权,是在runtime时候授权,而不是在原来安装的时候 ,同时默认情况下每次在运行时打开页面时候 ...

  2. python中range、xrange和randrange的区别

    range 函数说明:range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个列表. xrange 函数说明:和range 的用法完 ...

  3. Android 开发工具类 01_AppUtils

    1.获取应用程序名称: 2.获取应用程序版本信息. import android.content.Context; import android.content.pm.PackageInfo; imp ...

  4. Check类中的incl、union,excl,diff,intersect

    定义一些类,这些类之间有父子关系,如下: class Father{} class Son1 extends Father{} class Son2 extends Father{} class To ...

  5. Linux的MySQL不能远程访问

    1.首先,你要确认用户是否只允许localhost访问: 在linux下登录mysql mysql -uroot -p密码;     use mysql;     select `host`,`use ...

  6. Linux的用户(组),权限,文件精妙的三角关系,和强大的帮助系统

    在linux中一切都是文件(文件夹和硬件外设是特殊的文件),如果有可能尽量使用文本文件.文本文件是人和机器能理解的文件,也成为人和机器进行 交流的最好途径.由于所有的配置文件都是文本,所以你只需要一个 ...

  7. activemq的三种通信方式

    一.安装与启动 1.下载安装activemq,下载地址:http://activemq.apache.org/download.html. 2.安装完成后,进入其所在目录的bin目录下面,根据系统位数 ...

  8. window.location.replace

    有3个页面 a,b,c 如果当前页面是c页面,并且c页面是这样跳转过来的:a->b->c 1:b->c 是通过window.location.replace("..xx/c ...

  9. golang基础--func函数

    函数function Go函数不支持 嵌套, 重载和默认参数 支持以下特性: 无须声明原型,不定长度长度变参,多返回值,命名返回值参数,匿名函数,闭包 定义函数使用关键字func,且左侧大括号不能另起 ...

  10. ruby + phantomjs 自动化测试 - GA

    说起测试GA,真是一件枯燥乏味,重复性很高的工作,那么为什么我们不使用自动化测试代替它呢,显然,很多公司的产品迭代太快,ga也变化的比较频繁,但是确保ga工作正常,对于其他部门的工作是有很大帮助的,由 ...