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. Numpy 数组和dtype的一个使用误区

    首先自定义三种类型(如下代码1-3行),第一行使用scalar type,第2,3行使用Structured type. 提出问题:第5,7行同为创建数组,为什么第5行能work,而第7行会raise ...

  2. Django中url的反向查询

    明确几个概念: application namespace:   正在部署的app的名称,一个app的多个实例应该具有相同的application namespace.   可以通过在URLconf模 ...

  3. Java之集合(二十七)其它集合

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7551368.html 1.前言 本章介绍剩余的3个集合类:ConcurrentSkipListSet.CopyO ...

  4. mapreduce基本原理

    场景: 一个大小为100T的文件,统计单词"ERROR"和"INFO"的个数 普通做法 是不是效率太低了? 换个方式 说明: 把100T文件分成100份,一台机 ...

  5. 【Java并发编程】:守护线程与线程阻塞的四种情况

    守护线程 JAVA中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 用户线程即运行在前台的线程,而守护线程是运行在后台的线程. 守护线程作用是为其他前台线程的运 ...

  6. 【数组】4Sum

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  7. Hadoop HDFS概念学习系列之HDFS升级和回滚机制(十二)

    不多说,直接上干货! HDFS升级和回滚机制 作为一个大型的分布式系统,Hadoop内部实现了一套升级机制,当在一个集群上升级Hadoop时,像其他的软件升级一样,可能会有新的bug或一些会影响现有应 ...

  8. NoSQL之Redis入门笔记

    Redis 1.Redis介绍 1.1 NoSQL:一类新出现的数据库(not only sql),它的特点 不支持sql语法 存储结构跟传统关系型数据库中的那种关系表完全不同,nosql中存储的数据 ...

  9. Fiddler——PC上实现手机的抓包(转载 http://www.jianshu.com/p/13f8a81d7c7c)

    Fiddler是15年初,在千牛中做超级促销插件时,发现没有root的Android机和没有越狱的iPhone无法修改host,因此没办法测试.为了让我这个磨人的PD也能看到,开发推荐了Fiddler ...

  10. [转]验证发生前无法调用 Page.IsValid。应在 CausesValidation=True 且已启动回发的控件

    在ASP.Net中,为了方便表单的验证,提供了验证控件来完成表单输入数据的验证.这些验证控件确实是功能强大,为写表单程序提供了极大的便利.但是,在不熟悉的情况下,经常碰到问题.其中,最常见的是遇到错误 ...