1. 矫正数据,有以下2个表,建表语句如下所示

    -- 订单表
    create table t_order
    (
    id int auto_increment
    primary key,
    name varchar(255) null,
    total int null
    );
    -- 插入数据
    insert into sql_test.t_order (id, name, total) values (1, '家电', 1300);
    insert into sql_test.t_order (id, name, total) values (2, '洗漱', 170);
    insert into sql_test.t_order (id, name, total) values (3, '餐饮', 200); -- 详情表
    create table t_detail
    (
    id int auto_increment
    primary key,
    detail varchar(255) null,
    cost int null,
    order_id int null
    );
    -- 插入数据
    insert into sql_test.t_detail (id, detail, cost, order_id) values (1, '洗衣机', 500, 1);
    insert into sql_test.t_detail (id, detail, cost, order_id) values (2, '电视机', 800, 1);
    insert into sql_test.t_detail (id, detail, cost, order_id) values (3, '牙膏', 100, 2);
    insert into sql_test.t_detail (id, detail, cost, order_id) values (4, '洗衣液', 70, 2);
    insert into sql_test.t_detail (id, detail, cost, order_id) values (5, '白菜', 200, 3);

    由于故障导致t_order表中的total值出现异常,使用一个sql语句进行矫正;

    update t_order o,
    (select order_id as oid, sum(cost) as t from t_detail GROUP BY order_id) b
    set o.total = b.t
    where o.id = b.oid;
  2. 分类求和题,有表如下

    create table t_type
    (
    id int auto_increment
    primary key,
    type int null,
    num int null
    );
    -- 插入数据
    insert into sql_test.t_type (id, type, num) values (1, 1, 100);
    insert into sql_test.t_type (id, type, num) values (2, 1, 200);
    insert into sql_test.t_type (id, type, num) values (3, 2, 500);
    insert into sql_test.t_type (id, type, num) values (4, 2, 200);
    insert into sql_test.t_type (id, type, num) values (5, 3, 300);
    insert into sql_test.t_type (id, type, num) values (6, 3, 180);
    insert into sql_test.t_type (id, type, num) values (7, 4, 50);
    insert into sql_test.t_type (id, type, num) values (8, 5, 60);
    insert into sql_test.t_type (id, type, num) values (9, 6, 70);

    要求:当type>3type=8,并且分类求和,要达到的效果如下:

    type sum
    1 300
    2 700
    3 480
    8 180

    实现sql语句,需要使用到case when xxx then xxx else xxx end语句:

    select case when type > 3 then 8 else type end as t, sum(case when type > 3 then num else num end)
    from t_type
    group by t;
  3. 学生成绩相关

    -- 学生表
    create table student(
    id int unsigned primary key auto_increment,
    name char(10) not null
    );
    insert into student(name) values('张三'),('李四');
    -- 课程表
    create table course(
    id int unsigned primary key auto_increment,
    name char(20) not null
    );
    insert into course(name) values('语文'),('数学');
    -- 学生成绩表
    create table student_course(
    sid int unsigned,
    cid int unsigned,
    score int unsigned not null,
    foreign key (sid) references student(id),
    foreign key (cid) references course(id),
    primary key(sid, cid)
    );
    insert into student_course values(1,1,80),(1,2,90),(2,1,90),(2,2,70);
    1. 查询重名的学生,按照name,id升序

      select id,name from student  where name in  (select name c from student group by name HAVING count(name) > 1)  order by name,id;
      
      -- exits写法
      select t.id,t.name from student t where EXISTS (select s.name from student s where s.name = t.name GROUP BY name HAVING count(s.name) > 1 ) order by t.name,t.id;
    2. 在student_course表中查询平均分不及格的学生,列出学生id和平均分

      select sid, AVG(score) as a from student_course GROUP BY sid HAVING a < 60;
    3. 在student_course表中查询每门课成绩都不低于80的学生id

      select DISTINCT sid from student_course where sid not in (select sid from student_course where score < 80);
    4. 查询每个学生的总成绩,结果列出学生姓名和总成绩

      select s.name, sum(c.score) from student_course c, student s  where c.sid = s.id GROUP BY sid;
      -- 上述方法会过滤掉没有成绩的人,因此需要使用左连接
      select name,sum(score)
      from student left join student_course
      on student.id=student_course.sid
      group by sid;
    5. 总成绩最高的学生,结果列出学生id和总成绩

      select sid, sum(score) as ss from student_course GROUP BY sid order by ss desc limit 1;
    6. 在student_course表查询课程1成绩第2高的学生,如果第2高的不止一个则列出所有的学生

      select * from student_coursewhere cid=1 and score = (
      select score from student_course where cid = 1 group by score order by score desc limit 1,1
      );
    7. 在student_course表查询各科成绩最高的学生,结果列出学生id、课程id和对应的成绩

      select * from student_course as x where score>=
      (select max(score) from student_course as y where cid=x.cid);
    8. 在student_course表中查询每门课的前2名,结果按课程id升序,同一课程按成绩降序

      select * from student_course x where
      2>(select count(distinct(score)) from student_course y where y.cid=x.cid and y.score>x.score)
      order by cid,score desc;
    9. 一个叫team的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球队,两两进行比赛,用一条sql语句显示所有可能的比赛组合

      select a.name, b.name
      from team a, team b
      where a.name < b.name
    10. 竖变横

      -- 年	季度	销售
      -- 1991 1 11
      -- 1991 2 12
      -- 1991 3 13
      -- 1991 4 14
      -- 1992 1 21
      -- 1992 2 22
      -- 1992 3 23
      -- 1992 4 24
      -- 查询结果
      -- 年 一季度 二季度 三季度 四季度
      -- 1991 11 12 13 14
      -- 1992 21 22 23 24 select 年,
      sum(case when 季度=1 then 销售量 else 0 end) as 一季度,
      sum(case when 季度=2 then 销售量 else 0 end) as 二季度,
      sum(case when 季度=3 then 销售量 else 0 end) as 三季度,
      sum(case when 季度=4 then 销售量 else 0 end) as 四季度
      from sales group by 年;

面试题四:手写sql的更多相关文章

  1. SpringBoot项目里,让TKmybatis支持可以手写sql的Mapper.xml文件

    SpringBoot项目通常配合TKMybatis或MyBatis-Plus来做数据的持久化. 对于单表的增删改查,TKMybatis优雅简洁,无需像传统mybatis那样在mapper.xml文件里 ...

  2. Hive手写SQL案例

    1-请详细描述将一个有结构的文本文件student.txt导入到一个hive表中的步骤,及其关键字 假设student.txt 有以下几列:id,name,gender三列 1-创建数据库 creat ...

  3. 面试题|手写JSON解析器

    这周的 Cassidoo 的每周简讯有这么一个面试题:: 写一个函数,这个函数接收一个正确的 JSON 字符串并将其转化为一个对象(或字典,映射等,这取决于你选择的语言).示例输入: fakePars ...

  4. 前端面试题整理——手写AJAX

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 前端面试题之手写promise

    前端面试题之Promise问题 前言 在我们日常开发中会遇到很多异步的情况,比如涉及到 网络请求(ajax,axios等),定时器这些,对于这些异步操作我们如果需要拿到他们操作后的结果,就需要使用到回 ...

  6. hibernate使用手写sql以及对结果list的处理

    Session sees=simpleDAO.getSessionFactory().openSession(); String sql = "select * from fhcb_08_t ...

  7. js面试题之手写节流函数和防抖函数

    函数节流:不断触发一个函数后,执行第一次,只有大于设定的执行周期后才会执行第二次 /* 节流函数:fn:要被节流的函数,delay:规定的时间 */ function throttle(fn,dela ...

  8. IDEA中mybatis插件自动生成手写sql的xml文件

    上图: 选择这个安装,然后重启IDEA,ok.

  9. 前端面试题整理——手写简易jquery

    class jQuery { constructor(selector) { const result = document.querySelectorAll(selector) console.lo ...

随机推荐

  1. jmeter while控制器踩坑

    在使用jmeter测试一个复杂流程,需要用到while控制器,对于里面的contition,使用jexl3方式,怎么看都没有问题,可是死活跳不出流程,各种莫名其妙的报错,甚至用单个写死的变量(已设置为 ...

  2. Java 运行 Jar 包中java -cp 与 java jar 的区别

    java -cp java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库和jar包,需要全路径到jar包,多个jar包之间连接符:window上分号"; ...

  3. STM32电路设计注意

    以后画STM32的电路板 VDDA一定要接 张JF说  VDDA是给内部的时钟电路供电的 还有一定要留 串口或者  下载调试口(串口) 或者显示器接口 输入输出设备最好都留着 这样才能方便调试

  4. GPIO端口上拉下拉 与 硬件图的上拉下拉

    硬件图上的上拉下拉: 没有触发时默认接到IO的是高电平就是上拉: 没有触发时默认接到IO的是低电平就是 下拉: (2)对应GPIO的配置 配置与你的外围电路息息相关: 比如下图: 你只能配置为上拉: ...

  5. JavaScript正则表达式(深度)(Day_14)

    忘不掉的是回忆,继续的是生活,错过的,就当是路过. 简介 正则表达式是用于匹配字符串中字符组合的模式.在 JavaScript中,正则表达式也是对象. 这些模式被用于 RegExp 的 exec 和  ...

  6. C#异常处理18条最佳实践

    首先,异常处理应该是系统设计规约的一部分出现在系统设计文档中,而不仅仅是一种技术实现. 作为设计文档的一部分,异常处理应该着眼于系统容错性和稳定性(正如楼主提到的那样).然后在根据这个规约,再来具体讨 ...

  7. 项目中添加lib依赖

    Project Structure-->Artifacts

  8. webdriver中的等待——主要讲解WebDriverWait()

    webdriver中的等待--主要讲解WebDriverWait() 强制等待:sleep() 隐式等待:implicitly_wait() 显示等待:WebDriverWait() 与until() ...

  9. Django学习之完成数据库主从复制、读写分离和一主多从情况下的使用办法

    1.首先配置多个数据库,在settings配置文件中配置以下内容: DATABASES = { 'default': { #默认数据库,配置多个mysql数据也是ok的,混用数据库也是ok的 'ENG ...

  10. Python+Selenium - windows安全中心的弹窗(账号登录)

    当出现如下图所示的 Windows安全中心弹窗,需要输入用户名和密码时 如何用Python+selenium跳过这个登录. 步骤: 1.在注册表中三个位置各添加两个东西:iexplore.exe 和 ...