oracle database sql practice

Reference address: http://blog.csdn.net/rulon147/article/details/29599329
1. select * from emp;


2. select empno, ename, job from emp;


3. select empno number, ename name, job job from emp;


4. select job from emp;


5. select distinct job from emp;


6. select distinct empno, job from emp;
Description: Because the employee number is not repeated, it is proved that all the columns are not repeated, so the repetition cannot be eliminated 7.


Query out the employee's number, name, and job, but the format is displayed: the employee whose ID is: 7369, whose name is: smith, and whose job is: clear
select 'the employee whose ID is: ' || empno || ' , Name is: ' || ename || ', Job is: ' || job from emp;


8. Find the name and annual salary of each employee
select ename, sal * 12 income from emp;


9. Find the salary greater than 1500 All employee information of
select * from emp where sal > 1500;


10. Query the information of employees who can get bonus monthly
select * from emp where comm is not null;


11. Query the information of employees without bonuses
select * from emp where comm is not null;


12. Query the information of employees whose basic salary is greater than 1500 and can receive bonuses
select * from emp where sal > 1500 and comm is not null;


13. Query the information Information of employees whose basic salary is greater than 1500 or who can receive bonuses
select * from emp where sal > 1500 or comm is not null;


14. Query the information of employees whose basic salary is not more than 1500 or who cannot receive bonuses
select * from emp where not(sal > 1500 and comm is not null);


15. Query the information of all employees whose basic salary is greater than 1500, but less than 3000 select * from emp where sal >= 1500 and sal <= 3000; select * from emp where sal between 1500 and 3000; 17. Query out all employee information hired in 1981 (January 1, 1981 to employees employed between December 31, 1981)









select * from emp where hiredate between 'January-January-81' and '31-December-81';


18. Request to query the employee information whose name is smith
select * from emp where ename = 'SMITH';


19. Request Query the specific information of employees whose employees are 7369, 7499, 7521
select * from emp where empno = 7369 or empno = 7499 or empno = 7521;
select * from emp where empno in(7369, 7499, 7521);


20. Request query Get the specific information of the employees whose employees are not 7369, 7499, 7521
select * from emp where empno not in(7369, 7499, 7521);


21. Ask for the information of the employees whose names are smith, allen, king
select * from emp where ename in('SMITH', 'ALLEN', 'KING');


22. Query all employee information whose second letter contains "M"
        select * from emp where ename like '_M%';


23. Query the employee information whose name contains the letter M
select * from emp where ename like '%M%';


24. Query the information of employees hired in 1981
select * from emp where hiredate like '%81%';


25. Query the information of employees whose salary contains 5
select * from emp where sal like '%5%';


26. Query employee information whose employee number is not 7369
select * from emp where empno != 7369;
select * from emp where empno <> 7369;


27. Request to sort by salary from low to high
select * frm emp order by sal;
select * from emp order by sal asc;


28. Request to sort by salary from high to low
select * from emp order by sal desc;


29. ​​Request to query all employee information in 20 departments, the information to be queried is sorted by salary from high to low, if the wages are equal , then sort by employment date from early to late.
select * from emp where deptno = 20 order by sal desc, hiredate asc;


30. Change lowercase to uppercase
select upper('hello') from dual;


31. Uppercase Change letters to lowercase
select lower('HELLO WORLD' ) from dual;


32. Ask for the employee information whose name is smith
select * from emp where ename = upper('smith');


33. Use the initcap() function to capitalize the first letter of the word
select initcap('hello world') from dual ;


34. Change the names of employees in the employee table to uppercase
select initcap(ename) from emp;


35. Concatenate the strings "hello" and "world"
select concat('hello ', 'world') from dual ;


36. Common character processing functions that operate on strings
select substr('hello', 1, 3) intercepts the string, length('hello') the length of the string, replace('hello', 'l', ' x') string replacement from dual;
select substr('hello', 0, 3) intercept string, length('hello') string length, replace('hello', 'l', 'x') characters String replacement from dual;


37. Display the names of all employees and the last three characters of the name
select ename, substr(ename, length(ename) -2) from emp;
select ename, substr(ename, -3, 3) from emp;


38. Use numeric functions to perform rounding operations
select round ( 789.536) from dual; 39.


Requires the 789.536 value to be rounded to two decimal places ) from dual; 41. The trunc() function will not retain any decimals, and the decimal point will not perform rounding operations select trunc(789.536) from dual; 42. The reserved digits of the decimal point can also be specified by trunc() select trunc (789.536, 2) from dual; 43. Use negative numbers to indicate the number of digits select trunc(789.536, -2) from dual; 44. Use mod() function to perform remainder operation select mod(10, 3) from dual; 45 . Display the number of weeks in which 10 department employees entered the company (current date - hired date = days / 7 = weeks) select empno, ename, round((sysdate - hiredate) / 7) from emp where deptno = 10; 46. Date function months_between(): finds the number of months in a given date range





























add_months(): Add the specified number of months to the specified date, and find the date after
next_day(): The next date of the specified date
last_day(): Find the date of the last day of the month on the given date


47.
select empno, ename, months_between(sysdate, hiredate) from emp;
select empno, ename, round(months_between(sysdate, hiredate)) from emp;


48. select sysdate, add_months(sysdate, 4) from dual;


49. select next_day(sysdate, ' Monday') from dual;


50. select last_day(sysdate) from dual;


51. Conversion function
to_char(): Convert to string
to_number(): Convert to number
to_date(): Convert to date


52. Query all employees of employees number, name, hire
dateselect empno,
ename,
to_char(hiredate, 'yyyy') year,
to_char(hiredate, 'mm') months,
to_char(hiredate, 'dd') day
from emp;


select empno, ename, to_char(hiredate, 'yyyy-mm-dd') from emp;


select empno, ename, to_char(hiredate, 'fmyyyy-mm-dd') from emp;


53. Query the numbers of all employees , name and salary
select empno, ename, sal from emp;
select empno, ename, to_char(sal, '99,999') from emp;
select empno, ename, to_char(sal, 'L99,999') from emp;
select empno, ename, to_char(sal, '$99,999') from emp;


54. select to_number('123') + to_number('123') from dual;


55. Convert a string to a date type
select to_date('2009-01- 01', 'yyyy-mm-dd') from dual;


56. Find the annual salary of each employee (requires bonus)
select empno, ename, sal, comm, (sal + comm) * 12 from emp;
select empno , ename, sal, comm, nvl(comm, 0), (sal + nvl(comm, 0)) * 12 income from emp;


57. The decode() function is similar to the if....elsif...else statement
select decode(1, 1, 'content is 1', 2, 'content is 2', 3, 'content is 3') from dual ;


58. Query the employee's number, name, employment date and job, and ask to replace the employee's job with the following information:
select empno employee number,
ename employee name,
hiredate employment date,
decode(job,
'CLERK', 'salesperson' ',
'SALESMAN', 'Salesman',
'MANAGER', 'Manager',
'ANALYST', 'Analyst',
'PRESIDENT', 'President'
) Position
from emp;


59. Cartesian product (cross join)
select * from emp, dept;
select * from emp cross join dept;


60. inner join
select * from emp e, dept d where e.deptno = d.deptno;
select * from emp e inner join dept d on e.deptno = d .deptno;
select * from emp e join dept d on e.deptno = d.deptno;




61. Natural join
select * from emp natural join dept;
select * from emp e join dept d using(deptno);


62. Query the employee number , name, department number, name, address
select e.empno, e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno = d.deptno;


63. Query out Employee's name, job, the name of the employee's immediate superior
select e.ename, e.job, m.ename from emp e, emp m where e.mgr = m.empno;


64. Ask for the name, job, Name of employee's immediate supervisor and department name
select e.ename, e.job, m.ename, d.dname from emp e, emp m, dept d where e.mgr = m.empno and e.deptno = d.deptno ;


65. Ask for each employee's name, salary, department name, salary level in the company (salgrade), and the name of the leader and the level of the company where the salary is
selected e.ename, e.sal, d.dname, s.grade, m.ename, m.sal, ms.grade
from emp e, dept d, salgrade s, emp m, salgrade ms
where e.deptno = d.deptno
and e.sal between s.losal and s.hisal
and e.mgr = m.empno
and m.sal between ms. losal and ms.hisal;


select e.ename,
e.sal,
d.dname,
decode(s.grade, 1, 'fifth grade', 2, 'fourth grade', 3, 'third grade', 4 , 'second grade', 5, 'first grade'),
m.ename,
m.sal,
decode(ms.grade, 1, 'fifth grade', 2, 'fourth grade', 3, 'first grade' 3rd grade', 4, 'second grade', 5, 'first grade')
from emp e, dept d, salgrade s, emp m, salgrade ms
where e.deptno = d.deptno and e.sal between s. losal and s.hisal and e.mgr = m.empno
and m.sal between ms.losal and ms.hisal;


66. select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno = d.deptno;
    select empno, ename, d.deptno, dname, loc from emp e inner join dept d on e.deptno = d.deptno;


67. 左外连接
    select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno = d.deptno(+);
    select empno, ename, d.deptno, dname, loc from emp e left outer join dept d on e.deptno = d.deptno;
    select empno, ename, d.deptno, dname, loc from emp e left join dept d on e.deptno = d.deptno(+);


68. 右外连接
    select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno(+) = d.deptno;
    select empno, ename, d.deptno, dname, loc from emp e right outer join dept d on e.deptno = d.deptno;
    select empno, ename, d.deptno, dname, loc from emp e right join dept d on e.deptno = d.deptno;


69. select e.empno, e.ename, m.empno, m.ename from emp e, emp m where e.mgr = m.empno;


70. select e.empno, e.ename, m.empno, m.ename from emp e, emp m where e.mgr = m.empno(+);


71.
select * from emp e, dept d where e.deptno = d.deptno and d.deptno = 30;
select * from emp e inner join dept d on e.deptno = d.deptno where d.deptno = 30;
select * from emp e join dept d on e.deptno = d.deptno where d.deptno = 30;
select * from emp e natural join dept d where deptno = 30;
select * from emp e join dept d using(deptno) where deptno = 30;


72.
select e.ename, d.deptno, d.dname, d.loc from emp e right outer join dept d on e.deptno = d.deptno;
select e.ename, d.deptno, d.dname, d.loc from emp e right join dept d on e.deptno = d.deptno;
select e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno(+) = d.deptno;


73. select count(ename) from emp;


74. select min(sal) from emp;


75. select max(sal) from emp;


76. select sum(sal) from emp;


77. select avg(sal) from emp;


78. select sum(sal) from emp where deptno = 20;


79. select avg(sal) from emp where deptno = 20;


80. 求出每个部门的雇员数量
select deptno, count(deptno) from emp group by deptno;
select deptno, count(empno) from emp group by deptno;


81. 求出每个部门的平均工资
select deptno, avg(sal) from emp group by deptno;


82. Group by department, and display the name of the department and the number of employees in each department
select d.dname, count(e.empno) from emp e, dept d
where e.deptno = d.deptno
group by d.dname;


select d.deptno, d.dname, temp.c
from (select deptno, count(e.empno) c from emp e group by e.deptno) temp, dept d
where temp.deptno = d.deptno;


83. Request to display Show the department number and the average salary of the average salary greater than 2000
select deptno, avg(sal) from emp group by deptno having avg(sal) > 2000;


84. Show the non-sales person job name and the sum of the monthly salary of the employees engaged in the same job, And to meet the total monthly salary of employees engaged in the same job is greater than 5000, the output results are sorted in ascending order by the total monthly salary.
select job, sum(sal) su from emp where job <> 'SALESMAN' group by job having sum(sal) > 5000 order by su;


select temp.job, sum(temp.sal) s
from (select job, sal from emp e where job <> 'SALESMAN') temp
group by temp.job
having sum(temp.sal) > 5000
order by s;


85. Find the salary of the department with the highest average salary
select max(avg(sal)) from emp group by deptno;


86. Ask for the employee number
Select * from emp where sal >(select sal from emp where empno = 7654); 87. Query to find out the information of all employees


whose salary is higher than 7654 and who are engaged in the same job as 7788
select * from emp
where sal >(select sal from emp where empno = 7654)
and job = (select job from emp where empno = 7788);


88. Query the name, job, salary of the employee with the lowest salary
select ename, job, sal from emp where sal = (select min(sal) from emp);


89. Request to query: department name, number of employees in the department, average salary of the department, name of the lowest-paid employee in the department
select d.dname, temp.c, temp.a , e.ename
from dept d,
(select deptno, count(empno) c, avg(sal) a, min(sal) m from emp group by deptno) temp,
emp e
where d.deptno = temp.deptno and e.sal = temp.m;


select d.deptno, temp.dname, temp.c, temp.a, e.ename, e.sal
from
(select d.dname , count(e.empno) c, avg(e.sal) a, min(e.sal) m
from emp e, dept d
where e.deptno = d.deptno
group by d.dname) temp,
emp e,
dept d
where temp.m = e.sal
and temp.dname = d.dname;


90. 求出每个部门的最低工资的雇员的信息
select * from emp where sal in(select min(sal) from emp group by deptno);
select * from emp where sal =any(select min(sal) from emp group by deptno);
select * from
(select min(sal) m from emp group by deptno) temp,
emp e
where e.sal = temp.m;


91. In example 90, the employee information that is greater than the lowest (smaller) salary in the subquery condition
select * from emp where sal >any(select min(sal) from emp group by deptno);
select * from emp where sal > (select min(min(sal)) from emp group by deptno);


92. In Example 90, Bizi In the query condition, the employee information with the highest (largest) salary and the smaller salary
select * from emp where sal <any(select min(sal) from emp group by deptno);
select * from emp where sal < (select max(min(sal) ) from emp group by deptno);


93. In example 90, the employee information that is greater than the highest (largest) salary in the subquery condition
select * from emp where sal >all(select min(sal) from emp group by deptno) ;
select * from emp where sal > (select max(min(sal)) from emp group by deptno);


94. In example 90, the employee information that is less than the lowest (smaller) salary in the subquery condition
select * from emp where sal <all(select min(sal) from emp group by deptno);
select * from emp where sal < (select min(min(sal)) from emp group by deptno);


95. Find out the information of employees without bonuses in 20 departments
select * from emp where (sal, nvl(comm, -1)) in (select sal, nvl (comm, -1) from emp where deptno = 20);
select * from emp where deptno = 20 and comm is null;


96. union operator returns all unique rows selected by two queries
select deptno from emp union select deptno from dept;


97. The union all operation matches and merges all rows selected by the two queries, including duplicate rows select deptno from emp union all select
deptno from dept;


98. The intersect operator returns only the rows selected by both queries
deptno from emp intersect select deptno from dept;


99. The minus operator returns only the rows selected by the first query but not selected by the second query, that is, the rows that appear in the results of the first query are excluded from the results of the second query
select deptno from dept minus select deptno from emp; query employee name and sum of salary and bonus select ename,sal+ nvl(comm,0) from emp





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326335494&siteId=291194637