Sql语句练习总结查询语句_检索有一门或一门以上课程大于40-程序员宅基地

技术标签: 数据库  hibernate  sql  

练习一

现有关系数据库表如下:

学生表:学号 char(6) 姓名 性别 身份证号
课程表:课号 char(6) 名称
成绩表:id,学号,课号,分数
用sql实现如下两道题:
1.检索姓马的女同学情况(姓名,身份证号)
2.检索一门或者一门以上课程成绩大于等于90的所有学生信息(学号,姓名)

建表语句如下:

create table stu(
stuNo char(6),
name varchar(20),
gender varchar(4),
idcard varchar(18)
);

insert  into stu(stuno,name,gender,idcard) values('100001','马霞','女','232301198006013421');
insert  into stu(stuno,name,gender,idcard) values('100002','马立军','男','232301198006013422');
insert  into stu(stuno,name,gender,idcard) values('100003','黄蓉','女','232301198006013423');

create  table course(
cno varchar(6),
cname varchar(20)
);
insert into course(cno,cname) values('1','语文');
insert into course(cno,cname) values('2','数学');
insert into course(cno,cname) values('3','英语');

create table score(
id int primary key auto_increment,
stuno char(6)  references stu(stuno),
cno char(6)    references course(cno),
score numeric
);

insert into score(stuno,cno,score) values('100001','1',80);
insert into score(stuno,cno,score) values('100001','2',80);
insert into score(stuno,cno,score) values('100001','3',95);

insert into score(stuno,cno,score) values('100002','1',70);
insert into score(stuno,cno,score) values('100002','2',80);
insert into score(stuno,cno,score) values('100002','3',85);

insert into score(stuno,cno,score) values('100003','1',60);
insert into score(stuno,cno,score) values('100003','2',70);
insert into score(stuno,cno,score) values('100003','3',80);

答案

-- 1.检索姓马的女同学情况(姓名,身份证号)
use practice
select name, idcard from stu where name like "马%" and gender = "女"
-- 2.检索一门或者一门以上课程成绩大于等于90的所有学生信息(学号,姓名) 
select a.stuno, b.name from score a, stu b 
where a.stuno=b.stuno
and a.score >= 90
--或者使用关联查询
select stu.stuno, stu.name, course.cname, score.score
from stu join score on stu.stuno=score.stuno
join course on score.cno=course.cno
where score.score>=90

练习二

题目

有三张表: 学生表student , 课程Coures 学生课程表 sc, 学生可以选修多门课程,一门课程可以被多个学生选修,通过sc表关联, 详细要求如下:
1.写出建表语句:
2.写出SQL语句 查询选修了所有课程的学生
3.写出SQL语言 查询选修了至少2门以上的课程的学生

语句

create database tt default char set utf8;

create table student (
id int(10) primary key,
name varchar(20)
);

create table course(
     id int(10) primary key,
     name varchar(20)
);

create table sc(
      sid int(10) references student(id),
             cid  int(10) references  course(id),
            grade  int(3)
);


插入如下记录:
insert into student values(1,'feifei');
insert into student values(2,'jingjing');
insert into student values(3,'nannan');
insert into student values(4,'yuanyuan');
insert into student values(5,'jiejie');

insert into course  values(1,'corejava');
insert into course  values(2,'c++');
insert into course  values(3,'jdbc');
insert into course  values(4,'hibernate');

insert into sc  values(1,1,98);
insert into sc  values(2,1,97);
insert into sc  values(3,1,94);
insert into sc  values(4,1,92);
insert into sc  values(5,1,93);

insert into sc  values(1,2,94);
insert into sc  values(2,2,92);
insert into sc  values(3,2,95);
insert into sc  values(5,2,97);

insert into sc  values(1,3,92);
insert into sc  values(2,3,92);
insert into sc  values(4,3,91);

insert into sc  values(1,4,99);
insert into sc  values(3,4,89);

答案

#写出SQL语句 查询选修了所有课程的学生

select name from student where id in 
(select sid from sc group by sid having count(*) = (select count(*) from course))

#写出SQL语言 查询选修了至少2门以上的课程的学生
select name from student where id in 
(select sid from sc group by sid having count(*) >= 2)

练习三

题目

两张表 student和score;
student:学号,姓名,性别,年龄;
score:学号,语文,数学,英语

1.查询tom的学号,姓名,性别,语文,数学,英语
2.查询语文比数学好的同学
3.查询姓名相同的学生学号

建表语句如下:

create table student (
    stuno int(8) primary key,
    sname varchar(12),
    sex varchar(2) default '男',
    age int
);

create table  score(
stuno int(8),
chinese int(3),
math int(3),
english int(3)
);

insert into student(stuno,sname,sex,age)
values(1,'tom','男',22);

insert into student(stuno,sname,sex,age)
values(2,'terry','男',22);

insert into student(stuno,sname,sex,age)
values(3,'marry','女',23);

insert into student(stuno,sname,sex,age)
values(4,'marry','女',23);


insert into score(stuno,chinese,english,math)
values(1,70,80,90);

insert into score(stuno,chinese,english,math)
values(2,60,80,70);

insert into score(stuno,chinese,english,math)
values(3,70,85,95);

insert into score(stuno,chinese,english,math)
values(4,98,85,95);

答案

-- 1.查询tom的学号,姓名,性别,语文,数学,英语

select st.stuno, st.sname, st.sex, st.age, sc.chinese, sc.math, sc.english
from student st join score sc 
on st.stuno=sc.stuno
where st.sname='tom'

-- 2.查询语文比数学好的同学

select st.stuno, st.sname,sc.chinese,sc.math
from student st join score sc
on st.stuno=sc.stuno
where sc.chinese>sc.math


-- 3.查询姓名相同的学生学号
select stuno, sname
from student group by sname
having count(*)>1

select stuno, sname 
from student 
where sname in(select sname
from student group by sname
having count(*)>1)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_58818711/article/details/120039910

智能推荐

阿里云服务器CentOs中安装MySQL-5.7_aliyun centos安装mysql5.7-程序员宅基地

文章浏览阅读1.7k次,点赞4次,收藏4次。提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结前言提示:这里可以添加本文要记录的大概内容:例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。提示:以下是本篇文章正文内容,下面案例可供参考一、pandas是什么?示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。二、使用步骤1.引入库代码_aliyun centos安装mysql5.7

斗地主——找出顺子的算法-程序员宅基地

文章浏览阅读2k次。1 static List<crads> shuen(List<crads> list) 2 { 3 List<crads> cList = new List<crads>(); 4 for (int i = 0; i < list.Count;++i) ...

Thinkpad T460P安装win7_t460p支持win7吗-程序员宅基地

文章浏览阅读2.1w次。Thinkpad T460P安装win7 Skylake平台装Windows7_t460p支持win7吗

百事可乐⋅顺风车⋅迷路⋅出租车_百事顺风车-程序员宅基地

文章浏览阅读1.4k次。亲身经历的四段小事,早想写出来,一直拖到现在。今日做个小记,待日后慢慢品味。百事可乐 09年年初初来京时,居住在一个偏僻的村子里,村子虽小,日常生活都可以在这里解决。当时收入不多,为了省钱,每天早晨都会去一家简陋的小馆子里吃“炸油饼”。去的多了,渐渐的就与馆子的老板_百事顺风车

Android请求网络数据,json解析-FastJson遇到的问题 首字母大写问题_android网络请求大小写问题-程序员宅基地

文章浏览阅读842次。在Android app开发过程中,用fastjson获取后台数据,后台返回的数据:[{"doseFrequencyList":[{"FrequencyCode":"ed","name":"每天","ordinal":"1"},{"FrequencyCode":"iod","name":"隔天","ordinal":"2"},{"FrequencyCode":"iow","name":"隔周","_android网络请求大小写问题

cheerio制作markDown索引目录_toc-wrapper-程序员宅基地

文章浏览阅读235次。原文链接:Bougie的博客 制作目录索引这种东西当然是放在前端方便。选择放在后端一是为了了解Node后端生态,掌握更多后端技术;二是因为公司实行前后端分离的方式开发,睾贵的JAVA后端经常啥也不做处理就返回一个row数据(甚至有时时间戳都不处理),对此有些无语。最终目标 1. 点击索引单项跳转到相应标题 2. 大号标题包含小号标题,小号标题向右缩进 3. 滚动页面时自..._toc-wrapper

随便推点

mac上安装xcode老版本_xcode老版本安装-程序员宅基地

文章浏览阅读2k次。有的mac版本低无法安装最新版Xcode,此介绍安装老版Xcode1.首先打开连接 https://developer.apple.com/download/more/ 进入页面2.在搜索框中输入 xcode 回车搜索,如下图所示:3.等待下载安装..._xcode老版本安装

山东春考计算机本科学校分数线,2016年山东春季高考各校计算机专业录取分数线分别是多少?...-程序员宅基地

文章浏览阅读1.4k次。2016年山东春季高考各校计算机专业录取分数线分别是多少?以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!2016年山东春季高考各校计算机专业录取分数线分别是多少?2016年山东春季高考各校计算机专业录取分数线分别是多少?1、2016年全国各省份高考成绩及各批次控制分数线公布时间都集中在6月23-26日之间,预测的分数线..._山东滨州学院计算机专业春考本科分数线

Asp.net 2.0自定义控件(点击HyperLink后执行事件)[网友问题: DataList里HyperLink控件激发事件,在哪定义?]...-程序员宅基地

文章浏览阅读151次。(一). 概述HyperLink默认没有Click事件, 重写了一个HyperLink自定义控件. 实现原理: 默认Hyperlink是跳到点击请求的页面, 本HyperLink自定义控件最终也是跳转到请求的页面, 但期间执行了自己的一个方法, 我们可以在此方法中添写自己所需的功能. 本示例演示统计此超链接点击次数功能. [参考Asp.net 2.0高级编程](二). 代..._hyperlink控件后台点击方法

HBASE 启动报错 Can't get connection to ZooKeeper: KeeperErrorCode = ConnectionLoss for /hbase-程序员宅基地

文章浏览阅读9k次,点赞3次,收藏6次。查看防火墙状态$ service iptables status关闭防火墙$ service iptables stop查看防火墙状态$ service iptables status停止hbase$ stop-hbase.sh启动hbase$ start-hbase.sh_can't get connection to zookeeper: keepererrorcode = connectionloss for /hba

华为智慧屏鸿蒙系统手工升级,华为的“中场战事”:升级智能家居、推鸿蒙智慧屏,重构IoT赛道?...-程序员宅基地

文章浏览阅读324次。进一步切入全屋智能、大屏、车机等全场景。2020年,华为消费者业务的产品线纵深正进一步拓展。12月21日,华为面向家庭、出行场景正式发布了三大系列产品。其一是华为智能家居战略及全屋智能解决方案,顾名思义,是提升家居生活智能化的软硬件体系;其二是华为智慧屏S系列,搭载了鸿蒙OS最新版本,该系列是华为智慧屏家族的新成员,产品定位中低端市场,拥有55、65、75寸三种屏幕尺寸共6款机型;其三是车载智慧屏...

CMenu类中禁用/变灰某一项-程序员宅基地

文章浏览阅读322次。CMenu::EnableMenuItem启用、 禁用,或变暗的菜单项。UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable);参数nIDEnableItem根据所指定的菜单项,若要启用,nEnable。 弹出菜单项,以及标准菜单项,可以指定此参数。nEnable指定要执行的操作。 它可以是组合的M..._cmenu 菜单项置灰

推荐文章

热门文章

相关标签