Query (select) syntax in Oracle

Source: Internet
Author: User
Tags dname

Oracle query syntax <>

1. Select * from EMP;

2. Select empno, ename, job from EMP;

3. Select empno number, ename name, job from EMP;

4. Select job from EMP;

5. Select distinct job from EMP;

6. Select distinct empno, job from EMP;
Note: Because employee numbers are not repeated, it is proved that all columns are not repeated at this time, so repeated Columns cannot be eliminated.

7. query the employee ID, name, and work, but the displayed format is: 7369 employee, name: Smith, and work: Clear
Select ':' | empno | 'employee, name:' | ename | ', job:' | job from EMP;

8. Obtain the name and annual salary of each employee.
Select ename, Sal * 12 Income from EMP;

9. Obtain information about all employees whose salaries exceed 1500.
Select * From e where SAL> 1500;

10. Query Information about employees who can receive bonuses each month.
Select * from EMP where comm is not null;

11. Query Information about employees without bonuses
Select * from EMP where comm is null;

12. Query Information about employees whose basic salary is greater than 1500 and whose bonuses can be received
Select * from EMP where SAL> 1500 and comm is not null;

13. query the information of employees whose basic salary is greater than 1500 or who can receive the bonus
Select * from EMP where SAL> 1500 or comm is not null;

14. query the information of employees whose basic salary is not greater than 1500 or who cannot receive the bonus
Select * from EMP where not (SAL> 1500 and comm is not null );

15. query information about all employees whose basic salary is greater than 1500, but less than 3000
Select * from EMP where SAL> 1500 and Sal <3000;

16. query information about all employees whose basic salary is greater than or equal to 1500, but less than or equal to 3000
Select * from EMP where SAL> = 1500 and Sal <= 3000;
Select * from EMP where Sal between 1500 and 3000;

17. Query Information on all employees hired in 1981 (employees hired between January 1-19, 1981 and December 31)
Select * from EMP where hiredate between '1-January-81 'and '31-December-81 ';

18. query the employee information whose name is Smith
Select * from EMP where ename = 'Smith ';

19. query specific information of employees 7369,749 and 7521
Select * from EMP where empno = 7369 or empno = 7499 or empno = 7521;
Select * from EMP where empno in (7369,749 9, 7521 );

20. query the details of employees who are not 7369,749, 7521
Select * from EMP where empno not in (7369,749 9, 7521 );

21. query the employee information whose name is Smith, Allen, and King.
Select * from EMP where ename in ('Smith ', 'allen', 'King ');

22. The second letter in all employee names contains "M" employee information.
Select * from EMP where ename like '_ M % ';

23. The employee's name contains employee information with the letter M.
Select * from EMP where ename like '% m % ';

24. Query of information on employees hired in 1981
Select * from EMP where hiredate like '% 100 ';

25. query employee information that includes 5 in salary
Select * from EMP where Sal like '% 100 ';

26. Query Information of employees whose employee number is not 7369
Select * from EMP where empno! = 7369;
Select * from EMP where empno <> 7369;

27. requirements are sorted in descending order of wages
Select * frm EMP order by Sal;
Select * from EMP order by Sal ASC;

28. requirements are sorted by salary from high to low
Select * from EMP order by Sal DESC;

29. All employees in department 20 should be queried. The queried information is sorted by salary from high to low. If the wages are equal, the information is sorted by employment date from early to late.
Select * from EMP where deptno = 20 order by Sal DESC, hiredate ASC;

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

31. Change uppercase letters to lowercase letters.
Select lower ('Hello World') from dual;

32. query the employee information whose name is Smith
Select * from EMP where ename = upper ('Smith ');

33. Use the initcap () function to uppercase the first letter of a word
Select initcap ('Hello World') from dual;

34. Change employee names in the employee table to uppercase letters
Select initcap (ename) from EMP;

35. concatenate the string "hello" and "world"
Select Concat ('hello', 'World') from dual;

36. Common Character Processing functions for string operations
Select substr ('hello', 1, 3) captures the length of the string, replace ('hello', 'l', 'x ') string replacement from dual;
Select substr ('hello', 0, 3) captures the length of the string, replace ('hello', 'l', 'x ') string replacement from dual;

37. display the names and the last three characters of all employees
Select ename, substr (ename, length (ename)-2) from EMP;
Select ename, substr (ename,-3, 3) from EMP;

38. Use a numeric function to perform the rounding operation
Select round (789.536) from dual;

39. The 789.536 value must be kept in two decimal places.
Select round (789.536, 2) from dual;

40. The 10 digits of the integer in the 789.536 value must be rounded to carry.
Select round (789.536,-2) from dual;

41. Using the trunc () function will not retain any decimal places, and the decimal point will not be rounded down.
Select trunc (789.536) from dual;

42. You can also specify the number of reserved digits of the decimal point through trunc ().
Select trunc (789.536, 2) from dual;

43. A negative number indicates the number of digits.
Select trunc (789.536,-2) from dual;

44. Use the MOD () function to perform the remainder operation.
Select Mod (10, 3) from dual;

45. display the number of weeks for employees in 10 departments to enter the company (current date-employment date = days/7 = days)
Select empno, ename, round (sysdate-hiredate)/7) from EMP where deptno = 10;

46. Date Functions
Months_between (): returns the number of months in the specified date range.
Add_months (): add the specified number of months to the specified date to obtain the subsequent date.
Next_day (): Specifies the next date of the date
Last_day (): returns the date of the last day of the given month.

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 functions
To_char (): Convert to string
To_number (): convert to a number
To_date (): Convert to date

52. query employee numbers, names, and employment dates of all employees
Select 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 IDs, names, and salaries of all employees
Select empno, ename, Sal from EMP;
Select empno, ename, to_char (SAL, '201312') 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 ('000000') + to_number ('000000') from dual;

55. convert a string to a date type
Select to_date ('1970-01-01 ', 'yyyy-mm-dd') from dual;

56. Calculate the annual salary of each employee (requires a 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. Check the employee ID, name, employment date, and work. Replace the employee's work with the following information:
Select empno employee ID,
Ename employee name,
Hiredate employment date,
Decode (job,
'Wheel', 'salesclerk ',
'Salesman', 'salesman ',
'Manager', 'manager ',
'Analyst', 'analysts ',
'President ', 'President'
) Position
From EMP;

59. Cartesian Product (intersection join)
Select * from EMP, DEPT;
Select * from EMP cross join dept;

60. inner connection
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 connection
Select * from EMP natural join dept;
Select * from EMP e join dept D using (deptno );

62. The employee ID, name, department ID, name, and address must be queried.
Select E. empno, E. ename, D. deptno, D. dname, D. LOC from emp e, DEPT d Where E. deptno = D. deptno;

63. query the employee's name, work, and employee's direct superior's name
Select E. ename, E. Job, M. ename from emp e, EMP m where E. Mgr = M. empno;

64. query the employee's name, work, employee's direct superior leadership name, 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. query the name, salary, Department name, salary in the company's salgrade, and the name of the leader and the grade of the company where the salary is located.
Select 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, 'level 5 ', 2, 'level 4', 3, 'level 3 ', 4, 'level 2', 5, 'level 1 '),
M. ename,
M. Sal,
Decode (Ms. grade, 1, 'level 5 ', 2, 'level 4', 3, 'level 3 ', 4, 'level 2', 5, 'level 1 ')
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. left Outer Join
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. Right Outer Join
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. Find the number of employees in each department
Select deptno, count (deptno) from EMP group by deptno;
Select deptno, count (empno) from EMP group by deptno;

81. Calculate the average salary of each department
Select deptno, AVG (SAL) from EMP group by deptno;

82. Group by department and display the Department name and 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. requires that the Department numbers and average salaries with an average salary greater than 2000 be displayed
Select deptno, AVG (SAL) from EMP group by deptno having AVG (SAL)> 2000;

84. displays the non-Salesperson's work name and the total monthly salary of the same employee, and the total monthly salary of the employee engaged in the same job must be greater than 5000. The output result is the ascending order of 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) & gt; 5000
Order by S;

85. Find the Department salary with the highest average salary
Select max (AVG (SAL) from EMP group by deptno;

86. All employees whose salaries are higher than the employee number 7654 are required to be queried
Select * from EMP where SAL> (select Sal from EMP where empno = 7654 );

87. Query of all employees whose salaries are higher than 7654 and who are engaged in the same job as 7788
Select * from EMP
Where SAL> (select Sal from e-mapreduce where e-mapreduce = 7654)
And job = (select job from EMP where empno = 7788 );

88. The name, work, and salary of the employee with the lowest wage should be queried.
Select ename, job, Sal from EMP where sal = (select Min (SAL) from EMP );

89. Required query results: department name, number of employees of the Department, average salary of the Department, and name of the employee with the lowest income of 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. Obtain the minimum wage information for each department
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, employees whose salaries are higher than (smaller) in subquery Conditions
Select * from EMP where SAL> Any (select Min (SAL) from EMP group by deptno );
Select * from EMP where SAL> (select Min (SAL) from EMP group by deptno );

92. In example 90, the employee information is smaller than the highest (large) wage in the subquery condition.
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 is higher than the highest (higher) 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. employee information in example 90, which is smaller than the minimum wage in subquery Conditions
Select * from EMP where Sal <all (select Min (SAL) from EMP group by deptno );
Select * from EMP where Sal <(select Min (SAL) from EMP group by deptno );

95. Find information about employees who do not have any bonuses in department 20.
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. The Union operator returns two rows selected for the query that are not repeated.
Select deptno from EMP Union select deptno from Dept;

97. The Union all operator combines two queries for all selected rows, including duplicate rows.
Select deptno from EMP Union all select deptno from Dept;

98. The Intersect operator returns only the rows of both queries
Select 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 excluded from the second query result in the first query result.
Select deptno from Dept minus select deptno from EMP;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.