Common SQL query statements

Source: Internet
Author: User
Tags dname sorted by name

  1. 1. Find employees with a Commission higher than 60% of their salaries.
  2. Select * From e where comm> Sal * 0.6;
  3. 2. Find out the details of all clerks in department 10, all managers and department 20.
  4. Select * from EMP
  5. Where deptno = 10 and job = 'manager' or deptno = 20 and job = 'cler ';
  6. 3. Find out the details of all managers in department 10, all clerks in department 20, and all employees who are neither a manager nor clerk but whose salaries are greater than or equal to 2000.
  7. Select * from EMP
  8. Where deptno = 10 and job = 'manager' or deptno = 20 and job = 'wheel' or job not in ('manager', 'wheel') and Sal> = 2000;
  9. Select * from EMP
  10. Where deptno = 10 and job = 'manager' or deptno = 20 and job = 'wheel' or (job <> 'manager' and Sal> = 2000 );
  11. 4. Find out the different jobs of the employees who charge the commission.
  12. Select distinct job from EMP where comm is not null;
  13. 5. Find out the employees who do not receive a commission or receive a Commission less than 300.
  14. Select * from EMP where comm is null or comm <300;
  15. 6. Find all employees employed on the last day of each month.
  16. Select * from EMP where hiredate = last_day (hiredate );
  17. -- Find all employees who are last hired each month
  18. Select * from EMP where hiredate in
  19. (Select maxh from
  20. (Select max (hiredate) as maxh, extract (month from hiredate)
  21. From EMP
  22. Group by extract (month from hiredate )));
  23. 7. Find the employees employed later than 26 years ago.
  24. Select * from EMP where months_between (sysdate, hiredate) <= 26*12;
  25. 8. The names of all employees whose first letters are capitalized are displayed.
  26. Select * from EMP where ename = initcap (ename );
  27. 9. The employee name is exactly five characters long.
  28. Select * from EMP where length (ename) = 5;
  29. 10. The employee name without "R" is displayed.
  30. Select * from EMP where instr (ename, 'R') = 0;
  31. Select * from EMP where ename not like '% R % ';
  32. 11 display the first three characters of the names of all employees.
  33. Select substr (ename, 1, 3) as en3 from EMP;
  34. 12. display the names of all employees. Replace "a" with "".
  35. Select Replace (ename, 'A', 'A') from EMP;
  36. 13 display the names of all employees and the dates of the service life of 10 years.
  37. Select ename, hiredate, add_months (hiredate, 120) as h_10y from EMP;
  38. 14. Detailed information about employees is displayed, sorted by name.
  39. Select * from EMP order by ename;
  40. 15. display the employee name. The oldest employee is placed at the top of the list based on the service life.
  41. Select ename, hiredate from EMP order by hiredate;
  42. 16 displays the names, jobs, and salaries of all employees. Jobs are sorted in descending order of work, and jobs are sorted by salary.
  43. Select ename, job, Sal
  44. From EMP
  45. Order by job DESC, Sal;
  46. 17. The names of all employees and the year and month of joining the company are displayed, sorted by the month where the employee's employment day is located, and the items in the earliest year are listed at the top.
  47. Select ename, to_char (hiredate, 'yyyy-mm') as y_m
  48. From EMP
  49. Order by to_char (hiredate, 'mm'), to_char (hiredate, 'yyyy ');
  50. 18 shows the daily salary of all employees in the case of 30 days in a month, rounded up.
  51. Select round (SAL/30) as Sal from EMP;
  52. 19. Find all employees who were employed in April February (in any year.
  53. Select * from EMP where to_char (hiredate, 'mm') = '02 ';
  54. 20. For each employee, the number of days of joining the company is displayed.
  55. Select Ceil (SYSDATE-HIREDATE) as Dy from EMP;
  56. 21 shows any location of the Name field, including the names of all employees of ".
  57. Select ename from EMP where ename like '% A % ';
  58. Select ename from EMP where instr (ename, 'A')> 0;
  59. 22. The service life of all employees is displayed on a yearly, monthly, and daily basis.
  60. Select hiredate,
  61. Floor (months_between (sysdate, hiredate)/12) as y,
  62. MoD (floor (months_between (sysdate, hiredate), 12) as m,
  63. MoD (floor (SYSDATE-HIREDATE), 30) as d
  64. From EMP;
  65. 23 list all departments with at least one employee.
  66. Select * from Dept where deptno in (select distinct deptno from EMP );
  67. 24 list all employees with higher salaries than Smith.
  68. Select * from EMP where SAL> (select Sal from EMP where ename = 'Smith ');
  69. 25. List the names of all employees and their superiors.
  70. Select ygb. ename as empnm, sjb. ename as mgrnm
  71. From EMP ygb, EMP sjb
  72. Where ygb. Mgr = sjb. empno;
  73. 26 List all employees whose employment date is earlier than their direct superiors.
  74. Select * from EMP ygb where hiredate <(select hiredate from EMP sjb where sjb. empno = ygb. Mgr );
  75. 27 List departments and employees in these departments, and list departments without employees.
  76. /*
  77. Select * from Dept where deptno in (select distinct deptno from EMP)
  78. Union
  79. Select * from Dept where deptno not in (select distinct deptno from EMP)
  80. */
  81. Select dname, ename
  82. From dept a left join EMP B
  83. On a. deptno = B. deptno;
  84. 28. List the names of all "Clerk" (Clerks) and their department names.
  85. Select ename, dname
  86. From dept a, EMP B
  87. Where a. deptno = B. deptno and job = 'cler ';
  88. 29. List the minimum salaries of various types and make the minimum salary more than 1500.
  89. Select job, min (SAL)
  90. From EMP
  91. Group by job
  92. Having min (SAL)> 1500;
  93. 30. List the names of employees engaged in sales. Assume that they do not know the Department Number of the Sales Department.
  94. Select ename from EMP where deptno = (select deptno from Dept where dname = 'sales ');
  95. 31 List all employees whose salaries are higher than the company average.
  96. Select * from EMP where SAL> (select AVG (SAL) from EMP );
  97. 32. list all employees engaged in the same job as Scott.
  98. Select * from EMP where job = (select job from EMP where ename = 'Scott ') and ename <> 'Scott ';
  99. 33. List the names and salaries of all employees whose salaries are equal to those of Department 30.
  100. Select * from EMP where Sal in (select Sal from EMP where deptno = 30 );
  101. 34 list the names and salaries of all employees whose salaries are higher than those of employees who work in department 30.
  102. Select * from EMP where SAL> All (select Sal from EMP where deptno = 30 );
  103. 35 list the managers and other information of employees working in each department.
  104. Select a. *, B .*
  105. From dept a, EMP B
  106. Where a. deptno = B. deptno and job = 'manager ';
  107. /*
  108. Select a. *, B .*
  109. From (select * from EMP where job <> 'manager') A, (select * from EMP where job = 'manager') B
  110. Where a. deptno = B. deptno and B. Job = 'manager'
  111. Order by A. deptno;
  112. */
  113. 36 List the names, department names, and salaries of all employees.
  114. Select ename, dname, Sal
  115. From dept a, EMP B
  116. Where a. deptno = B. deptno;
  117. 37 list different groups of employees engaged in the same job but belonging to different departments.
  118. Select * from EMP order by job, deptno;
  119. 38 list the details of all departments allocated with the number of employees, even if the number of employees is 0.
  120. Select * from Dept where deptno in (select distinct deptno from EMP)
  121. Union
  122. Select * from Dept where deptno not in (select distinct deptno from EMP)
  123. 39 list the minimum wage for various types of jobs.
  124. Select job, min (SAL)
  125. From EMP
  126. Group by job;
  127. 40 list the minimum salaries of managers in each department.
  128. Select min (SAL) from EMP where job = 'manager ';
  129. 41 list the annual salary of all employees sorted by annual salary.
  130. Select Sal * 12 as y_sal from EMP order by Sal * 12;
  131. 42 list the employees whose salaries are at the fourth place.
  132. Select * from
  133. (Select a. *, row_number () over (order by Sal DESC) as rn from emp)
  134. Where Rn = 4;
  135. 43. Search for the first five records in the EMP table
  136. Select * from EMP where rownum <= 5;
  137. 44 search for more than 10 records in the EMP table
  138. Select * from (select a. *, rownum as rn from EMP a) Where rn> 10;
  139. 45. Search for employees with a salary of 5th in the EMP table
  140. Select * from
  141. (Select a. *, row_number () over (order by Sal DESC) as rn from emp)
  142. Where Rn = 5;
  143. 46. Find employees with a salary of 3rd in the EMP table Department 30.
  144. Select * from
  145. (Select a. *, row_number () over (partition by deptno order by Sal DESC) as rn from emp)
  146. Where Rn = 5 and deptno = 30;
  147. 47 search for employees with a salary of 3rd per department in the EMP table
  148. Select * from
  149. (Select a. *, row_number () over (partition by deptno order by Sal DESC) as rn from emp)
  150. Where Rn = 3;
  151. 48. Calculate the total salaries of each department.
  152. Select deptno, sum (SAL) as sumsal
  153. From EMP
  154. Group by deptno;
  155. -- Details of employees and managers of each department: number, name, salary, employment date, Department number
  156. Select a. empno as Eno, A. ename as ENM, A. Job as ejob, A. Sal as esal, A. hiredate as edate, A. deptno as edept,
  157. B. empno as MnO, B. ename as mnm, B. Job as mjob, B. Sal as msal, B. hiredate as mdate, B. deptno as mdept
  158. From (select * from EMP where job <> 'manager' and job <> 'President ') A, (select * from EMP where job = 'manager') B
  159. Where a. deptno = B. deptno;

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.