查询语句练习

Posted 将心比心

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查询语句练习相关的知识,希望对你有一定的参考价值。


1、创建表my_emp和emp表具有相同的结构和记录。
           drop table my_emp;
    create table my_emp(
                EMPNO    NUMBER(4)    not null,                          
                ENAME    VARCHAR2(10) ,                         
                JOB      VARCHAR2(9) ,                         
                MGR      NUMBER(4)  ,                          
                HIREDATE DATE    ,                             
                SAL      NUMBER(7,2)  ,                         
                COMM     NUMBER(7,2) ,                         
                DEPTNO   NUMBER(2)                          
                 )


insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7369, ‘SMITH‘, ‘CLERK‘, 7902, to_date(‘17-12-1980‘, ‘dd-mm-yyyy‘), 800.00, null, 20);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7499, ‘ALLEN‘, ‘SALESMAN‘, 7698, to_date(‘20-02-1981‘, ‘dd-mm-yyyy‘), 1600.00, 300.00, 30);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7521, ‘WARD‘, ‘SALESMAN‘, 7698, to_date(‘22-02-1981‘, ‘dd-mm-yyyy‘), 1250.00, 500.00, 30);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, ‘JONES‘, ‘MANAGER‘, 7839, to_date(‘02-04-1981‘, ‘dd-mm-yyyy‘), 2975.00, null, 20);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, ‘MARTIN‘, ‘SALESMAN‘, 7698, to_date(‘28-09-1981‘, ‘dd-mm-yyyy‘), 1250.00, 1400.00, 30);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, ‘BLAKE‘, ‘MANAGER‘, 7839, to_date(‘01-05-1981‘, ‘dd-mm-yyyy‘), 2850.00, null, 30);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, ‘CLARK‘, ‘MANAGER‘, 7839, to_date(‘09-06-1981‘, ‘dd-mm-yyyy‘), 2450.00, null, 10);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, ‘SCOTT‘, ‘ANALYST‘, 7566, to_date(‘19-04-1987‘, ‘dd-mm-yyyy‘), 3000.00, null, 20);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, ‘KING‘, ‘PRESIDENT‘, null, to_date(‘17-11-1981‘, ‘dd-mm-yyyy‘), 5000.00, null, 10);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, ‘TURNER‘, ‘SALESMAN‘, 7698, to_date(‘08-09-1981‘, ‘dd-mm-yyyy‘), 1500.00, 0.00, 30);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, ‘ADAMS‘, ‘CLERK‘, 7788, to_date(‘23-05-1987‘, ‘dd-mm-yyyy‘), 1100.00, null, 20);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, ‘JAMES‘, ‘CLERK‘, 7698, to_date(‘03-12-1981‘, ‘dd-mm-yyyy‘), 950.00, null, 30);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, ‘FORD‘, ‘ANALYST‘, 7566, to_date(‘03-12-1981‘, ‘dd-mm-yyyy‘), 3000.00, null, 20);

insert into my_emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, ‘MILLER‘, ‘CLERK‘, 7782, to_date(‘23-01-1982‘, ‘dd-mm-yyyy‘), 1300.00, null, 10);
2、给myemp的empno列添加主建约束。
alter table my_emp add constraint my_emp_empno_pk primary key(empno);

3、给myemp添加一条记录。
insert into my_emp
values (7935, ‘WANG‘, ‘CLERK‘, 7782, to_date(‘23-01-1982‘, ‘dd-mm-yyyy‘), 1300.00, null, 10);

4、给myemp添加一条记录只有empno,ename,mgr,sal,deptno有值,其他列为空。

insert into my_emp(EMPNO, ENAME,  MGR, SAL, DEPTNO) values (
 7936,‘XI‘,7782,1400,10);
5、显示所有薪金高于公司均薪金的人。
select *from my_emp where sal>(select avg(sal)from my_emp)  ;



    
6、给所有10部门的经理(MANAGER)和20部门的职员(CLERK),增加薪金10%。
update (select * from my_emp where deptno=10 and job=‘MANAGER‘or deptno=‘20‘and job=‘CLERK‘)
 set sal=sal*(1+10/100);

7、使用EMP,DEPT表显示所有销售部‘SALES‘的员工。
select * from emp where deptno=(select deptno from dept where dname=‘SALES‘);

8、删除DEPT中没有员工的部门。
 delete dept where deptno=(select deptno from dept where deptno not in(select deptno from emp));

9、删除雇佣年限低于20年的员工。
delete emp where hiredate=(select hiredate from emp where (sysdate-hiredate)/365<20 );

10、显示薪金最高的3位员工
select * from(select a.*,rownum rn from (select * from emp order by sal desc) a) b where b.rn<=3;

11、显示10部门薪金前二名的员工
 select* from(select a.*,rownum rn from (select *from emp where deptno=10 order by sal desc) a) b where b.rn<=2;

以上是关于查询语句练习的主要内容,如果未能解决你的问题,请参考以下文章

MySql练习---[SQL查询语句练习]

数据库经典查询语句与练习题

MySQL 查询语句练习1

MySQL查询语句练习题

sql查询语句练习

SQL语句练习