oracle-sql入門練習及答案


1,通過命令行方式打開sqlplus
2,執行登錄命令
sqlplus scott/scott@192.168.248.129/orcl
3進行sqlplus命令測試

Set time on
Set pagesize 數字
Set linesizes 數字
Set pause on/off

Describe命令  查看表或視圖結構 也可以desc

Show 查看相應參數 如:show parameter,show user,show error等

Get命令 把一個sql腳本文件內容放入緩沖區

用系統編輯程序編輯命令
在SQL*PLUS中運行操作系統缺省的文本編輯程序(EDIT)
命令為:edit或者ed

將緩存中的sql語句保存到文件中:SAVE 文件名【具體路徑】
查看緩存sql:list

執行文件中的sql語句:
START my.sql
@ my.sql

清空緩沖區:clear buffer
格式化命令:column
column sal format $999,999.00
如:
SQL> select ename, sal from emp
  2
SQL> /


ENAME               SAL
---------- ------------
SMITH           $800.00
ALLEN         $1,600.00
WARD          $1,250.00
JONES         $2,975.00
MARTIN        $1,250.00
BLAKE         $2,850.00
CLARK         $2,450.00

重置為默認值:clear columns;
將屏幕上的內容寫入到文件中
spool fileName
結束寫入
spool off

3.Oracle查詢
1) 查詢EMP表,顯示部門號為10的所有雇員的NAME 、JOB、SALARY和 DEPTNO,並以崗位降序、工資升序進行排序。
select ename,job,sal,deptno from emp where deptno=10 order by job desc , sal asc

2) 從EMP表中查詢所有雇員的NAME和補助(SALARY+COMM),並處理null行。
select ename,(sal+nvl(comm,0)) from emp

3) 統計30號部門的總人數、人均工資、最高工資和最低工資。
select count(*) totle,avg(sal),max(sal),min(sal) from emp where deptno=30

4) 查詢各個部門各個工種的平均工資、每個部門的平均工資和所有員工的平均工資。
select deptno,job,avg(sal) from emp group by rollup(deptno,job);

5) 查詢各個部門中各個工種的平均工資、每個部門的平均工資、每個工種的平均工資和所有員工的平均工資。
select deptno,job,avg(sal) from emp group by cube(deptno,job);

6) 查詢EMP,顯示1999年之后參加工作人員的NAME、JOB和HIREDATE,並以工作日期的升序進行排序。
select * from emp where hiredate>'1-JAN-99';

7) 查詢名字以“S”開始的所有職工:
select *from emp where ename like 'S%';

8) 讓員工姓名右對齊顯示
col ename jus left

9) 顯示姓名只有5個字母組成的員工信息
select *from emp where ename like '_____'
或者
select *from emp where length(ename)=5

10) 查詢在當月倒數第三天入職的員工信息
select *from emp where hiredate = last_day(hiredate)-2



2) 對各表中的數據進行不同條件的查詢;
a)查詢全體學生的學號和姓名
select sno,sname from student;

b)查詢全體學生的詳細記錄
select *from student;

c)查詢所有選修過課程的學生學號
select distinct sno from sc;
或者
select sno from student where sno in (select sno from sc);

d)查詢考試有不及格的學生學號
select sno from sc where nvl(grade,0) <60;

e)查詢不是信息系(IS)、計算機系(CS)的學生性別、年齡、系別
select ssex,sage,sdept from student where sdept not in('is','cs');

f)查詢選修了4號課的學生學號和成績,結果按成績降序排列
select sc1.sno,sc1.grade from sc sc1,sc sc2 where sc2.cno=4 and sc1.sno=sc2.sno;

g)查詢每個課程號和相應的選課人數
select cno,count(sno) from sc group by cno;

h)查詢計算機系(CS)的學生姓名、年齡、系別
select sname,sage,sdept from student where sdept='cs'

i)查詢年齡18-20歲的學生學號、姓名、系別、年齡;
select sno,sname,sage,sdept from student where sage between 18 and 20

j)查詢姓劉的學生情況
select *from student where sname like '劉%'

k)查詢既選修1號課程,又選修2號課程的學生學號和成績
select sno,grade from sc where cno = 1 and cno in(select sno from sc where cno=2);

l)查詢學生的姓名和出生年份(今年2003年)
select sname,2017-sage born from student;

m)查詢沒有成績的學生學號和課程號
select s.sno,c.cno from student s,course c where (s.sno,c.cno) not in(select sno,cno from sc);

n)查詢總成績大於200分的學生學號
select sno from sc group by sno having sum(grade)>200;

o)查詢每門課程不及格學生人數
select cno ,count(sno) from sc where nvl(grade,0)<60 group by cno【不及格分組】

p)查詢不及格課程超過3門的學生學號
select sno from (select sno,cno from sc where nvl(grade,0)<60) r group by sno having count(r.cno)>3

q)查詢年齡在10到19歲之間的學生信息
select *from student where sage>=10 and sage<=19;
select *from student where sage between 10 and 19;【between and 包含兩頭】

r)查詢全體學生情況,按所在系升序排列,同一個系的學生按年齡降序排列
select *from student order by sdept asc,sage desc;

s)查詢選了1號課程的學生平均成績
select cno, avg(grade) avgGrade from sc where cno=1 group by cno;

t)查詢選了3號課程的學生的最高分
select max(grade) from sc where cno=3;

u)查詢每個同學的總成績
select sno, count(grade) from sc group by sno;

復雜查詢
內容和主要步驟:
1)實驗一中的數據為基礎

2) 對各表中的數據進行不同條件的連接查詢和嵌套查詢;

(1)?查詢每個學生及其選課情況;
select * from sc;

(2)?查詢每門課的間接先修課
select a.cno,b.cpno from course a,course b where a.cpno=b.cno;

(3)?將STUDENT,SC進行右連接
select *from student right join sc on student.sno=sc.sno;

(4)?查詢有不及格的學生姓名和所在系
select sname,sdept from student where sno in (select distinct sno from sc where nvl(grade,0)<60);

(5)?查詢所有成績為優秀(大於90分)的學生姓名
select sname from student where sno in (select distinct sno from sc where nvl(grade,0)>90);

(6)?查詢既選修了2號課程又選修了3號課程的學生姓名、學號;
 select sname,sno from student where sno  in (select sno from sc where cno = 3 and cno in(select sno from sc where cno=2));

(7)?查詢和劉晨同一年齡的學生
select *from student where sname<>'劉晨' and sage in (select sage from student where sname='劉晨');

(8)?選修了課程名為“數據庫”的學生姓名和年齡
select sname,sage from student  where sno in(select sno from sc where cno in(select cno from course where cname='數據庫'))

(9)?查詢其他系比IS系任一學生年齡小的學生名單
select sname from student where sdept<>'is' and sage < (select max(sage) from student where sdept='is')

(10)?查詢其他系中比IS系所有學生年齡都小的學生名單
select sname from student where sdept<>'is' and sage < (select min(sage) from student where sdept='is')

(11)?查詢選修了全部課程的學生姓名
 select sname from student where sno in(select sno from sc group by sno having count(cno) = (select count(cno)from sc))

(12)?查詢計算機系學生及其性別是男的學生
select *from student where sdept='cs' and ssex='男'

(13)?查詢選修課程1的學生集合和選修2號課程學生集合的差集
select *from sc where cno='1' and sno not in(select sno from sc where cno='2')

(14)?查詢李麗同學不學的課程的課程號
select cno from course where cno not in(select cno from sc where sno in (select sno from student where sname='李麗'))

(15)?查詢選修了3號課程的學生平均年齡
select avg(sage) from student where sno in(select sno from sc where cno=3)

(16)?求每門課程學生的平均成績
select cno ,avg(grade) from sc group by cno;

(17)?統計每門課程的學生選修人數(超過3人的才統計)。
要求輸出課程號和選修人數,結果按人數降序排列,若人數相同,按課程號升序排列
select cno ,count(sno) from sc group by cno having count(sno)>3 order by count(sno) desc,cno asc;

(18)?查詢學號比劉晨大,而年齡比他小的學生姓名。
select a.sname from student a,(select sno,sname,sage from student where sname='劉晨') b where a.sno>all b.sno and a.sage<all b.sage;

(19)?求年齡大於女同學平均年齡的男同學姓名和年齡
select sname,sage from student where sage  > (select avg(sage) from student where ssex='女');

(20)?求年齡大於所有女同學年齡的男同學姓名和年齡
select sname,sage from student where sage  >all (select sage from student where ssex='女');

(21)?查詢至少選修了95002選修的全部課程的學生號碼
思想:首先查找學號為95002的同學的課程號集合,然后查找sc中所有學號在該集合中的選課信息,【此時該選課信息均為95002選過的課程信息】記為 msg
然后從msg中以學號進行分組,然后選出cno個數和95002選課個數相同的學號,該學號就是至少選修了95002選修的全部課程的學生學號
select sno from sc where sno<>95002
and
cno in (select cno from sc where sno=95002)
group by sno
having
count(cno)=(select count(cno) from sc where sno=95002)

(22)?查詢95001和95002兩個學生都選修的課程的信息
select cno from sc where sno =95001 and cno in (select cno from sc where sno=95002);


更新查詢題目:
1)    應用INSERT,UPDATE,DELETE語句進行更新操作;
a)    插入如下學生記錄(學號:95030,姓名:李莉,年齡:18)
insert into student (sno,sname,sage) values (95030,'李莉',18)

b)    插入如下選課記錄(95030,1)
insert into sc (sno,cno) values(95030,1)

c)    計算機系學生年齡改成20
update student set sage=20  where sdept='cs';

d)    數學系所有學生成績改成0
update sc set grade = 0
where sno in (select sno from student where sdept='ma')

e)    把低於總平均成績的女同學成績提高5分
update sc set grade = grade+5 where sno in (select sno from student where ssex='女' and grade < (select avg(grade) from sc))

f)    修改2號課程的成績,若成績小於75分提高5%,成績大於75時提高%1
update sc set  grade = grade+grade*0.05 where grade<75
update sc set grade=grade+grade*0.01 where grade>75

4)(兩個語句實現,注意順序)
g)    刪除95030學生信息
delete student where sno=95030

h)    刪除SC表中無成績的記錄
delete sc where grade is null;

i)    刪除張娜的選課記錄
delete sc where sno in (select sno from student where sname='張娜')

j)    刪除數學系所有學生選課記錄
delete sc where sno in (select sno from student where sdept='ma')

k)    刪除不及格的學生選課記錄
delete sc where grade <60

l)    查詢每一門課程成績都大於等於80分的學生學號、姓名和性別,把值送往另一個已經存在的基本表STU(SNO,SNAME,SSEX)中
create table stu as (select sno,sname,ssex from student where sno in(select sno from sc where sno not in(select distinct sno from sc where grade<80)))

m)    把所有學生學號和課程號連接追加到新表中
create table newTable as select * from student,sc;

n)    所有學生年齡增1
update student set sage=sage+1;

o)    統計3門以上課程不及格的學生把相應的學生姓名、系別追加到另外一個表中
create table failT as select sname,sdept from student where sno in(select sno from sc where grade<60 group by sno having count(cno)>=3)




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM