基本SQL SELECT语句
1. 下面的语句是否可以执行成功
select ename , job , sal as salary from emp;
2. 下面的语句是否可以执行成功
select * from emp;
3. 找出下面语句中的错误
select empno , ename sal * 12 ANNUAL SALARY from emp;
修改后
select empno , ename ,sal * 12 "ANNUAL SALARY" from emp;
4. 显示表dept的结构,并查询其中的全部数据
SQL> desc dept;
Name Type Nullable Default Comments
------ ------------ -------- ------- --------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14) Y
LOC VARCHAR2(13) Y
SQL>
5. 显示出表emp中的不重复的岗位job
SELECT DISTINCT JOB FROM EMP;
6. 连接表emp的全部列,各个列之间用逗号连接,列头显示成OUT_PUT(提示:使用连接符||)
select EMPNO|| ',' || ENAME || ',' || JOB || ',' || MGR || ',' || HIREDATE || ',' || SAL || ',' || COMM || ',' || DEPTNO as "OUT_PUT" from emp;
过滤和排序数据
1. 查询工资大于1600的员工姓名和工资
select ename, sal from emp where sal>1600;
2. 查询员工号为7369的员工的姓名和部门号
select empno,ename,deptno from emp where empno=7369;
3. 选择工资不在4000到5000的员工的姓名和工资
select ename,sal from emp where sal not between 4000 and 5000;
4. 选择雇用时间在1981-2-19到1981-12-3之间的员工姓名,JOB和雇用时间HIREDATE
Select ename, job, hiredate from emp where hiredate between to_date('1981-2-19','YYYY-mm-dd') and to_date('1981-12-3','YYYY-mm-dd');
5. 选择在20和30号部门工作的员工姓名和部门号
select ename,deptno from emp where deptno in(20,30);
6. 选择在1987年雇用的员工的姓名和雇用时间
Select ename,hiredate
from emp
where hiredate between to_date('1987-1-1','YYYY-mm-dd')
and to_date('1987-12-31','YYYY-mm-dd');
7. 选择公司中没有管理者的员工姓名及JOB
select ename,job
from emp
where mgr is null;
8. 选择公司中有奖金的员工姓名,工资和奖金
select ename,sal,comm
from emp
where comm is not null
and comm>0;
9. 选择员工姓名的第三个字母是A的员工姓名
select ename from emp
where ename like '__A%';
10. 选择姓名中有字母A和E的员工姓名
select ename from emp
where ename like '%A%' and ename like '%E%' ;
单行函数
1. 显示系统时间
select sysdate from emp;
2. 查询员工表emp中员工号empno,姓名ename,工资sal,以及工资提高百分之20%后的结果
select empno,ename,sal,sal*0.2+sal from scott.emp
3. 将员工的姓名按首字母排序,并写出姓名的长度(length)
select ename from scott.emp order by substr(ename,1,1)
4. 查询各员工的姓名ename,并显示出各员工在公司工作的月份数(即:与当前日期比较,该员工已经工作了几个月)。
select ename,hiredate,round((sysdate-hiredate)/30) from scott.emp order by ename
5. 查询员工的姓名和工资,按下面的形式显示
Name |
Salary |
SMITH |
$$$$$$$$$$24000 |
select ename,sal,lpad(sal,10,'$') from scott.emp
6. 查询员工的姓名ename和工资数sal,条件限定为工资数必须大于1200,并对查询结果按月份数降序方式进行排列
select to_char(hiredate,'mm') as monthorder from scott.emp
where comm>1200
order by monthorder
7. 做一个查询,产生下面的结果
select ename || ' earns ' || to_char(sal,'$99,999') || ' monthly but wants ' || to_char(sal*3,'$99,999') "Dream Salary"
from scott.emp
<enamename> earns <sal> monthly but wants <sal*3>
Dream Salary |
King earns $24000 monthly but wants $72000
|
8. 做一个查询,产生类似下面的结果
Ename |
HireDate |
reiew |
SMITH |
1980-12-17 |
1980年12月17日 |
select ename,to_char(hiredate,'yyyy-mm-dd') as hiredate,
to_char(hiredate,'YYYY"年"MONTH"月"DD"日"' ) as review
from scott.emp
9. 使用decode函数,按照下面的条件:
job grade
PRESIDENT A
MANAGER B
ANALYST C
SALESMAN D
CLERK E
产生类似下面形式的结果
ENAME |
Job |
Grade |
SMITH |
CLERK |
E |
select ename,job,
DECODE(job, 'PRESIDENT', 'A',
'MANAGER', 'B',
'ANALYST', 'C',
'SALESMAN', 'D',
'CLERK', 'E'
)
Grade
from scott.emp
10. 将第9题的查询用case函数重新实现。
select ename,job,
CASE job WHEN 'PRESIDENT' THEN 'A'
WHEN 'MANAGER' THEN 'B'
WHEN 'ANALYST' THEN 'C'
WHEN 'SALESMAN' THEN 'D'
WHEN 'CLERK' THEN 'E'
END "Grade"
from scott.emp