一學期積累下來的SQL語句寫法的學習
整合了一下上學期學習的積累,希望可以幫到初學者!
可能以後會有用吧!
A 基本語句的運用
操作基於emp表
1、按工資從高到低排列
SQL> select rownum as 次序,ename,sal
2 from (select ename,sal
3 from emp
4 order by sal desc)
5 where rownum<=5
6 ;
2、做內嵌式圖由大到小排序後找前五個的錯誤寫法
SQL> select ename,sal
2 from emp
3 where rownum<=5
4 order by sal desc;
先找前五個記錄後按照工資排序(並不是所求,這是錯誤的寫法)
3、把名為scott的job 獎金更改
update emp set job=‘MANAGER‘,comm=‘4000‘
where ename=‘SCOTT‘;
4、尋找薪水大於scott或hiredate早於scott的
select ename,empno from emp
where sal>(select sal from emp where ename=‘SCOTT‘)
or hiredate>(select hiredate from emp where ename=‘SCOTT‘);
5、尋找最大
select ename,sal from emp
where sal=(select max(sal) from emp);
6、尋找部門人員小於4 的部門
select avg(sal) from emp
where deptno=
(select deptno from emp
group by deptno having count(*)<4);
7、一些設置參考
set linesize 180 設置每行顯示的字符總數
set pagesize 100 設置每頁顯示的行數
set feedback on/off 設置是否顯示已選擇XX行
set heading on/off 設置是否顯示列名
set time on/off 設置是否顯示當前系統時間
set timing on/off 設置是否顯示每條SQL執行消耗的時間
set termout on/off 設置在執行sql文件時是否在控制臺打印相關信息
set trimout on/off 設置是否去除標準輸出每行的拖尾空格
set trimspool on/off 設置是否去除spool輸出文件中每行的拖尾空格
8、按照不同的維度分組 多維的數據統計
select avg(grade),stu_no,cno
from mark2
group by rollup(cno,stu_no);
先以cno分組,再以stu_no排序
計算一組cno的平均值
9、cube 把group by的數據各維進行組合
select avg(grade),stu_no,cno
from mark2
group by cube(stu_no,cno);
AVG(GRADE) STU_NO CNO
---------- ---------- ----------
78.8888889
90 1
83.3333333 2
63.3333333 3
73.3333333 1404010525
90 1404010525 1
80 1404010525 2
50 1404010525 3
83.3333333 1404010526
100 1404010526 1
80 1404010526 2
70 1404010526 3
80 1404010527
80 1404010527 1
90 1404010527 2
70 1404010527 3
先計算按照cno分組的平均值
再按照(stu_no,cno)分組計算平均值
10、查詢一個表中 課程1比課程2分數高的學生號
select a.num,a.score
as score1,b.score
as score2 from(
select * from sc where cno=‘001‘)a
left join
(select * from sc where cno=‘002‘)b
on a.num=b.num
where a.score<b.score;
11、查詢表中課程1 比課程2高的學生信息:返回多值?用in!
select num,name
from stu
where num in
(select a.num from(
select * from sc where cno=‘001‘)a
left join
(select * from sc where cno=‘002‘)b
on a.num=b.num
where a.score<b.score);
12、查詢平均成績大於60的學生學號、平均成績、姓名
1\查詢平均成績大於90的學生學號: select num,avg(score) from sc group by num having avg(score)>90
2\ select num,name
from stu
where num in(
select num from sc group by num having avg(score)>90);(平均成績怎麽辦)?
3\select num,avg(score)
from sc
group by num having avg(score)>90;(單查學號和平均成績)
最後寫法:
select stu.num,stu.name,avg(sc.score)
from stu,sc
where stu.num in(
select num from sc group by (sc.num,name) having avg(score)>90)
and stu.num=sc.num ;
13、查詢所有同學的學號、姓名、選課數、總成績
select stu.num,stu.name,count(sc.cno),sum(sc.score)
from stu,course,sc
where stu.num=sc.num and course.cno=sc.cno
group by stu.num,stu.name order by num;
14、查詢所有姓李的老師
select * from teacher
where teacher.name like ‘李%‘;
15、查詢沒選過chinese課程的學生的學號與姓名
select stu.num,stu.name
from stu
where stu.num not in
(select distinct stu.num
from stu,course,sc
where stu.num=sc.num and course.cno=sc.cno
and course.name=‘chinese‘);
16、查詢選過maths課程的所有學生學號與姓名
select stu.num,stu.name
from stu
where stu.num in
(select distinct stu.num
from stu,course,sc
where stu.num=sc.num and course.cno=sc.cno
and course.name=‘maths‘);
17、查詢同時選過maths和chinese課程的所有學生學號與姓名
select * from stu
where stu.num in
(select num from sc,course where sc.cno=course.cno and course.name=‘maths‘)
and stu.num in
(select num from sc,course where sc.cno=course.cno and course.name=‘chinese‘);
18、查詢所有課程小於90分的學生姓名與學號。
select * from stu
where stu.num not in
(select num from sc where score>=90);
B PL/SQL語句
1、用table類型輸出一些語句
declare
type my_table_type is table of varchar(20)
index by binary_integer;
my_table my_table_type;
begin
my_table(1):=‘what the fuck‘;
my_table(2):=‘hehe is a gay‘;
my_table(3):=‘what the hell‘;
my_table(-100):=‘is it a fky‘;
dbms_output.put_line(my_table(1));
dbms_output.put_line(my_table(2));
dbms_output.put_line(my_table(3));
dbms_output.put_line(my_table(-100));
dbms_output.put_line(my_table.count);
end;
/
2、select語句的應用 :註意,查詢結果只能是唯一的一條記錄
SQL> declare
2 v_dname student.name%type;
3 v_dept_rec student%rowtype;
4 begin
5 select name into v_dname from student where num=‘02‘;
6 dbms_output.put_line(‘name of student 02 is:‘||v_dname);
7 end;
8 /
name of student 02 is:wangwu
3、遊標:是一個緩存區,在內存裏臨時存放多條記錄的存儲區。
打開遊標 取數據 關閉遊標
4、存儲過程
查詢指定號碼的姓名:
create or replace procedure p1
as
v_ename emp.ename%type;
begin
select ename into v_ename
from emp where empno=7788;
dbms_output.put_line(‘7788員工姓名為‘||v_ename);
end;
/
查詢指定編號員工的姓名
create or replace procedure p1(p_no in emp.empno%type)
as
v_ename emp.ename%type;
begin
select ename into v_ename
from emp where empno=p_no;
dbms_output.put_line(p_no||‘員工姓名為‘||v_ename);
exception
when no_data_found then
dbms_output.put_line(‘沒有這個編號‘);
when others then
dbms_output.put_line(‘系統有錯‘);
end;
/
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
C sql語句練習
1、 列出所有員工的姓名及其直接上司的姓名;
select a.ename empname,b.ename mgrname
from emp a,emp b
where a.mgr=b.empno;
2、 列出在“IT”部門工作的員工姓名;
select ename from emp
where deptno=
(select deptno from dept
where dname=‘IT‘);
3、 列出工資高於公司平均工資的所有雇員的姓名、編號及部門號;
select ename,deptno,empno from emp
where sal>(select avg(sal) from emp);
4、 列出在每個部門的員工數量、平均工資;
select avg(sal),count(*)
from emp
group by deptno ;
5、列出所有部門的詳細信息和部門人數;
select * from dept a left join
(select count(*),deptno from emp
group by deptno)
b
on
a.deptno=b.deptno;
6、列出各種職位的最低工資;
select job,min(sal)
from emp
group by job;
7、 列出部門經理中工資最低的那個經理的姓名、工資和部門號;
select ename,sal,deptno
from emp
where sal<=all(
select sal from emp where job=‘MANAGER‘
) and job=‘MANAGER‘;
8、 列出公司裏工資最高的五位員工的基本信息。
select * from emp
where ename in(
select ename from (
select ename from emp
order by sal desc) a
where rownum <6);
一學期積累下來的SQL語句寫法的學習