当前位置:首页 » 编程语言 » sql面试经典500题
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql面试经典500题

发布时间: 2022-06-22 14:29:47

⑴ 经典的sql面试题目,百度有答案,谁有更简洁的

select a.id,a.proid as pro1,b.proid as pro2 from (select * from tablename where proid =1) a,(select * from tablename where proid =2) b where a.id=b.id

⑵ SQL语句面试题

SELECT*,
(SELECTCOUNT(*)FROM(SELECTCOUNT(*),b.sidFROMscbLEFTJOINcoursecONb.Cid=c.CidGROUPBYb.sid,c.tid)owWHEREow.sid=a.sid)as'
选课数量',
(SELECTsum(Score)FROMScdWHEREa.sid=d.sid)as'总成绩'
FROMstudenta
;

SELECTd.sid,d.snamefromteachera
LEFTJOINcoursebona.tid=b.tid
LEFTJOINscconc.cid=b.cid
leftJOINstudentdONd.sid=c.sid
WHEREa.tname='叶萍';

SELECTsid,sname
FROMstudent
WHEREsidin(selecta.sid
FROMscAleftjoinscbona.sid=b.sid
WHEREa.cid=1andb.cid=2anda.score>b.score)

SELECTsc.Sid,sum(CASEWHENc.Cname='
数学'thensc.Scoreelse0end)数学,SUM(casewhenc.Cname='物理
'THENsc.ScoreELSE0END)物理,AVG(sc.Score)平均分
FROMsc
INNERJOINCourseconsc.Cid=c.Cid
WHEREc.Cnamein('数学','物理')
GROUPBYsc.Sid
ORDERBYAVG(sc.Score)DESC

insertintoSc(Sid,Cid,Score)values(003,3,85);

insertintoSc(Sid,Cid,Score)values(003,3,30);

首先这个不知道是你i写错了还是怎么一个人化学成绩有2个所以我在这里按照你这个上做的查询所以有一个人是选了单个课程

⑶ SQL面试题

select name
from
(
select name,sum(score) sc
from 表

group by name
) t1
where t1.sc>200
这样?

⑷ sql面试题

1,答
select * into B库.table_b from A库.table_a

2,答
select * into A库.new_table from A库.table_a left outer join B库.table_b on A库.table_a.name=B库.table_b.name and A库.table_a.Text=B库.table_b.text

delete from A库.table_a left outer join A库.new_table on A库.table_a.name=A库.new_table.name and A库.table_a.Text=A库.new_table.text
delete from B库.table_b left outer join A库.new_table on b库.table_b.name=A库.new_table.name and b库.table_b.Text=A库.new_table.text

3 答

select * from (select top 10 * from (select top 100 * from 表 )as a) as b

4 答
select * into 表c from 表A
union all
select * into 表c from 表B

试试如果不对请指正。

⑸ SQL数据库面试题 急急急

a)select pname as '商品名',avg(qty) as 平均销售量 from s,p,m where m.city='上海' and s.mno=m.mno and p.pno=s.pno,select p.Pno,p.pname,sum(s.qty)
from s left join p on s.pno=p.pno left join m on p.Mno=m.Mno
where m.city='上海市'
group by p.Pno,p.pname,p.city,p.color
b)、先删除Sale表的外键PNO,再删除gds表。

c)联系:视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系
区别:1、视图是已经编译好的sql语句。而表不是
2、视图没有实际的物理记录。而表有。
3、表是内容,视图是窗口
4、表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时四对它进行修改,但视图只能有创建的语句来修改
5、表是内模式,视图是外模式
6、视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。
7、表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。
8、视图的建立和删除只影响视图本身,不影响对应的基本表。

⑹ 面试题目(sql)

1、忍不住想说一句,因为第一题中的字段类型是
【日期型】,而各种数据库操作日期型数据有不同的方法,没有一种共通的方法,所以脱离了数据库而言没有一种共通的sql。
2、select
ID,NAME,ADDRESS,PHONE,LOGDATE
from
T
where
ID
in(
select
ID
from
T
group
by
NAME
having
count(*)>1)
order
by
NAME;
3、delete
from
T
where
ID
not
in
(select
min(id)
from
T
group
by
name);
4、update
T
set
T.ADDRESS=(select
E.ADDRESS
from
E
where
E.NAME=T.NAME),
T.PHONE=(select
E.PHONE
from
E
where
E.NAME=T.NAME);
5、这个不同的数据库也有不同的处理方法,不能脱离数据库谈了。
如:SqlServer或者access可以使用
top
oracle可以使用
rownum

---
以上,希望对你有所帮助。

⑺ sql面试题,请大神解答,急!!!

第一题,最高的10-20。
1234select*from(selectcolD,colE,dense_rank()over(orderbycolGdesc)asrkfromtableb)whererkbetween10and20--当然面试的话这里可以注明dense_rank和rank区别等等。

第二题,2楼写的就可以了

第三题,注意题目是人数之和
12345selectcount(*)ascntfromtableBwherecolAin(=传入idconnectbypriorcolA=colB)

第四题
--测试数据
withtableAas(select1ascolA,0ascolB,'北京',0ascolB,'上海'fromal),tableBas(select1ascolD,'人员1'ascolE,1ascolA,2000ascolG,0ascolHfromalunionallselect2,'人员2',1,2000,0fromalunionallselect3,'人员3',1,2000,0fromalunionallselect4,'人员4',1,2000,0fromalunionallselect5,'人员5',1,2000,1fromalunionallselect6,'人员6',2,2000,1fromalunionallselect7,'人员7',1,2000,1fromal)--查询sqlselectdecode(t.colH,0,'女','男')asgender,max(decode(t.colC,'北京',t.cnt,0))as北京,max(decode(t.colC,'上海',t.cnt,0))as上海from(selecta.colC,b.colH,count(*).colA=b.colAgroupbycolC,colH)tgroupbyt.colH
这里可以解释下本sql的思路:--首先查出每个地区不同性别的人数,在行转列(由于题目很明显告诉你固定数据)

⑻ sql语句 面试题

A.创建表格CODE省略

注明:学生表PK stu_id 课程表pk cos_id 分数表PK enrollment_id FK stu_id,cos_id

B.插入数据code省略

C.Query

  1. select s.stu_id,stu_name,count(cos_id) from student s,enrollments e where s.stu_id = e.stu_id and e.grade>60 group by s.stu_id,stu_name;

  2. select e.stu_id,s.stu_name,c.cos_name from student s,enrollments e,course c

    where s.stu_id = e.stu_id

    and e.cos_id = c.cos_id

    and c.cos_name = 'CHINESE'

    and s.stu_name like 'W%';

  3. select stu_id,stu_name from (select e.stu_id,stu_name,cos_name from enrollments e,student s,course c

    where s.stu_id = e.stu_id

    and e.cos_id = c.cos_id

    and c.cos_name IN ('CHINESE','MUSIC'))

    group by stu_id,stu_name

    having count(cos_name) = 2

  4. select distinct e.cos_id,c.cos_name,count(e.stu_id) stu_count,count(e.stu_id)-NVL(A.FAIL,0) upscore,(count(e.stu_id)-NVL(A.FAIL,0))/count(e.stu_id) rate from

    (select cos_id,count(stu_id) fail from enrollments where grade<60 group by cos_id) a,enrollments e,course c

    where e.cos_id = a.cos_id(+)

    and e.cos_id = c.cos_id

    group by e.cos_id,NVL(a.fail,0),c.cos_name;

  5. update student

    set avg_grade =(select avg(grade) X from enrollments group by stu_id

    having student.stu_id = enrollments.stu_id);

  6. select stu_id,avg(grade) from

    (select stu_id,cos_id,grade,row_number() over(partition by stu_id order by grade ) X from enrollments)

    group by stu_id

    having count(*)<=2

    UNION

    select A.stu_id,avg(A.grade)from

    (select stu_id,cos_id,grade,row_number() over(partition by stu_id order by grade ) X from enrollments) A,

    (select stu_id,count(*) c from

    (select stu_id,cos_id,grade,row_number() over(partition by stu_id order by grade ) X from enrollments)

    group by stu_id) B

    where A.stu_id = B.stu_id

    and A.x>1 and x<B.c

    group by A.stu_id,b.c

_________________________________________________

环境:oracle 10g/TOAD 以上代码均通过测试,如有问题,请联系,谢谢

⑼ 数据库SQL查询语句面试题

5.1
select a.username,b.deptname from users a,dept b where a.dept_id=b.id;

5.2
update users set dept_id='9' where dept_id='2';

5.3
select a.deptname,b.count_id from dept a,(select dept_id,count(id) as count_id from users group by dept_id having count(id)>1) b where a.id=b.dept_id;

5.4
select a.deptname,b.count_man,c.count_woman from dept a,(select dept_id,count(sex) as count_man from users where sex='男' group by dept_id) b,(select dept_id,count(sex) as count_woman from users where sex='女' group by dept_id) c where a.id=b.dept_id and a.id=c.dept_id;

5.5
添加历史记录表
create table history(
id number(8), -- 记录编号
dept_id varchar2(5), -- 部门ID
user_id varchar2(5), -- 用户ID
change_date date -- 变动日期
);

⑽ SQL 面试题

SELECT aa.日期, aa.数值, SUM(lj.数值) AS 累计
FROM 累积 lj INNER JOIN
(SELECT *
FROM 累积) aa ON lj.日期 <= aa.日期
WHERE (aa.日期 <= '2005-05-04') --日期可以变成时间段
GROUP BY aa.日期, aa.数值
ORDER BY aa.日期