第二章 select 查询

/查看当前用户的表及部分字段信息/
select TABLE_NAME,
COLUMN_NAME,
DATA_TYPE,
DATA_LENGTH,
NULLABLE
from USER_TAB_COLUMNS
where
TABLE_name = ‘BONUS’;
/查看当前用户的表(视图??)/
Select * from tab;
/查看当前用户的表及字段信息/
Select * from user_tab_columns
where table_name =’EMP’;

/使用算数计算/
SELECT ename , sal , 12*sal+100 year_sal
FROM emp e
where sal>2000-200;

/*1、查询员工的姓名,工作,月薪等信息
2、查询部门表中20部门的信息
3、查询20部门的员工信息
4、给自己创建的用户授予查询scott用户
的emp表的权限,并进行查询操作*/
select ename,job,sal from emp;
select * from dept where deptno = 20;
select * from emp where deptno = 20;
/通过system赋给scott创建用户的权限/
grant create user to scott ;
/通过scott用户创建了cici的用户/
create user cici identified by 123456;
/通过system赋给cici登录权限/
grant create session to cici;
/scott赋予cici查询emp表的权限/
grant select on emp to cici;
/用户cici查询scott用户下的表的时候,记得带上用户名/
select * from scott.emp;

/*1、查询工作为manager的员工的信息
2、查询员工的年薪,要求年薪为 月薪*12+奖金
3、查询SMITH的工资信息
4、创建一个角色,使该角色拥有登录的权限,并创建用户,
将拥有登录权限的角色授予新创建的用户,进行用户的登录,
删除用户,删除角色*/
1.
select * from emp where job = ‘MANAGER’;
2、
select ename, sal * 12 +comm as year_money from bonus ;
select ename, sal * 12 + nvl(comm,0)as year_money ,sal,comm from emp;

3、
select ename,sal,comm from emp where ename = ‘SMITH’;

/空值的条件/
select * from emp where comm is not null;
select * from emp where comm is null;

/别名/
SELECT ename AS 姓名 , sal salary
FROM emp t1
where t1.ename=’SMITH’;
/等同于 /
SELECT ename AS name , sal salary
FROM emp where emp.ename=’SMITH’;

/||连接,注意连接后的结果中如果有’,需要写”/
SELECT ename || ‘的工资是:’ || sal FROM emp t1 ;
SELECT ename || ‘’s’ || sal FROM emp t1 ;
SELECT ename || ”’s’ || sal FROM emp t1 ;

/去重/

select distinct job from emp;
select distinct deptno from emp;
select distinct deptno,job from emp order by deptno;

SELECT ename 姓名, deptno 部门编号, sal 薪水
FROM emp;
SELECT * FROM emp WHERE sal>1500;
SELECT * FROM emp WHERE comm is null;
SELECT ename, empno, deptno FROM emp WHERE job= ‘CLERK’;
SELECT t.*,round(sal/30) 日薪金 FROM emp t;
SELECT * FROM emp
WHERE sal is null
and deptno=20;
SELECT * FROM emp WHERE job=’SALESMAN’;
SELECT * FROM emp WHERE deptno=’10’;

猜你喜欢

转载自blog.csdn.net/weixin_42800008/article/details/81222909
今日推荐