数据库表联合查询
上一篇 /
下一篇 2014-12-24 14:05:28
/ 个人分类:mysql
1. 等值连接 inner join, 左连接:left join, 右连接:right join
数据库查询练习题:
A有三个字段Code、Name、Age、其中Code为主键;
B有三个字段Code、Class、Score,其中Code
+ Class为主键。两表记录如下:
CodeName Age Code Class Score
97001张三22 97001数学80
97002赵四21 97002计算机59
97003张飞20 97003计算机60
97004李五22 97004数学55
1、请写出SQL,找出所有姓张的学生,并按年龄从小到大排列;
select * from TableX where
name like '张%' order by age 降序是desc
1. 通过等值联接,取出Name、Class、Score,请写出SQL即输出结果
select A.name, B.class, B.score from A, B where A.code =B.code
NOTE: 等值连接inner join一般不写出来,条件查询时不写出来
2. 通过外联接,取出每个学生的Name、Class、Score、请写SQL输出结果
select A.name, B.cleass,B.score from A left join BonA.code =B.code;
update tablesetxxx where xxx;
insert into table values(xxx,xxxx,xxx);
5、请写SQL,在TableX表中增加一条学生记录(学号:97005姓名:赵六年龄:20);
insert into tablex
values('97005','赵六',20)
6、李五的年龄记录错了,应该是21,请写SQL,根据主键进行更新;
update tablex set age=21
where code='97004'
7、请写SQL,删除TableX中没有考试成绩的学生记录,请使用not
in条件;
delete fromtablex where code not in (select code from tabley)
三.骑虎
9、图书(图书号,图书名,作者编号,出版社,出版日期) 作者(作者姓名,作者编号,年龄,性别) 用SQL语句查询年龄小于平均年龄的作者姓名、图书名,出版社。
select 作者姓名,图书名,出版社 from 图书,作者 where 图书.作者编号=作者.作者编号 and 年龄< (select avg(年龄) from 作者)
四.喜安科 面试题
13、数据库: student表(Sno,Sname,Sage,Ssex) course表(Cno,Cname,Ccredit) SC表 (Sno,Cno,grade)(1)建表student、course、SC (2)查询年龄小于20的学生学号、年龄,并显示(3)查询年龄不在20-23(包括20 、23 )的学生的成绩 create table student ( Sno int, Sname char(20), Sage int, Ssex bit, primary key(Sno) ) Create table course ( Cno int, Cname char(20), Ccredit int, primary key(Cno) ) create table SC ( Sno int, Cno int, grade float,primary key(Sno,Cno), foreign key(Sno) references Student(sno), foreign key(Cno) regerences course(Cno))
2. select Sno,Sage from Student where Sage<20 3. select grade from SC where Sno not in ( select Sno from Student where Sagebetween 20 and 23)
http://wenku.baidu.com/view/9ac037c42cc58bd63186bdaf.html
合计函数 (比如 SUM) 常常需要添加GROUP BY语句。
HAVING 子句
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。
http://tech.sina.com.cn/s/2008-07-09/1127725761.shtml
收藏
举报
TAG: