一、简单查询
1.检索所有教师的姓名、职称、工资、参加工作时间等信息。
select name,title,wage,hrie_date from teachers;
2.检索所有教师的姓名、职称、工资、参加工作时间等信息,日期格式按”YYYY-MM-DD”输出.
select name,title,wage,TO_CHAR(hire_date,’YYYY-MM-DD’) from teachers;
3.检索students表中所有行,所有列的信息。要求(*)
select * from students;
4.检索students表,显示学生专业列(不带DISTINCT关键字)
select specialty from students
5.检索students表,显示学生专业列(带DISTINCT关键字)
select distinct specialty from students;
6.检索students表,选择name,dob两列,并分别使用别名姓名和生日。
select name as姓名,dob as生日from students;
7.检索students表,选择name、dob两列,使用字符串连接,形成学生生日清单。
如:王晓芳生日是:07-5月-88
select name||’生日是:’||dob as学生生日清单 from students;
8.计算所有教师的月总收入
select name as姓名,bonus+wage as月总收入from teachers;
9.计算所有教师的月总收入(bonus+wage)。
注意:当某位教师的bonus不为NULL时,该教师的月总收入显示为(bonus+wage)的 计算结果;但当某位教师的bonus为null时,该教师月总收入也为null。
使用NVL(expr1,expr2)函数解决这个问题,如果expr1为空,取expr2的值,否则取本身的值。
select name as ‘姓名’ ,NVL(bonus,0)+wage as ‘月总收入’ from teachers;
10.计算所有教师的月总收入,并利用函数NVL2()处理bonus出现的情况
NVL2(expr1,expr2,expr3):如果参数表达式expr1值为null,则NVL2()函数返回参数表达式expr3的值;如果参数表达式expr1的值不为null,则NVL2()函数返回参数表达式expr2的值。
select name as姓名,NVL2(bonus,bonus+wage,wage)as月总收入from teachers;
11.计算所有教师的月总收入,并利用函数coalesce()处理bonus出现的情况
coalesce(expr1[,expr2]…):返回参数列表中的第一个非空值。如果所有的表达式都是空值,最终将返回一个空值。
select name as姓名,coalesce(bonus,bonus+wage,wage)as月总收入from teachers;
二、条件查询
1.检索teachers表中工资大于等于2000元的教师信息
select name,hire_date,title,wage from teachers where wage>=2000;
2.检索students表中计算机专业的学生信息
select student_id,name,specialty,dob
from students
3.检索students表中1990年1月1日以前出生的学生信息
select student_id,name,specialty,dob from students where dob<’1-1月-1990’;
4.检索teachers表中获得奖金为500和600元的教师信息
5.检索students表中1989年12月26日和1990年8月10日出生的学生信息。
select student_id,name,specialty,dob
from students where dob in(’08-10月-1990’,’26-12月-1989’);
6.检索teachers表中获得奖金在500~600元之间的教师信息。
select name,hire_date,title,bonus
7.检索students表中所有姓王的学生信息
select student_id,name,specialty,dob
from students
where name like ‘王%’;
8.检索teachers表中奖金未定的教师信息
select name,hire_date,title,bonus from teachers where bonus is null;
9.检索teachers表中无职称的教师信息
select name,hire_date,title,bonus from teachers where title is null;
10.检索students表中出生日期未知的学生信息
select student_id,name,specialty,dob
from students
where dob is null;
11.检索students表中计算机专业男生的学生信息
select student_id,name,sex,specialty
from students
where specialty=’计算机’ AND sex=’男’;
12.检索students表中计算机和自动化专业的学生信息
select student_id,name,sex,specialty
from students
where specialty=’计算机’ or specialty=’自动化’;
13.检索students表中不是计算机专业的学生信息
select student_id,name,sex,specialty
from students
where not specialty=’计算机’;
14.检索students表中欧阳春岚和高山以外的学生信息
select student_id,name,specialty,dob
from students where name not in(‘欧阳春岚’,’高山’)
15.检索students表中在1989年1月1日于1990年1月1日之间出生的学生信息
select student_id,name,specialty,dob
from students
where dob between ‘1-1月-1989’ and ‘1-1月-1990’;
16.检索students表中不姓张的学生信息
select student_id,name,specialty,dob
from students
where name not like ‘张%’;
17.检索teachers表中奖金已经确定的教师信息
select name,hire_date,title,bonus
from teachers
where bonus is not null;
18.检索students表中计算机专业女生和机电工程专业男生的学生信息的学生信息
select student_id,name,sex,specialty from students
where specialty=’计算机’ AND sex=’女’
OR specialty=’机电工程’ AND sex=’男’;
19.检索teachers表中不是工程师,并且2002年1月1日前参加工作,工资低于3000元的教师信息
select name,hire_date,title,bonus,wage
from teachers
where not title=’工程师’ and hire_date<’1-1月-2002’ and wage<3000;
三、记录排序
1.按工资由小到大的顺序检索teachers表。
select name,hire_date,title,bonus,wage
from teachers order by wage ASC;
2.按工资由小到大的顺序检索teachers表(升序排序可省略ASC)
select name,hire_date,title,bonus,wage
from teachers order by wage;
3.按学生姓名降序检索students表(使用列序号)。
select student_id,name,specialty,dob
from students order by 2 desc;
4.按出生日期降序检索students表(使用列别名)
select name as姓名,dob as出生日期
from students order by出生日期desc;
5.按专业、姓名升序检索students表
select student_id,name,specialty,dob
from students
order by specialty ,name ;
6.按专业升序,姓名降序检索students表。
select student_id,name,specialty,dob
from students order by specialty,name desc;
四、分组查询
1.计算教师的平均工资。
select avg(wage) from teachers;
2.统计全体学生的人数。
select count(*) from students;
3.找出全体学生中最大的及最小的出生日期。
select max(dob) ,min(dob) from students;
4.求出全体教师工资总额
select sum(wage) from teachers;
5.按系部号对teachers表进行分组
select department_id from teachers group by department_id;
6.按系部号及职称对teachers表进行分组
select department_id , title
from teacher
group by department_id,title;
7.查询每一个系部的教师工资最大值及工资最小值
select department_id , max(wage) , min(wage)
from teachers
group by department_id;
8.求每一个系部的教师工资总和和工资平均值
select department_id,sum(wage) ,avg(wage)
from teachers
group by department_id;
10.求每一个系部的教师人数
select department_id , count(*)
from teachers
group by department_id;
11.求出每一个系部工资在1000元以上的教师工资平均值
select department_id , avg(wage)
from teachers
where wage>1000
group by department_id;
12.检索平均工资高于2200元的系部,显示系部号、平均工资
select department_id,avg(wage) from teachers
group by department_id
having avg(wage)>2200;
13.在工资低于3000元的教师中检索平均工资高于2000元的系部,显示系部号、平均工资。
select department_id,avg(wage)
from teachers
where wage<3000
group by department_id
having avg(wage)>2000;
14.在工资低于3000元的教师中检索平均工资高于2000元的系部,显示系部号、平均工资,并将显示结果按平均工资升序排列
select department_id,avg(wage) from teachers
where wage<3000
group by department_id
having avg(wage)>=2000
order by 2;