oracle查詢練習


1

成績表score如下,查詢出每門課都大於80分的學生姓名

准備數據

-建表-

  
SQL> create table score(
  2  name varchar(50),
  3  kecheng varchar(50),
  4  fenshu number(3)
  5  );
  

插入數據

 
SQL> insert into score(name,kecheng,fenshu) values('張三','語文',81);
1 row inserted

SQL> insert into score(name,kecheng,fenshu) values('張三','數學',78);
1 row inserted

SQL> insert into score(name,kecheng,fenshu) values('李四','語文',76);
1 row inserted

SQL> insert into score(name,kecheng,fenshu) values('李四','數學',90);
1 row inserted

SQL> insert into score(name,kecheng,fenshu) values('王五','語文',81);
1 row inserted

SQL> insert into score(name,kecheng,fenshu) values('王五','數學',100);
1 row inserted

SQL> insert into score(name,kecheng,fenshu) values('王五','英語',88);
1 row inserted

score表
  
SQL> select * from score;

NAME           KECHENG                 FENSHU


張三           語文                        81
張三           數學                        78
李四           語文                        76
李四          數學                    90
王五           語文                        81
王五           數學           100
王五           英語                        88
7 rows selected

方法一
  

SQL> select name from score group by name having min(fenshu)>80;
NAME


王五

方法二
  

SQL> select distinct name from score where name not in(
  2  select name from score
  3  where fenshu<80);

NAME

--------------------------------------------------
王五
  

2

刪除除了自動編號不同,其他都相同的學生冗余信息

准備數據
  

SQL> create table students1
  2  (
  3  自動編號 number(5),
  4  學號 number(10),
  5  姓名 varchar(40),
  6  課程編號 varchar(60),
  7  課程名稱 varchar(160),
  8  分數 number(3)
  9  );

Table created

SQL> insert into students1 values(1,2005001,'張三',0001,'數學',69);
1 row inserted

SQL> insert into students1 values(2,2005002,'李四',0001,'數學',69);
1 row inserted

SQL> insert into students1 values(3,2005001,'張三',0001,'數學',69);
1 row inserted

SQL> commit;

Commit complete

students1表
  
SQL> select * from students1;

 自動編號   學號 姓名     課程編號   課程名稱  分數

 ---------------------------------------- 

 1    2005001    張三      1      數學       69

 2    2005002    李四      1      數學       69

 3    2005001    張三      1      數學       69

  
方法一
  
SQL> delete from students1 where 自動編號 not in(
  2  select min(自動編號)
  3  from students1
  4  group by 學號,課程編號);

1 row deleted

方法二
  
SQL> delete from students1 where rowid not in(
  2  select min(rowid)
  3  from students1
  4  group by 學號,課程編號);

1 row deleted
  

3

一個叫team的表,里面只有一個字段name,一共有4條記錄,分別是abcd,對應4個球隊,現在4個球隊進行比賽,用一條sql語句顯示所有可能的比賽組合

team表


SQL> select * from team;
NAME

a
b
c
d

方法一,考慮主客場
  

SQL> select * from team a,team b
  2  where a.name<>b.name;
NAME      NAME
--------- ---------
a         b
a         c
a         d
b         a
b         c
b         d
c         a
c         b
c         d
d         a
d         b
d         c
12 rows selected
  
方法二,不考慮主客場
  
SQL> select * from team a,team b
  2  where a.name
   
   
   
           

4

請用sql語句實現,從testdb數據表中查詢出所有月份的發生額都比101科目相應月份的發生額
高的科目,

注意: testdb中有很多科目,都有1-12月份的發生額

testdb表
  
SQL> select * from testdb;

  ACCID OCCMONTH  DEBITOCCUR

------- -------- -----------

    101        1          34

    101        2          35

    101        3          36

    101        4          37

    102        1          44

    102        2          45

    102        3          46

    102        4          47

    103        1          44

    103        2          45

    103        3          36

    103        4          37

12 rows selected
  
方法一
  
SQL> select distinct accid from testdb where accid not in(

  2  select b.accid
  3  from testdb a,testdb b
  4  where a.accid='101'
  5  and b.accid<>a.accid
  6  and a.occmonth=b.occmonth
  7  and a.debitoccur>=b.debitoccur)
  8  and accid<>'101';

  ACCID

-------

    102
  

5

行轉列,將表a查詢為表b

表a
  
SQL> select * from a;

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

8 rows selected
  
表b
  
      YEAR         M1         M2         M3         M4

---------- ---------- ---------- ---------- ----------

      1991        1.1        1.2        1.3        1.4

      1992        2.1        2.2        2.3        2.4

  
方法一
  
SQL> select 1991 year,1.1 m1,1.2 m2,1.3 m3,1.4 m4 from dual

  2  union all

  3  select 1992 year,2.1 m1,2.2 m2,2.3 m3,2.4 m4 from dual;

      YEAR         M1         M2         M3         M4

---------- ---------- ---------- ---------- ----------

      1991        1.1        1.2        1.3        1.4

      1992        2.1        2.2        2.3        2.4
  
方法二
  
SQL> select year,
  2  max(case month when 1 then amount end) m1,
  3  max(case month when 2 then amount end) m2,
  4  max(case month when 3 then amount end) m3,
  5  max(case month when 4 then amount end) m4
  6  from a group by year;

YEAR         M1         M2         M3         M4

---- ---------- ---------- ---------- ----------

1991        1.1        1.2        1.3        1.4

1992        2.1        2.2        2.3        2.4

  
方法三
  
SQL> select * from
  2  (select year,amount m1 from a where month=1)
  3  natural join
  4  (select year,amount m2 from a where month=2)
  5  natural join
  6  (select year,amount m3 from a where month=3)
  7  natural join
  8  (select year,amount m4 from a where month=4);

YEAR   M1   M2   M3   M4

---- ---- ---- ---- ----

1991  1.1  1.2  1.3  1.4

1992  2.1  2.2  2.3  2.4

  

6

article_history表存放文章的修改記錄有以下字段,查詢出所有文章的最后修改人和修改時間

title文章標題
last_time修改時間
username修改人姓名
source文章來源
pin_name作者筆名

article_history
  

SQL> select * from article_history;
TITLE      LAST_TIME   USERNAME  SOURCE       PIN_NAME
--------------------------------------------------------
1          2019/12/4 1       s         cnblog       s
2          2019/12/4 1       l         cnblog       l
1          2019/12/4 1       s         cnblog       s
2          2019/12/4 1       l         cnblog       l
1          2019/12/5 9       s         cnblog       s
  
方法一
  
SQL> select title,username,last_time from article_history where 
(title,last_time) in(
  2  select title,max(last_time)
  3  from article_history
  4  group by title);

TITLE               USERNAME                LAST_TIME

---------------------------------------------------------

2    l     2019/12/4 1

1    s     2019/12/5 9

  

7

有兩個表AA和BB,均有key和value兩個字段,如果BB的key在AA中也有,就把BB中的value換為AA中對應的value

准備數據
  
SQL> create table aa
  2  (
  3  key varchar(100),
  4  value varchar(100)
  5  );

Table created



SQL> 

SQL> create table bb
  2  (
  3  key varchar(100),
  4  value varchar(100)
  5  );

Table created



SQL> 

SQL> insert into aa values('aa','gond');
1 row inserted

SQL> insert into aa values('bb','tend');
1 row inserted

SQL> insert into aa values('cc','ailie');
1 row inserted

SQL> insert into bb values('aa','geng');
1 row inserted

SQL> insert into bb values('bb','dlis');
1 row inserted

SQL> insert into bb values('dd','lista');
1 row inserted

  
aa表
  
SQL> select * from aa;

KEY                  VALUE

--------------------------------
aa                    gond
bb                    tend
cc                    ailie
  
bb表
  
SQL> select * from bb;

KEY                       VALUE

----------------------------------
aa                       geng
bb                       dlis
dd                       lista
  
方法一
  
SQL> update bb b
  2  set value=(select value from aa a where b.key=a.key)
  3  where exists(select 1 from aa a where b.key=a.key);

2 rows updated
  
方法二
  
SQL> merge into bb b
  2  using aa a
  3  on(b.key=a.key)
  4  when matched then
  5    update
  6    set b.value=a.value;

2 rows merged
  

8

什么是事務,事務有哪些特性,各個特性是什么意思

  • 一個commit,rollback就是一個事務
  • 原子性,一個事務,要么都執行,要么都回滾,是一個整體
  • 一致性,事務發生之前,所有人查詢數據都是一致的,事務發生之后,所有人查詢數據都是一致的
  • 隔離型,事務之間相互隔離,比如一個insert和另一個update,相互之間不影響
  • 持久性,事務發生之后,如果沒有別的事務,則數據不會再改變

9

有如下三個表

students表
  
SQL> select sno 學號,sname 姓名,class 所屬班級 from students;

學號       姓名                 所屬班級

---------- -------------------- -----

108        曾華                 95033

105        匡明                 95031

107        王麗                 95033

101        李軍                 95033

109        王芳                 95031

103        陸君                 95031

6 rows selected
  
courses表
  
SQL> select cno 課程編號,cname 課程名稱 from courses;

課程編號 課程名稱

----- --------------------

3-105 計算機導論

3-245 操作系統

6-166 數據電路

9-888 高等數學
  
scores表
  
SQL> select sno 學號,cno 課程編號,score 成績 from scores;

學號  課程編號                       成績

----- -------------------- ------------

103   3-245                        86.0

105   3-245                        75.0

109   3-245                        68.0

103   3-105                        92.0

105   3-105                        88.0

109   3-105                        76.0

101   3-105                        64.0

107   3-105                        91.0

108   3-105                        78.0

101   6-166                        85.0

107   6-106                        79.0

108   6-166                        81.0

12 rows selected
  

1使用標准sql嵌套語句查詢選修課程名稱為'計算機導論'的學員學號和姓名

方法
  
SQL> select sno,sname from students where sno in(
  2  select sno from scores where cno=(
  3  select cno from courses where cname='計算機導論'));

SNO        SNAME

---------- --------------------

103        陸君

105        匡明

109        王芳

101        李軍

107        王麗

108        曾華

6 rows selected
  

2使用標准sql嵌套語句查詢選修課程編號為'3-245'的學員姓名和班級

方法
  
SQL> select sname,class from students where sno in(
  2  select sno from scores where cno='3-245');

SNAME                CLASS

-------------------- -----

陸君                 95031

匡明                 95031

王芳                 95031
  

3使用標准sql嵌套語句查詢不選修課程編號為'6-166'的學員姓名和所屬單位

方法
  
SQL> select sname,class from students where sno not in(
  2  select sno from scores where cno='6-166');

SNAME                CLASS

-------------------- -----

匡明                 95031

陸君                 95031

王芳                 95031

王麗                 95033
  

4查詢選修了課程的學員人數

方法
  
SQL> select count(distinct sno) from scores;

COUNT(DISTINCTSNO)

------------------

                 6
  

5查詢選修課程沒有超過3門的學員學號和所屬班級

方法
  
SQL> select sno,class from students where sno in(
  2  select sno
  3  from scores
  4  group by sno having count(1)<=3);

SNO        CLASS

---------- -----

101        95033

105        95031

109        95031

103        95031

108        95033

107        95033

6 rows selected
  

10

查詢表aplus中存在id重復三次以上的記錄,寫出查詢語句

准備數據
  
SQL> create table aplus(
  2  id number(6)
  3  );

Table created



SQL> 

SQL> insert into aplus values(1);
1 row inserted

SQL> insert into aplus values(1);
1 row inserted

SQL> insert into aplus values(2);
1 row inserted

SQL> insert into aplus values(2);
1 row inserted

SQL> insert into aplus values(2);
1 row inserted

SQL> insert into aplus values(3);
1 row inserted

SQL> insert into aplus values(3);
1 row inserted

SQL> insert into aplus values(3);
1 row inserted

SQL> insert into aplus values(3);
1 row inserted
  
aplus表


SQL> select * from aplus;
ID

  1
  1
  2
  2
  2
  3
  3
  3
  3

9 rows selected

方法一
  
SQL> select id
  2  from aplus
  3  group by id
  4  having count(1)>3;

     ID

-------

      3
  

11

表sales(pid,sale_date,amount)分別代表產品id,銷售日期,銷售量. 查詢出各個產品每天的銷售占當月已銷售額的比例

注意當月已銷售額是指本月第一天到現在的總和

准備數據
  
SQL> create table sales(
  2  pid number(10),
  3  sale_date date,
  4  amount number(16)
  5  );

Table created
SQL> insert into sales 
values(1,to_date(20191201,'yyyy/mm/dd'),7789);

1 row inserted

SQL> insert into sales 
values(2,to_date(20101201,'yyyy/mm/dd'),2589);

1 row inserted

SQL> insert into sales 
values(1,to_date(20191202,'yyyy/mm/dd'),6921);

1 row inserted

SQL> insert into sales 
values(2,to_date(20101202,'yyyy/mm/dd'),3501);

1 row inserted

SQL> insert into sales 
values(1,to_date(20191203,'yyyy/mm/dd'),7249);

1 row inserted

SQL> insert into sales 
values(2,to_date(20101203,'yyyy/mm/dd'),3511);

1 row inserted

  
sales表
  
SQL> select * from sales;
        PID SALE_DATE              AMOUNT
----------- ----------- -----------------
          1 2019/12/1                7789
          2 2010/12/1                2589
          1 2019/12/2                6921
          2 2010/12/2                3501
          1 2019/12/3                7249
          2 2010/12/3                3511
6 rows selected
  
方法一
  
SQL> select pid,sale_date,amount,amount/sum_a
  2  from (select a.pid,a.sale_date,a.amount,
  3  (select sum(b.amount) from sales b where a.pid=b.pid) sum_a
  4  from sales a);

        PID SALE_DATE              AMOUNT AMOUNT/SUM_A

----------- ----------- ----------------- ------------

          1 2019/12/1                7789 0.3547064984

          2 2010/12/1                2589 0.2696594104

          1 2019/12/2                6921 0.3151782868

          2 2010/12/2                3501 0.3646495156

          1 2019/12/3                7249 0.3301152147

          2 2010/12/3                3511 0.3656910738

6 rows selected
  


免責聲明!

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



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