不管是面试什么,数据库都是必考的,今天给大家整理下几个重要的sql查询。所有的语句都是在此语句上扩展的。学会了基本可以无忧了。话不多说,直接上干货:

– 一、学生表记录如下(学号 姓名 性别 年龄)
– 0001 xw 男 18
– 0002 mc 女 16
– 0003 ww 男 21
– 0004 xw 男 18
– 请写出实现如下功能的SQL语句
– 删除除了学号(自动编号)字段以外,其他字段都相同的冗(rong)余记录

方便windows命令行,sql语句中文支持

set character_set_client=gbk;
set character_set_results=gbk;create database day0509 charset utf8;
create table student(id int auto_increment primary key comment "主键自增",xuehao varchar(10) not null comment "学号",name  varchar(10) not null comment "姓名",gender varchar(10) not null comment "性别",age int not null comment "年龄"
);insert into student(xuehao,name,gender,age) values("0001", "xw", "男", 18),
("0002", "mc", "女", 16),
("0003", "ww", "男", 21),
("0004", "xw", "男", 18);

按照性别,对数据分组统计

 select gender,count(*) as gender_count from student group by gender;

查询出数据不重复的记录

select * from student group by name,gender,age;

查询出数据不重复的记录, id最小的

select min(id) as min_id from student group by name,gender,age;

删除1,子查询删除(效率低)

delete from student where id not in ( select min_id from (select min(id) as min_id from student group by name,gender,age) a);

删除2,中间临时表,删除冗余记录, (处理大数据量的时候)

create table tmp (select min(id) as id from student group by name,gender,age);
delete from student where id not in (select id from tmp);
drop table tmp;

、数据库有三个表 teacher表, student表, tea_stu关系表,
– teacher表 teaID name age ,
– student 表 stuID name age ,
– teacher_student表 teaID stuID

– 要求用一条SQL查询出这样的结果:
– 1.显示的字段要有老师 id age 每个老师所带的学生人数

– 2.只列出老师age为40 以下, 学生age为12以上的记录

create table teacher(tea_id int auto_increment primary key comment "主键自增",name  varchar(10) not null comment "姓名",age int not null comment "年龄"
);create table student(stu_id int auto_increment primary key comment "主键自增",name  varchar(10) not null comment "姓名",age int not null comment "年龄"
);

第一种,外键在建表之后添加

create table teacher_student(tea_id int comment "外键关联teacher表",stu_id int comment "外键关联teacher表"
);
ALTER TABLE `teacher_student` ADD CONSTRAINT `tea_id_fk` FOREIGN KEY (`tea_id`) REFERENCES `teacher` (`tea_id`);
ALTER TABLE `teacher_student` ADD CONSTRAINT `stu_id_fk` FOREIGN KEY (`stu_id`) REFERENCES `student` (`stu_id`);

第二种,外键在建表时创建:

create table teacher_student(tea_id int comment "外键关联teacher表",stu_id int comment "外键关联teacher表",KEY `tea_id_fk` (`tea_id`),KEY `stu_id_fk` (`stu_id`),CONSTRAINT `tea_id_fk` FOREIGN KEY (`tea_id`) REFERENCES `teacher` (`tea_id`),CONSTRAINT `stu_id_fk` FOREIGN KEY (`stu_id`) REFERENCES `student` (`stu_id`)
);

插入模拟数据

insert into teacher(tea_id, name,age) values(1,"刘慧",29),(2,"邵青莲",45), (3,"赵迎伟",28);
insert into student(stu_id, name,age) values(1,"王彦鑫",23),(2,"张栩",18),(3,"海玲",11),
(4,"单涛",20),(5,"贾雯",19),(6,"付亚军",21),
(7,"付小雷",24),(8,"石水伟",25),(9,"李娜",21);insert into teacher_student(tea_id, stu_id) values(1,1),(1,2), (1,3),
(2,7),(2,8), (2,9),
(3,4),(3,5), (3,6);

1.显示的字段要有老师 id age 每个老师所带的学生人数

select teacher.tea_id, teacher.name, teacher.age, count(teacher_student.stu_id) as stu_count from teacherleft join teacher_student  on teacher.tea_id = teacher_student.tea_idgroup by teacher_student.tea_id;

2.只列出老师age为40 以下, 学生age为12以上的记录

select teacher.tea_id, teacher.name, teacher.age, count(teacher_student.stu_id) as stu_count from teacherleft join teacher_student  on teacher.tea_id = teacher_student.tea_idleft join student on student.stu_id = teacher_student.stu_idwhere teacher.age<40 and student.age>12group by teacher_student.tea_id;

– 三、前提: a 部门表 b员工表
– a表字段(
– id – 部门编号
– departmentName – 部门名称
– )
– b表字段(
– id – 员工编号
– employee – 员工名称
– aid – 外键关联部门id
– )
– 问题: 如何一条SQL语句查询出每个部门共有多少人?

create table a(id int auto_increment primary key comment "主键自增",department_name  varchar(10) not null comment "部门名称"
);create table b(id int auto_increment primary key comment "主键自增",employee  varchar(10) not null comment "员工姓名",aid int not null comment "部门id"
);insert into a(id,department_name) values(1,"技术部"),(2,"运营"),(3,"产品部");
insert into b(id,employee,aid) values(1,"徐桐",3),(2,"徐秋杰",3),
(3,"高世一",1),(4,"方士源",1),(5,"徐航",1),
(6,"张栩",2);

如何一条SQL语句查询出每个部门共有多少人

select a.id as department_id, a.department_name, count(b.id) as employee_count from aleft join b on a.id = b.aidgroup by a.id;

– 四、有3张表, Student表, SC表和Course表
– Student表: 学号(Sno).姓名(Sname).性别(Ssex).年龄(Sage)和系名(Sdept)
– Course表:课程号(Cno).课程名(Cname)和学分(Ccredit)
– SC表:学号(Sno).课程号(Cno)和成绩(Grade)
– 请使用SQL语句查询学生姓名及其课程总学分
– (注:如果课程不及格, 那么此课程学分为0)

create table student(id int auto_increment primary key comment "主键自增",sno varchar(20) not null comment "学号",sname  varchar(10) not null comment "姓名",ssex  varchar(10) not null comment "性别",sage  int not null comment "年龄",sdept  varchar(10) not null comment "系名"
);create table course(id int auto_increment primary key comment "主键自增",cno varchar(20) not null comment "课程号",cname  varchar(10) not null comment "课程名",ccredit  int not null comment "学分"
);create table sc(id int auto_increment primary key comment "主键自增",sno varchar(20) not null comment "学号",cno varchar(20) not null comment "课程号",grade  int not null comment "成绩"
);insert into student(id,sno,sname,ssex,sage,sdept) values(1,"001","张栩","男",18,"计算机及应用"),
(2,"002","徐秋杰", "男", 24 , "计算机及应用"),
(3,"003","徐桐", "男", 24 , "物业管理");insert into course(id,cno,cname,ccredit) values(1,"02326","操作系统",4),
(2,"00015","英语二", 14),
(3,"02324","离散数学", 4);insert into sc(sno, cno, grade) values("001","02326",60),
("001","00015",59),
("001","02324",70),
("002","02326",75),
("002","00015",95),
("003","00015",80);

请使用SQL语句查询学生姓名及其课程总学分

select student.id,sname,sum(course.ccredit) as amount_ccredit from studentleft join sc on sc.sno = student.snoleft join course on sc.cno = course.cnowhere sc.grade >= 60group by student.id;

统计出每门课程 合格的学生数目

select course.cname, count(sc.sno) as amount_sno from courseleft join sc on course.cno = sc.cnowhere sc.grade >= 60group by sc.cno;

每门课程, 合格的学生名字

select sc.id,course.cname,student.sname from scleft join student on student.sno = sc.snoleft join course on course.cno = sc.cnowhere sc.grade >= 60order by sc.cno;

– 五、有3个表 S, C, SC
– S(SNO, SNAME) 代表(学号, 姓名)
– C(CNO, CNAME, CTEACHER) 代表(课号, 课名, 教师)
– SC(SNO, CNO, SCGRADE) 代表(学号, 课号, 成绩)
– 问题:
– 1.找出没选过”黎明”老师的所有学生姓名.
– 2.列出2门以上(含2门)不及格学生姓名及平均成绩.
– 3.既学过1号课程又学过2号课程所有学生的姓名.

create table s(id int auto_increment primary key comment "主键自增",sno varchar(20) not null comment "学号",sname  varchar(10) not null comment "姓名"
);create table c(id int auto_increment primary key comment "主键自增",cno varchar(20) not null comment "课程号",cname  varchar(10) not null comment "课程名",teacher  varchar(20) not null comment "教师"
);create table sc(id int auto_increment primary key comment "主键自增",sno varchar(20) not null comment "学号",cno varchar(20) not null comment "课程号",grade  int not null comment "成绩"
);insert into s(id, sno, sname) values(1,"001","张栩"),
(2,"002","徐秋杰"),
(3,"003","徐桐");
insert into s(id, sno, sname) values(4,"004","王彦鑫");
insert into s(id, sno, sname) values(5,"005","王严");insert into c(id,cno,cname,teacher) values(1,"02326","操作系统","刘慧"),
(2,"00015","英语二", "黎明"),
(3,"02324","离散数学", "史春廷");insert into sc(sno, cno, grade) values("001","02326",60),
("001","00015",59),
("001","02324",70),
("002","02326",75),
("002","00015",95),
("003","00015",80);insert into sc(sno, cno, grade) values("004","02324",80),
("004","02326",55);insert into sc(sno, cno, grade) values("005","02324",59),
("005","00015",59),
("005","02326",59);

1.找出没选过”黎明”老师的所有学生姓名.

001 找出黎明老师 所有带过的学生

select s.sname from scleft join s on s.sno = sc.snoleft join c on c.cno = sc.cnowhere c.teacher = "黎明";

002 子查询方式, 找出没选过”黎明”老师的所有学生姓名

select s.sname from s where s.sname not in (select s.sname from scleft join s on s.sno = sc.snoleft join c on c.cno = sc.cnowhere c.teacher = "黎明"
);

2,列出2门以上(含2门)不及格学生姓名及平均成绩.

select s.sname, avg(sc.grade) as avg_grade from sleft join sc on s.sno = sc.snogroup by s.snamehaving count(sc.grade<60 or null)>=2;

3.既学过1号课程又学过2号课程所有学生的姓名.

select s.sname from sleft join sc on s.sno = sc.snowhere sc.cno="02326" or sc.cno="00015"group by s.snamehaving count(sc.cno="02326" or null)=1 and count(sc.cno="00015" or null)=1;

面试经常考的五个Sql查询相关推荐

  1. MySQL数据库基础(五)——SQL查询

    MySQL数据库基础(五)--SQL查询 一.单表查询 1.查询所有字段 在SELECT语句中使用星号""通配符查询所有字段 在SELECT语句中指定所有字段 select fro ...

  2. JAVA面试常考系列五

    转载自 JAVA面试常考系列五 题目一 串行(serial)收集器和吞吐量(throughput)收集器的区别是什么? 吞吐量收集器使用并行版本的新生代垃圾收集器,它用于中等规模和大规模数据的应用程序 ...

  3. Hello MySQL(五)——SQL查询

    一.单表查询 1.查询所有字段 在SELECT语句中使用星号"*"通配符查询所有字段 在SELECT语句中指定所有字段 select * from `TStudent`; 2.查询 ...

  4. 五个 SQL 查询性能测试题,只有 40% 及格率,你敢来挑战吗?

    作者 | 董旭阳TonyDong,CSDN 博客专家 责编 | 唐小引 头图 | CSDN 下载自东方 IC 出品 | CSDN 博客 下面是 5 个关于索引和 SQL 查询性能的测试题:其中 4 个 ...

  5. oracle function 写查询语句_五个 SQL 查询性能测试题,只有 40% 及格率,你敢来挑战吗?...

    作者 | 董旭阳TonyDong,CSDN 博客专家 责编 | 唐小引 头图 | CSDN 下载自东方 IC 出品 | CSDN 博客 下面是 5 个关于索引和 SQL 查询性能的测试题:其中 4 个 ...

  6. 五个 SQL 查询性能测试题,只有 40% 及格率,你敢来挑战吗?| 原力计划

    作者 | 董旭阳TonyDong,CSDN 博客专家 责编 | 唐小引 头图 | CSDN 下载自东方 IC 出品 | CSDN 博客 下面是 5 个关于索引和 SQL 查询性能的测试题:其中 4 个 ...

  7. mysql及格率70以上_五个 SQL 查询性能测试题,只有 40% 及格率,你敢来挑战吗?| 原力计划...

    作者 | 董旭阳TonyDong,CSDN 博客专家 责编 | 唐小引 头图 | CSDN 下载自东方 IC 出品 | CSDN 博客 下面是 5 个关于索引和 SQL 查询性能的测试题:其中 4 个 ...

  8. SQL server 实验五 (sql 查询语句)

    实验五 实验知识储备: 一.当做一个查询题时 (1)首先要审题,明确你的查询对象 即select 后面的内容 (2)明确你的查询对象分别来自于哪一个表 即from 后面的内容 (3)找出查询条件/约束 ...

  9. 【面试必备】除了基本的增删改查外,面试官好问的那些看上去高级但又非常实用的Sql查询语句(以Mysql为例),涵盖视图和存储过程等众多知识点

    在平时的面试中,我们经常遇到面试官考我们的SQL功底,当然并不是考我们的基本的增删改查这些简单的SQL,而是那些我们不经常使用的,通常在你原来的公司你不怎么关注但在新公司却常用的语句.这种情况下,如果 ...

  10. 面试官:编写一个 SQL 查询,找出每个部门工资第二高的员工

    今天我们来看看大数据开发中row_number函数. 作为一名程序员,求职面试时时常会遇到需要编写一些基础的sql,编写sql这样做的目的主要是考验求职者的逻辑思维及编写sql基础能力.而row_nu ...

最新文章

  1. NBT:主流高通量测序仪在人/细菌/宏基因组测序评测结果发布,华大智造表现优异...
  2. Ubuntu下开启SSH服务
  3. wxWidgets:wxMenu/wxMenuBar 示例
  4. 我们团队设计的一个基于微服务的高并发服务器架构
  5. IE 10的新HTML​解析规则​
  6. css3鼠标悬停图片抖动效果
  7. linux 内核高端内存意义,Linux内核高端内存管理
  8. ORA-01113:文件n需要介质恢复 (转载)
  9. 【编译原理笔记07】语法分析:SLR、LR(1)、LALR、二义性分析与错误处理
  10. C 语言指针与汇编地址(一)
  11. (七)、Java异常类型及处理
  12. Google亲儿子 Nexus/Pixel 手机刷机Root之旅
  13. php换行替换,php怎么替换回车换行
  14. Landsat8处理小工具(python)
  15. mtk刷机/启动流程学习笔记
  16. python 通达信公式函数,python使用通达信公式,请人用python编写如下公式,我对编程一窍不通...
  17. AP计算机从绝望到满分-----深圳中学张嘉轩如何说
  18. 开机总是进行磁盘检查
  19. 如何建立起一套有效的APP监控体系
  20. 传感器自学笔记第八章——DHT11温湿度传感器(附带自己写的很好用的51单片机DHT11代码模板,参数列表只有一个字符数组指针)

热门文章

  1. 中国IT产业未来在哪里
  2. 外贸业务员询盘处理方法技巧 Google开发客户方法找网址和邮箱
  3. network attached storage 的简称,中文称为网络附加存储
  4. [生存志] 第26节 历代大事件概览 明朝
  5. 越狱相关三:OpenSSH
  6. 苦口婆心一考拉|向沉迷游戏的中(大)学生讲解内存和磁盘
  7. Excel 实现 平均数±标准差
  8. 修改Centos服务器主机名称
  9. 陆地生态系统生态学原理
  10. win10系统下计算器界面变成英文的解决方法