1. 程式人生 > >SQL面試題

SQL面試題

and 試題 mit heat 指定 查詢 刪除 關聯 ner

1.表結構

-- Create table 課程表
create table T_COURSE
(
  cno        VARCHAR2(20),
  cname      VARCHAR2(20),
  creatdate  CHAR(10),
  updatedate CHAR(10)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

-- Create table 學生表
create table T_STUDENT
(
  sno   VARCHAR2(20),
  sname VARCHAR2(20),
  sage  NUMBER
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

-- Create table 學生與課程關聯表
create table T_STUDENT_COURSE
(
  sno     VARCHAR2(64),
  cno     VARCHAR2(64),
  score   NUMBER(15,2),
  ischeat NUMBER
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

二:表數據

insert into T_STUDENT (SNO, SNAME, SAGE)
values ('s1001', 'JITION', 50);

insert into T_STUDENT (SNO, SNAME, SAGE)
values ('s1002', 'CQS', 13);

insert into T_STUDENT (SNO, SNAME, SAGE)
values ('s1003', 'CAO', 30);

insert into T_STUDENT (SNO, SNAME, SAGE)
values ('s1004', 'QI', 15);

insert into T_STUDENT (SNO, SNAME, SAGE)
values ('s1005', 'SHUN', 30);

--
insert into T_COURSE (CNO, CNAME, CREATDATE, UPDATEDATE)
values ('c1001', 'JAVA程序設計', null, null);

insert into T_COURSE (CNO, CNAME, CREATDATE, UPDATEDATE)
values ('c1002', '數學', null, null);

insert into T_COURSE (CNO, CNAME, CREATDATE, UPDATEDATE)
values ('c1003', '英語', null, null);

insert into T_COURSE (CNO, CNAME, CREATDATE, UPDATEDATE)
values ('c1004', 'SQL程序設計', null, null);

insert into T_COURSE (CNO, CNAME, CREATDATE, UPDATEDATE)
values ('c1005', '語文', null, null);

insert into T_COURSE (CNO, CNAME, CREATDATE, UPDATEDATE)
values ('c1006', 'JAVA算法', '2010-10-30', null);

insert into T_COURSE (CNO, CNAME, CREATDATE, UPDATEDATE)
values ('c1007', '算法設計', '2017-10-30', null);

insert into T_COURSE (CNO, CNAME, CREATDATE, UPDATEDATE)
values ('c1008', '物理', null, null);

--
insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1001', 'c1001', 36.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1001', 'c1002', 72.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1002', 'c1001', 100.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1002', 'c1003', 0.00, 0);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1003', 'c1011', 0.00, 0);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1004', 'c1001', 90.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1004', 'c1003', 80.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1001', 'c1007', 0.00, 0);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1002', 'c1007', 90.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1003', 'c1007', 85.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1004', 'c1007', 0.00, 0);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1005', 'c1007', 96.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1001', 'c1004', 80.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1001', 'c1003', 80.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1001', 'c1005', 90.00, 1);

insert into T_STUDENT_COURSE (SNO, CNO, SCORE, ISCHEAT)
values ('s1001', 'c1006', 80.00, 1);

3:問題及sql語句

--1.指定課程所有學員姓名和學號
select ts.sno,ts.sname,tc.cname from t_student_course tsc 
inner join T_COURSE tc on tc.cno=tsc.cno
inner join t_Student ts on ts.sno=tsc.sno
where tc.cname='JAVA程序設計'

--2.指定課程並且年齡大於20
select count(1) "求和" from t_student_course tsc 
inner join T_COURSE tc on tc.cno=tsc.cno
inner join t_Student ts on ts.sno=tsc.sno
where tc.cname='算法設計' and ts.sage>20

--3.查詢選修課程超過5門的學生學號、姓名
      --方法一
select ts.sno,ts.sname from t_student_course tsc 
left join T_COURSE tc on tc.cno=tsc.cno
left join t_Student ts on ts.sno=tsc.sno
GROUP BY ts.sno ,ts.sname
having count(*)>5
       --方法二
select ts.sno,ts.sname from t_student ts
where ts.sno in(
select tsc.sno FROM t_student_course tsc
GROUP BY tsc.sno
having count(Distinct(tsc.cno))>5 
)

--4.修改t_student_course表中ISCheat為0的學生成績更新為0
update t_student_course  tsc set tsc.score=0 where tsc.ischeat=0 

--5.將t_student_course表中ISCheat為0的學生從t_student表中刪除
delete from t_student ts where ts.sno in( select tsc.sno from t_student_course  tsc where   tsc.ischeat=0 )

--6.查詢所有課程分數小於60分且沒有作弊的學生號,學生姓名,分數
select * from t_student_course tsc
left join t_student ts on tsc.sno=ts.sno
where tsc.score<60 and tsc.ischeat=1


SQL面試題