当前位置:文档之家› 尚硅谷_宋红康_sql面试题

尚硅谷_宋红康_sql面试题

尚硅谷_宋红康_sql面试题
尚硅谷_宋红康_sql面试题

sql数据库基础面试题复习试题考试题_全

不定项选择题(针对以下题目,请选择最符合题目要求的答案,每道题有一项或二项正确答案。针对每一道题目,所有答案都选对,则该题得分,所选答案错误或不能选出所有答案,则该题不得分。题量为50道,每题2分,总分为100分。) 第一章 1、是SQLServer数据库的主数据文件的扩展名。(选择一项) A、.sql B、.mdb C、.ldf D、.mdf 2、在SQL Server 2005中,有系统数据库和用户数据库,下列不属于系统数据库的是()。 (选择一项) A、master B、pubs C、model D、msdb 3、当安装完SQL Server2005数据库时,系统默认当前的超级管理员是( ) (选择一项) A、sa B、master C、administrator D、super 4、在使用SQL Server2005数据库时,有时需要将本机的数据库移动到其他机器上,恢复成对应的数据库使用。移动数据库分两步进行,应包括()和附加数据库(选择一项)A、分离数据库 B、删除数据库 C、新建数据库 D、合并数据库 5、在SQL Server2005中,附加数据库操作是指()(选择一项) A、把SQL Server 数据库文件保存为其他数据文件 B、根据数据库物理文件中的信息,把数据库在SQL Server 2005中恢复 C、把所有该数据库表的数据清空 D、把数据库删除掉 6、某单位由不同的部门组成,不同的部门每天都会生产一些报告、报表等数据,以为都采用纸张的形式来进行数据的保存和分类,随着业务的发展,这些数据越来越多,管理这些报告越来越费力,此时应考虑()(选择一项) A、由多个人来完成这些工作 B、在不同的部门中,由专门的人员去管理这些数据 C、采用数据库系统来管理这些数据 D、把这些数据统一成一样的格式 7、在SQL Server 2005中,对于数据库的定义正确的是()(选择一项) A、数据库是用来描述事物的符号记录 B、数据库是位于用户与操作系统之间的一层数据管理软件

Sql常见面试题 受用了

Sql常见面试题受用了 1. 用一条SQL 语句查询出每门课都大于80 分的学生姓名 name kecheng fenshu 张三语文81 张三数学75 李四语文76 李四数学90 王五语文81 王五数学100 王五英语90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80 2. 学生表如下: 自动编号学号姓名课程编号课程名称分数 1 2005001 张三0001 数学69 2 2005002 李四0001 数学89 3 2005001 张三0001 数学69 删除除了自动编号不同, 其他都相同的学生冗余信息 A: delete tablename where 自动编号not in(select min( 自动编号) from tablename group by 学号, 姓名, 课程编号, 课程名称, 分数) 3. 一个叫team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗? 答:select https://www.doczj.com/doc/bc7028961.html,, https://www.doczj.com/doc/bc7028961.html, from team a, team b where https://www.doczj.com/doc/bc7028961.html, < https://www.doczj.com/doc/bc7028961.html, 4. 请用SQL 语句实现:从TestDB 数据表中查询出所有月份的发生额都比101 科目相应月份的发生额高的科目。请注意:TestDB 中有很多科目,都有1 -12 月份的发生额。AccID :科目代码,Occmonth :发生额月份,DebitOccur :发生额。 数据库名:JcyAudit ,数据集:Select * from TestDB 答:select a.* from TestDB a ,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ******************************************************************************* ***** 5. 面试题:怎么把这样一个表儿 year month amount 1991 1 1.1

SQL经典面试题及答案

SQL经典面试题及答案 1.一道SQL语句面试题,关于group by 表内容: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-05-10 负 2005-05-10 负 如果要生成下列结果, 该如何写sql语句? 胜负 2005-05-09 2 2 2005-05-10 1 2 ------------------------------------------ create table #tmp(rq varchar(10),shengfu nchar(1)) insert into #tmp values('2005-05-09','胜') insert into #tmp values('2005-05-09','胜') insert into #tmp values('2005-05-09','负')

insert into #tmp values('2005-05-09','负') insert into #tmp values('2005-05-10','胜') insert into #tmp values('2005-05-10','负') insert into #tmp values('2005-05-10','负') 1)select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum (case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq 2) select N.rq,N.勝,M.負 from ( select rq,勝=count(*) from #tmp where shengfu='胜'group by rq)N inner join (select rq,負=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq 3)select a.col001,a.a1 胜,b.b1 负 from (select col001,count(col001) a1 from temp1 where col002='胜'

常见SQL笔试题

精心整理 SQL笔试题 1.统计查询SQL练习 数据库中表结构如下,字段分别任rg(日期),shengfu(胜负),考察groupby语句的使用:2005-05-09胜 2005-05-09胜 2005-05-09负 2005-05-09负 2005-05-10胜 2005-05-10负 2005-05-10负 如果要生成下列结果,该如何写sql语句? 胜负 2005-05-0922 2005-05-1012 答案: 1)selectrq,sum(casewhenshengfu='胜'then1else0end)'胜',sum(casewhenshengfu='负' then1else0end)'负'from#tmpgroupbyrq 2)selectN.rq,N.胜,M.负from( selectrq,胜=count(*)from#tmpwhereshengfu='胜'groupbyrq)Ninnerjoin (selectrq,负=count(*)from#tmpwhereshengfu='负'groupbyrq)MonN.rq=M.rq 3)selecta.col001,a.a1胜,b.b1负from (selectcol001,count(col001)a1fromtemp1wherecol002='胜'groupbycol001)a,

(selectcol001,count(col001)b1fromtemp1wherecol002='负'groupbycol001)b wherea.col001=b.col001 2.条件判断SQL练习 表中有ABC三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列 答案: select(casewhena>bthenaelsebend), (casewhenb>cthenbeslecend) fromtable_name 3.日期统计SQL练习 请取出tb_send表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间) 答案: select*fromtbwheredatediff(dd,SendTime,getdate())=0 4.统计查询SQL练习 有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70 分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。 显示格式: 语文数学英语

(完整版)常见SQL笔试题

SQL笔试题 1.统计查询SQL练习 数据库中表结构如下,字段分别任rg(日期),shengfu(胜负),考察group by 语句的使用: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-05-10 负 2005-05-10 负 如果要生成下列结果, 该如何写sql 语句? 胜负 2005-05-09 2 2 2005-05-10 1 2 答案: 1)select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq 2) select N.rq,N.胜,M.负from ( select rq,胜=count(*) from #tmp where shengfu='胜'group by rq)N inner join

(select rq,负=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq 3)select a.col001,a.a1 胜,b.b1 负from (select col001,count(col001) a1 from temp1 where col002='胜' group by col001) a, (select col001,count(col001) b1 from temp1 where col002='负' group by col001) b where a.col001=b.col001 2.条件判断SQL练习 表中有A B C 三列,用SQL 语句实现:当A 列大于B 列时选择A 列否则选择B 列, 当B 列大于C 列时选择B 列否则选择C 列 答案: select (case when a>b then a else b end ), (case when b>c then b esle c end) from table_name 3.日期统计SQL练习 请取出tb_send 表中日期(SendTime 字段) 为当天的所有记录?(SendTime 字段为 datetime 型,包含日期与时间)

SQL经典面试题及答案

1. 用一条SQL 语句查询出每门课都大于80 分的学生姓名 name kechengfenshu 张三语文81 张三数学75 李四语文76 李四数学90 王五语文81 王五数学100 王五英语90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80 select name from table group by name having count(kecheng)>=3 and min(fenshu)>=80 2. 学生表如下: 自动编号学号姓名课程编号课程名称分数 1 2005001 张三0001 数学69 2 2005002 李四0001 数学89 3 2005001 张三0001 数学69 删除除了自动编号不同, 其他都相同的学生冗余信息 A: delete tablename where 自动编号not in(select min( 自动编号) from tablename group by 学号, 姓名, 课程编号, 课程名称, 分数) 3. 面试题:怎么把这样一个表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4

sql经典笔试题目(整理)

一单词解释(2分/个) 34 Data 数据 Database 数据库 RDBMS 关系数据库管理系统 GRANT 授权 REVOKE取消权限 DENY 拒绝权限 DECLARE 定义变量 PROCEDURE存储过程 Transaction事务 TRIGGER触发器继续 continue 唯一 unqiue 主键 primary key 标识列 identity 外键 foreign kdy 检查 check 约束 constraint 二编写SQL语句(5分/题)50 1) 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话Create table stu (学号 int , 姓名 varchar(8), 年龄 int, 性别 varchar(4), 家庭地址 varchar(50), 联系电话 int ); 2) 修改学生表的结构,添加一列信息, 学历 Alter table stu add 学历 varchar(6); 3) 修改学生表的结构,删除一列信息,家庭住址 Alter table stu drop column 家庭地址 4) 向学生表添加如下信息: 学号姓名年龄性别联系电话学历 1 A 22 男 123456 小学 2 B 21 男 119 中学 3 C 23 男 110 高中 4 D 18 女 114 大学 Insert into stu values('1', 'A', '22', '男' , ' 123456', '小学') Insert into stu values('2', 'B', '21', '男', '119' , '中学') Insert into stu values('3', 'C', '23', '男', '110', '高中') Insert into stu values('4' , 'D', '18', '女', '114', '大学') 5) 修改学生表的数据,将电话号码以11开头的学员的学历改为“大专” Update stu set 学历=’大专’ where 联系电话 like ‘11%’ 6) 删除学生表的数据,姓名以C开头,性别为‘男’的记录删除 Delect from stu where 性别=’男’ and 姓名 like ‘c%’ 7) 查询学生表的数据,将所有年龄小于22岁的,学历为“大专”的,学生的姓名和学号示出来 Select 姓名,学号 from stu where 年龄<22 and 学历=’大专’ 8) 查询学生表的数据,查询所有信息,列出前25%的记录 Select top 25 percent * from stu 9) 查询出所有学生的姓名,性别,年龄降序排列

SQL常见面试题集(三)

SQL试题集(三) 1.用一条SQL语句 查询出每门课都大于80分的学生姓名  name kecheng fenshu 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 语文 81 王五 数学 100 王五 英语 90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) 2.学生表 如下: 自动编号 学号 姓名 课程编号 课程名称 分数 1 2005001 张三 0001 数学 69 2 2005002 李四 0001 数学 89 3 2005001 张三 0001 数学 69 删除除了自动编号不同,其他都相同的学生冗余信息 A: delete tablename where 自动编号 not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数) 一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗? 答:select https://www.doczj.com/doc/bc7028961.html,, https://www.doczj.com/doc/bc7028961.html, from team a, team b where https://www.doczj.com/doc/bc7028961.html, < https://www.doczj.com/doc/bc7028961.html, 请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101

科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有 1-12月份的发生额。 AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。 数据库名:JcyAudit,数据集:Select * from TestDB 答:select a.* from TestDB a ,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ************************************************************************面试题:怎么把这样一个表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 答案一、 select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3,

Sql常见面试题

(资料来源:互联网) Sql常见面试题(总结) 1.用一条SQL语句查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三语文 81 张三数学 75 李四语文 76 李四数学 90 王五语文 81 王五数学 100 王五英语 90 A: select distinct name from table where name not in (select distinct name fr om table where fenshu<=80) 2.学生表如下: 自动编号学号姓名课程编号课程名称分数 1 2005001 张三 0001 数学 69 2 2005002 李四 0001 数学 89 3 2005001 张三 0001 数学 69 删除除了自动编号不同,其他都相同的学生冗余信息 A: delete tablename where 自动编号 not in(select min(自动编号) from tablename g roup by 学号,姓名,课程编号,课程名称,分数) 一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗? 答:select https://www.doczj.com/doc/bc7028961.html,, https://www.doczj.com/doc/bc7028961.html, from team a, team b where https://www.doczj.com/doc/bc7028961.html, < https://www.doczj.com/doc/bc7028961.html,

请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有1-12月份的发生额。 AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。 数据库名:JcyAudit,数据集:Select * from TestDB 答:select a.* from TestDB a ,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ******************************************************************************* ***** 面试题:怎么把这样一个表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 答案一、 select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3, (select amount from aaa m where month=4 and m.year=aaa.year) as m4 from aaa group by year 这个是ORACLE 中做的: select * from (select name, year b1, lead(year) over (partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over( partition by name order by year) rk from t) where rk=1; ******************************************************************************* ***** 精妙的SQL语句! 精妙SQL语句

Sql面试题大全

Sql常见面试题受用了 1.用一条SQL 语句查询出每门课都大于80 分的学生姓名 name kecheng fenshu 张三语文 81 张三数学 75 李四语文 76 李四数学 90 王五语文 81 王五数学 100 王五英语 90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80 2.学生表如下: 自动编号学号姓名课程编号课程名称分数 1 2005001 张三 0001 数学 69 2 2005002 李四 0001 数学 89 3 2005001 张三 0001 数学 69 删除除了自动编号不同, 其他都相同的学生冗余信息 A: delete tablename where 自动编号 not in(select min( 自动编号) from tablename group by 学号, 姓名, 课程编号, 课程名称, 分数) 3.一个叫team 的表,里面只有一个字段name, 一共有4 条纪录,分别是 a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗?

答:select https://www.doczj.com/doc/bc7028961.html,, https://www.doczj.com/doc/bc7028961.html, from team a, team b where https://www.doczj.com/doc/bc7028961.html, < https://www.doczj.com/doc/bc7028961.html, 4.请用SQL 语句实现:从TestDB 数据表中查询出所有月份的发生额都比101 科目相应月份的发生额高的科目。请注意:TestDB 中有很多科目,都有1 -12 月份的发生额。 AccID :科目代码,Occmonth :发生额月份,DebitOccur :发生额。 数据库名:JcyAudit ,数据集:Select * from TestDB 答:select a.* from TestDB a ,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ********************************************************************* *************** 5.面试题:怎么把这样一个表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4

经典SQL面试题总结

表 Student(S#,Sname,Sage,Ssex) 学生表 CREATE TABLE student ( sid varchar(10) NOT NULL, sName varchar(20) DEFAULT NULL, sAge datetime DEFAULT '1980-10-12 23:12:36', sSex varchar(10) DEFAULT NULL, PRIMARY KEY (sid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Course(C#,Cname,T#) 课程表 CREATE TABLE course ( cid varchar(10) NOT NULL, cName varchar(10) DEFAULT NULL, tid int(20) DEFAULT NULL, PRIMARY KEY (cid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; SC(S#,C#,score) 成绩表 CREATE TABLE sc ( sid varchar(10) DEFAULT NULL, cid varchar(10) DEFAULT NULL, score int(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8; Teacher(T#,Tname) 教师表 CREATE TABLE teacher ( tid int(10) DEFAULT NULL, tName varchar(10) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 数据:(MySQL) insert into teacher(tid,tName) values (1,'李老师'),(2,'何以琛'),(3,'叶平'); insert into student(sid,sName,sAge,sSex) values ('1001','张三丰','1980-10-12 23:12:36','男'),('1002','张无极','1995-10-12 23:12:36','男'),('1003','李奎','1992-10-12 23:12:36','女'),('1004','李元宝','1980-10-12 23:12:36','女'),('1005','李世明','1981-10-12 23:12:36','男'),('1006','赵六','1986-10-12 23:12:36','男'),('1007','田七','1981-10-12 23:12:36','女'); insert into sc(sid,cid,score) values ('1','001',80),('1','002',60),('1','003',75),('2','001',85),('2','002',70),('3','004',100), ('3','001',90),('3','002',55),('4','002',65),('4','003',60); insert into course(cid,cName,tid) values ('001','企业管理',3),('002','马克思',3),('003','UML',2),('004','数据库',1),('005 ','英语',1); 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩;

经典的SQL语句面试题

经典的SQL语句面试题 from (select s#,score from SC where C#= 001 ) a, (select s#,score from SC where C#= 002 ) b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩;select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like 李% 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student where S# not in (select distinct( SC.S#) from

SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname= 叶平); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#= 001 and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#= 002 ); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,T eacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname= 叶平group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname= 叶平)); 8、查询所有课程成绩小于60分的同学的学号、姓名; select S#,Sname from Student

2021年SQL经典面试题及答案

1. 用一条SQL 语句查询出每门课都不不大于80 分学生姓名 name kecheng fenshu 张三语文81 张三数学75 李四语文76 李四数学90 王五语文81 王五数学100 王五英语90 A:select distinct name from table where name not in (select distinct name from table where fenshu<=80) select name from table group by name having min(fenshu)>80 select name from table group by name having count(kecheng)>=3 and min(fenshu)>=80 2. 学生表如下: 自动编号学号姓名课程编号课程名称分数 1 001 张三0001 数学69 2 002 李四0001 数学89 3 001 张三0001 数学69 删除除了自动编号不同,其她都相似窗生冗余信息

A:delete tablename where 自动编号not in(select min( 自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数) 3. 面试题:怎么把这样一种表儿 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一种成果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 答案一、 select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3, (select amount from aaa m where month=4 and m.year=aaa.year) as m4 from aaa group by year

SQL基础面试题

SQL面试题(3) 1.触发器的作用? 答:触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的。它可以强化约束,来维护数据的完整性和一致性,可以跟踪数据库内的操作从而不允许未经许可的更新和变化。可以联级运算。如,某表上的触发器上包含对另一个表的数据操作,而该操作又会导致该表触发器被触发。2。什么是存储过程?用什么来调用? 答:存储过程是一个预编译的SQL语句,优点是允许模块化的设计,就是说只需创建一次,以后在该程序中就可以调用多次。如果某次操作需要执行多次SQL,使用存储过程比单纯SQL语句执行要快。可以用一个命令对象来调用存储过程。 3。索引的作用?和它的优点缺点是什么? 答:索引就一种特殊的查询表,数据库的搜索引擎可以利用它加速对数据的检索。它很类似与现实生活中书的目录,不需要查询整本书内容就可以找到想要的数据。索引可以是唯一的,创建索引允许指定单个列或者是多个列。缺点是它减慢了数据录入的速度,同时也增加了数据库的尺寸大小。3。什么是内存泄漏? 答:一般我们所说的内存泄漏指的是堆内存的泄漏。堆内存是程序从堆中为其分配的,大小任意的,使用完后要显示释放内存。当应用程序用关键字new等创建对象时,就从堆中为它分配一块内存,使用完后程序调

用free或者delete释放该内存,否则就说该内存就不能被使用,我们就说该内存被泄漏了。 4。维护数据库的完整性和一致性,你喜欢用触发器还是自写业务逻辑?为什么? 答:我是这样做的,尽可能使用约束,如check,主键,外键,非空字段等来约束,这样做效率最高,也最方便。其次是使用触发器,这种方法可以保证,无论什么业务系统访问数据库都可以保证数据的完整新和一致性。最后考虑的是自写业务逻辑,但这样做麻烦,编程复杂,效率低下。5。什么是事务?什么是锁? 答:事务就是被绑定在一起作为一个逻辑工作单元的SQL语句分组,如果任何一个语句操作失败那么整个操作就被失败,以后操作就会回滚到操作前状态,或者是上有个节点。为了确保要么执行,要么不执行,就可以使用事务。要将有组语句作为事务考虑,就需要通过ACID测试,即原子性,一致性,隔离性和持久性。 锁:在所以的DBMS中,锁是实现事务的关键,锁可以保证事务的完整性和并发性。与现实生活中锁一样,它可以使某些数据的拥有者,在某段时间内不能使用某些数据或数据结构。当然锁还分级别的。 6。什么叫视图?游标是什么? 答:视图是一种虚拟的表,具有和物理表相同的功能。可以对视图进行增,改,查,操作,试图通常是有一个表或者多个表的行或列的子集。对视图的修改不影响基本表。它使得我们获取数据更容易,相比多表查询。

经典的sql数据库面试题以及答案

Student(S#,Sname,Sage,Ssex) 学生表 S#:学号;Sname:学生姓名;Sage:学生年龄;Ssex:学生性别 Course(C#,Cname,T#) 课程表 C#,课程编号;Cname:课程名字;T#:教师编号 SC(S#,C#,score) 成绩表 S#:学号;C#,课程编号;score:成绩 Teacher(T#,Tname) 教师表 T#:教师编号; Tname:教师名字 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平'); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002'); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平' group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname='叶平')); 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

相关主题
文本预览
相关文档 最新文档