1. 显示系统时间(注:日期+时间)
a) select to_char(sysdate,'yyyy-mm-dd hh:mi:ss')
b) from dual第一个显示系统的时间,转成char类型的sysdateselect to_char(sysdate,'yyyy:mi:ss') from dual我就不写年月日这三个字了
我们这里应该用字符标起来
2. 查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
a) select employee_id,last_name,salary,salary*1.2 "new salary"
b) from employeesselect employee_id,last_name,salary,以及工资提高百分之二十的结果,那就是salary乘以1.2呗,起个名字,当你的新名字中间有空格的时候,一定要使用双引号的,new salary,from employeesselect employee_id,last_name,salary,salary*1.1 "new salary" from employees
3. 将员工的姓名按首字母排序,并写出姓名的长度(length)
a) select last_name,length(last_name)
b) from employees
c) order by last_name ascselect,来一个last_name,姓名的长度,那就length(),这是一个函数,length(last_name),按首字母排序,加上一个order by,last_name,你写上asc也行,不写默认的也是ascselect last_name,length(last_name) from employees order by last_name asc
4. 查询各员工的姓名,并显示出各员工在公司工作的月份数(worked_month)。
a) select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b) from employees查询各员工的姓名,并显示各员工在公司工作的月份数,工作多少个月select last_name,hire_date,工作的月份数,那就判断一下当前的日期,叫系统日期,跟hire_date之间,相差的时间数,工作的月份,months_between,sysdate,我们需要把sysdate写前面,hire_date,这是我们算出来的工作月份数,起个别名,workded_month,这个时候我的月份数,是由好多位的小数点,如果你想就去保留一位,你可以在外面再包一层,round也行,逗号,保留一位小数,这样写,起个别名select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month from employees
这是他的雇佣日期,这是他来公司一共工作多少天,相减如果有小数的话,我们接着往下看5. 查询员工的姓名,以及在公司工作的月份数(worked_month),并按月份数降序排列
a) Select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b) from employees
c) order by workded_month desc查询员工的姓名,以及在公司的工作月份数,跟那个差不多,并按月份数降序排列,那跟这个题目变化不大,只需要在后面加上一个order by,按照一个列的别名进行排序,他的降序排,通过它descselect last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month from employees order by workded_month desc
下面就有一个Grade,如果没有写的,Grade就是null,自己写的时候尽量写的规范一点,放在word里面格式需要稍微调一下,你一定要把这些例子亲自去写一写,并不难,但是呢,不敲的话容易忘,下面我们用case重写一遍8. 将第7题的查询用case函数再写一遍。
a) select last_name "Last_name",job_id "Job_id",case job_id when 'AD_PRES'then 'A'
b) when 'ST_MAN' then 'B'
c) when 'IT_PROG' then 'C'
d) when 'SA_REP' then 'D'
e) when 'ST_CLERK' then'E' end "Grade"
f) from employeescase他,然后when,逗号不要,然后改成then,逗号去掉,小括号右括号相当于一个end,起个别名select last_name "Last_name",job_id "Job_id", case job_id when 'AD_PRES' then 'A'when 'IT_MAN' then 'B'when 'IT_PROG' then 'C'when 'SA_REP' then 'D'when 'ST_CLERK' then 'E' end 'Grade'from employees