oracle练习题后15个

31,32题更正:

 1 SQL> --31、 查询所有教师和同学的name、sex和birthday.
 2 SQL> select sname, ssex, sbirthday from student
 3   2  union all
 4   3  select tname, tsex, tbirthday from teacher;
 5 SNAME    SSEX SBIRTHDAY
 6 -------- ---- -----------
 7 曾华     男   1977/9/1
 8 匡明     男   1975/10/2
 9 王丽     女   1976/1/23
10 李军     男   1976/2/20
11 王芳     女   1975/2/10
12 陆君     男   1974/6/3
13 李诚     男   1958/12/2
14 张旭     男   1969/3/12
15 王萍     女   1972/5/5
16 刘冰     女   1977/8/14
17 10 rows selected
18 
19 SQL> --32、查询所有“女”教师和“女”同学的name、sex和birthday.
20 SQL> select sname, ssex, sbirthday from student where ssex = ''
21   2  union all
22   3  select tname, tsex, tbirthday from teacher where tsex = '';
23 SNAME    SSEX SBIRTHDAY
24 -------- ---- -----------
25 王丽     女   1976/1/23
26 王芳     女   1975/2/10
27 王萍     女   1972/5/5
28 刘冰     女   1977/8/14
  1 SQL> --31、 查询所有教师和同学的name、sex和birthday.
  2 SQL> select sname, ssex, sbirthday from student;
  3 SNAME    SSEX SBIRTHDAY
  4 -------- ---- -----------
  5 曾华     男   1977/9/1
  6 匡明     男   1975/10/2
  7 王丽     女   1976/1/23
  8 李军     男   1976/2/20
  9 王芳     女   1975/2/10
 10 陆君     男   1974/6/3
 11 6 rows selected
 12 
 13 SQL> select tname, tsex, tbirthday from teacher;
 14 TNAME TSEX TBIRTHDAY
 15 ----- ---- -----------
 16 李诚  男   1958/12/2
 17 张旭  男   1969/3/12
 18 王萍  女   1972/5/5
 19 刘冰  女   1977/8/14
 20 
 21 SQL> 32-32、查询所有“女”教师和“女”同学的name、sex和birthday.
 22   2  ;
 23 32-32、查询所有“女”教师和“女”同学的name、sex和birthday.
 24 ORA-00900: 无效 SQL 语句
 25 
 26 SQL> select sname ssex sbirthday from student where ssex = '';
 27 select sname ssex sbirthday from student where ssex = ''
 28 ORA-00923: 未找到要求的 FROM 关键字
 29 
 30 SQL> select sname, ssex, sbirthday from student where ssex = '';
 31 SNAME    SSEX SBIRTHDAY
 32 -------- ---- -----------
 33 王丽     女   1976/1/23
 34 王芳     女   1975/2/10
 35 
 36 SQL> select tname, tsex, tbirthday from teacher where tsex = '';
 37 TNAME TSEX TBIRTHDAY
 38 ----- ---- -----------
 39 王萍  女   1972/5/5
 40 刘冰  女   1977/8/14
 41 
 42 SQL> --33、 查询成绩比该课程平均成绩低的同学的成绩表。
 43 SQL> select degree from score where degree < (select avg(degree) from score);
 44 DEGREE
 45 ------
 46   75.0
 47   68.0
 48   76.0
 49   64.0
 50   78.0
 51   79.0
 52 6 rows selected
 53 
 54 SQL> --34、 查询所有任课教师的Tname和Depart.
 55 SQL> select tname, depart from teacher;
 56 TNAME DEPART
 57 ----- ----------
 58 李诚  计算机系
 59 张旭  电子工程系
 60 王萍  计算机系
 61 刘冰  电子工程系
 62 
 63 SQL> --35 、 查询所有未讲课的教师的Tname和Depart. 
 64 SQL> select tname, depart from teacher where tname not in
 65   2  (select tname from teacher te, score sc, course co where te.tno = co.tno and co.cno = sc.cno);
 66 TNAME DEPART
 67 ----- ----------
 68 刘冰  电子工程系
 69 
 70 SQL> --36、查询至少有2名男生的班号。
 71 select  class from student where ssex = '' group by class having count (1) >=2; 
 72   2  
 73 SQL> select  class from student where ssex = '' group by class having count (1) >=2; 
 74 CLASS
 75 -----
 76 95033
 77 95031
 78 
 79 SQL> --37、查询Student表中不姓“王”的同学记录。
 80 SQL> select * from student where sname not in '王%';
 81 SNO SNAME    SSEX SBIRTHDAY   CLASS
 82 --- -------- ---- ----------- -----
 83 108 曾华     男   1977/9/1    95033
 84 105 匡明     男   1975/10/2   95031
 85 107 王丽     女   1976/1/23   95033
 86 101 李军     男   1976/2/20   95033
 87 109 王芳     女   1975/2/10   95031
 88 103 陆君     男   1974/6/3    95031
 89 6 rows selected
 90 
 91 SQL> select * from student where sname not like '王%';
 92 SNO SNAME    SSEX SBIRTHDAY   CLASS
 93 --- -------- ---- ----------- -----
 94 108 曾华     男   1977/9/1    95033
 95 105 匡明     男   1975/10/2   95031
 96 101 李军     男   1976/2/20   95033
 97 103 陆君     男   1974/6/3    95031
 98 
 99 SQL> --38、查询Student表中每个学生的姓名和年龄。
100 SQL> --select sysdtaeselect sbirthday  from student;
101 SQL> 
102 SQL> --39
103 SQL> select max(sbirthday) from student;
104 MAX(SBIRTHDAY)
105 --------------
106 1977/9/1
107 
108 SQL> --39、查询Student表中最大和最小的Sbirthday日期值。
109 SQL> select min(sbirthday) from student;
110 MIN(SBIRTHDAY)
111 --------------
112 1974/6/3
113 
114 
115 
116 SQL> --40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
117 SQL> select * from student order by class desc, sbirthday;
118 SNO SNAME    SSEX SBIRTHDAY   CLASS
119 --- -------- ---- ----------- -----
120 107 王丽     女   1976/1/23   95033
121 101 李军     男   1976/2/20   95033
122 108 曾华     男   1977/9/1    95033
123 103 陆君     男   1974/6/3    95031
124 109 王芳     女   1975/2/10   95031
125 105 匡明     男   1975/10/2   95031
126 6 rows selected
127 
128 SQL> --41、查询“男”教师及其所上的课程。
129 SQL> select tname, cname from teacher te, course co where te.tno = co.tno and te.tsex = '';
130 TNAME CNAME
131 ----- ----------
132 李诚  操作系统
133 张旭  数字电路
134 
135 SQL> --42、查询最高分同学的Sno、Cno和Degree列。
136 SQL> select sno, cno, degree from score where degree >= all (select degree from score);
137 SNO CNO   DEGREE
138 --- ----- ------
139 103 3-105   92.0
140 
141 SQL> --43、查询和“李军”同性别的所有同学的Sname.
142 SQL> select sname from student where ssex = (select ssex from student where ssname = '李军');
143 select sname from student where ssex = (select ssex from student where ssname = '李军')
144 ORA-00904: "SSNAME": 标识符无效
145 
146 SQL> select sname from student where ssex = (select ssex from student where sname = '李军');
147 SNAME
148 --------
149 曾华
150 匡明
151 李军
152 陆君
153 
154 SQL> --44、查询和“李军”同性别并同班的同学Sname.
155 SQL> select sname from student where ssex = (select ssex from student where sname = '李军') and class = (select class from 
156 
157 student where sname = '李军');
158 SNAME
159 --------
160 曾华
161 李军
162 
163 SQL> --45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
164 SQL> select sname, degree from student st, score sc, course co where st.sno = sc.sno and sc.cno = co.cno and co.cname ='165 
166 算机导论'
167   2  and ssex = '';
168 SNAME    DEGREE
169 -------- ------
170 曾华       78.0
171 匡明       88.0
172 李军       64.0
173 陆君       92.0
174 
175 SQL> --20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
176 
177 SQL> select * from score where (degree < any(select degree from score)) and sno in(select sno from score group by sno 
178 
179 having count(1) > 1);
180 SNO CNO   DEGREE
181 --- ----- ------
182 101 3-105   64.0
183 101 6-166   85.0
184 105 3-105   88.0
185 105 3-245   75.0
186 109 3-105   76.0
187 109 3-245   68.0
188 103 3-245   86.0
189 108 3-105   78.0
190 108 6-166   81.0
191 107 3-105   91.0
192 107 6-166   79.0
193 11 rows selected
194 
195 SQL> --38、查询Student表中每个学生的姓名和年龄。
196 SQL> select sname, trunc(((months_between(sbirthday , (select trunc(sysdate) from dual))) / (-12)), 0) as 年龄 from student;
197 SNAME            年龄
198 -------- ----------
199 曾华             38
200 匡明             40
201 王丽             39
202 李军             39
203 王芳             40
204 陆君             41
205 6 rows selected
原文地址:https://www.cnblogs.com/dirgo/p/4920294.html