Short training-database basics (2)

1. Grouping function

Commonly used SUM, AVG, MAX, MIN, COUNT

*Exercise
1. Query the maximum wage and minimum wage*

SELECT MIN(SAL),MAX(SAL) FROM EMP

2. Query the average salary, minimum salary, maximum salary, salary and ** of all employees of the position SALESMAN

SELECT AVG(SAL),MIN(SAL),MAX(SAL),SUM(SAL) FROM EMP 
WHERE JOB = 'SALESMAN'

3. Query how many employees are in department 30

SELECT COUNT(*) FROM EMP 
WHERE DEPTNO = 30

4. Query the number of departments with employees

SELECT COUNT(DISTINCT DEPTNO) FROM EMP 

5. Query the average salary and maximum salary of all employees whose positions start with SALE

SELECT AVG(SAL),MAX(SAL) FROM EMP 
WHERE JOB LIKE 'SALE%'

6. Query how many employees in department 30 receive bonuses

SELECT COUNT(DEPTNO) FROM EMP 
WHERE DEPTNO = 30 AND COMM IS NOT NULL
SELECT COUNT(COMM) FROM EMP WHERE DEPTNO = 30 

*Exercise
1. Query the employees of department 20, the sum and average salary of each month*

SELECT SUM(SAL),AVG(SAL) FROM EMP WHERE DEPTNO = 20

2. Query the number of employees working in CHICAGO, the maximum wage and minimum wage

SELECT COUNT(EMPNO),MAX(SAL),MIN(SAL) FROM EMP,DEPT 
WHERE LOC = 'CHICAGO' AND EMP.DEPTNO = DEPT.DEPTNO  

3. There are several types of positions in the query employee table

SELECT COUNT(DISTINCT JOB) FROM EMP  

4. Query the average salary of each department?

SELECT DEPTNO,AVG(SAL) FROM EMP 
GROUP BY DEPTNO

5. Query the number, average salary, and employee name of each department

SELECT AVG(SAL),DEPTNO,ENAME FROM EMP 
GROUP BY DEPTNO,ENAME

2. GROUP BY groups according to specified conditions

(The items after the SELECT statement, except for the grouping function, must be included in the GROUP BY clause)

3. Group by multiple columns

Query the total salary of each position in each department

SELECT SUM(SAL) FROM EMP 
GROUP BY DEPTNO,JOB

*Exercise
1. Query the department number, department name, number of departments, maximum wage, minimum wage, sum of wages, average wage of each department*

SELECT DEPTNO,DNAME,COUNT(EMPNO),MAX(SAL),MIN(SAL),SUM(SAL),AVG(SAL) FROM EMP E,DEPT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY E.DEPTNO,DNAME

2. Query each department, department number, department name, job name, number of departments, maximum wage, minimum wage, sum of wages, average wage of each post

SELECT DEPTNO,DNAME,JOB,COUNT(EMPNO),MAX(SAL),MIN(SAL),SUM(SAL),AVG(SAL) FROM EMP E ,DEPT D 
WHERE E.DEPTNO = D.DEPTNO 
GROUP BY E.DEPTNO,DNAME,JOB

3. Query the number of people managed by each manager, manager number, manager name, and request information about personnel without a manager

SELECT COUNT(E.EMPNO),E.MGR 经理编号,M.ENAME 经理姓名 FROM EMP E,EMP M
WHERE E.MGR = M.EMPNO(+)
GROUP BY E.MGR,M.ENAME

4. HAVING clause

1. Query the department number with the highest salary of each department greater than 2900, the highest salary

SELECT DEPTNO,MAX(SAL) FROM EMP HAVING MAX(SAL)>2900 GROUP BY DEPTNO

2. Query the department number, department name, and number of people in each department

SELECT DEPTNO,DNAME,COUNT(EMPNO) FROM EMP,DEPT 
WHERE EMP.DEPTNO = DEPT.DEPTNO
GROUP BU DEPT.DEPTNO,DNAME

3. Query the total salary and position of each position that does not start with SALE. The total salary is greater than 5000 and sorted in descending order by the total salary

SELECT SUM(SAL),JOB FROM EMP 
WHERE JOB NOT LIKE 'SALE%' 
HAVING SUM(SAL)>5000 
GROUP BY JOB 
ORDER BY SUM(SAL) DESC

4. Display the maximum average salary (function nesting)

SELECT  MAX(AVG(SAL)) FROM EMP 
GROUP BY DEPTNO

*Exercise
1. Query the department number, department name, and department number whose number of people is greater than 2*

SELECT EMP.DEPTNO,DNAME,COUNT(EMPNO) FROM EMP,DEPT 
WHERE EMP.DEPTNO= DEPT.DEPTNO
HAVING COUNT(EMPNO)>2
GROUP BY EMP.DEPTNO,DNAME

2. Query the department number, department name, number of people, average salary of the department where the average salary of the department is greater than 2000 and the number of people is greater than 2, and sort them in ascending order by the number of departments

SELECT EMP.DEPTNO,DNAME,COUNT(EMPNO),AVG(SAL) FROM EMP,DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO
HAVING AVG(SAL)>2000 AND COUNT(EMPNO)>2
GROUP BY EMP.DEPTNO,DNAME
ORDER BY COUNT(EMPNO) 

5. Subquery

1. Query information about employees whose salary is higher than JONES

SELECT ENAME,EMPNO,SAL,JOB FROM EMP 
WHERE SAL > (SELECT SAL FROM EMP WHERE ENAME= 'JONES')

2. Query the name of the employee with the lowest salary
SELECT ENAME FROM EMP WHERE SAL =
(SELECT MIN(SAL) FROM EMP)

3. Display the name and job of the employee who is engaged in the same job as employee 7369 and whose salary is greater than that of employee 7876

SELECT JOB,SAL,ENAME FROM EMP 
WHERE JOB = (SELECT JOB FROM EMP WHERE EMPNO = 7369)
AND SAL>(SELECT SAL FROM EMP WHERE EMPNO = 7876)

4. Query the name, position and salary of the employee with the lowest salary (group function is used in the subquery)

SELECT ENAME,JOB,SAL FROM EMP 
WHERE SAL =(SELECT MIN(SAL) FROM EMP)

5. Query the department number and minimum wage whose minimum wage is higher than the minimum wage of department No. 20 (subqueries are used in the HAVING clause)

SELECT DEPTNO,MIN(SAL) FROM EMP 
HAVING MIN(SAL)>(SELECT MIN(SAL) FROM EMP WHERE DEPTNO = 20) 
GROUP BY DEPTNO 

6. Query which department has the number of employees higher than the average number of departments

SELECT COUNT(EMPNO),DEPTNO FROM EMP 
GROUP BU DEPTNO 
HAVING COUNT(EMPNO)>
(SELECT AVG(COUNT(EMPNO)) FROM EMP) GROUP BY DEPTNO)

*–Exercise
1. Query the name, salary, department name of employees whose salary is higher than that of SMITH and whose work location is in CHICAGO*

SELECT ENAME,SAL,DNAME FROM EMP 
JOIN DEPT USING 

2. Query the name and salary of employees whose salary is higher than the minimum salary of 20 departments

SELECCT ENAME,SAL FROM EMP 
WHERE SAL>(SELECT MIN(SAL) FROM EMP WHERE DEPTNO = 20)

3. Query the department number, department name, and department number whose number of people is greater than the average number of all departments

SELECT DEPT.DEPTNO,DNAME,COUNT(DEPT.EMPNO) FROM EMP,DEPT 
WHERE EMP.DEPTNO = DEPT.DEPTNO
HAVING COUNT(DEPT.EMPNO)>(SELECT AVG(COUNT(EMPNO)) FROM EMP GROUP BY EMP.DEPTNO)
GROUP BY EMP.DEPTNO,DNAME

6. Multi-line subquery

IN ANY ALL
1. The query is the name and salary of the manager’s employee

SELECT ENAME,SAL FROM EMP 
WHERE EMPNO IN(SELECT DISTINCT MGR FROM EMP)
SELECT ENAME,SAL FROM EMP 
WHERE EMPON = ANY(SELECT DISTINCT MGR FROM EMP)
<ANY和结果集中任意一个结果比较,小于最大值即可
>ANY和结果集中任意一个结果比较,大于最小值即可
=ANY和IN的作用一样

2. Query the number, name, position and salary of the employee whose department number is not 10 and whose salary is higher than that of any employee in department 10.

SELECT EMPNO,ENAME,JOB,SAL FROM EMP 
WHERE SAL > ANYSELECT SLA FROM EMP WHERE DEPTNO = 10AND DEPTNO <> 10

3. Query the number, name, position, and salary of employees whose department number is not 10 and whose salary is higher than that of all employees in department 10.

SELECT EMPNO,ENAME,JOB,SAL FROM EMP 
WHERE SAL > ALLSELECT SLA FROM EMP WHERE DEPTNO = 10AND DEPTNO <> 10

*Exercise 4
Query the name and position of the employee whose position is the same as that of any employee in department 10, excluding employees in department 10*

SELECT ENAME,JOB FROM EMP 
WHERE JOB IN(SELECT JOB FROM EMP WHERE DEPTNO = 10)
AND DEPTNO <>10

7. Multi-column subquery

*Check out the department and position of any employee who joined in 1981. The name, department, and position of the employee are exactly the same as that of the employee who
joined in 1981 , excluding employees who joined in 1981.*

SELECT ENAME,DEPTNO,JOB,HIREDATE FROM EMP 
WHERE (DEPTNO,JOB) IN(SELECT DEPTNO,JOB FROM EMP 
WHERE HIREDATE(1981))

8. Null value problem in subquery

1. Query the name and number of employees who are not managers

SELECT ENAME,EMPNO FROM EMP 
WHERE EMPNI NOT IN(SELECT MGR FROM EMP)
WHERE MGR IS NULL

9. Use subqueries in FROM

Query the name, salary, department number, and average salary of employees whose average salary is higher than their own department
//SELECT AVGSAL, DEPTNO FROM T_AVGSAL

SELECT A.ENAME,A.SAL,A.DEPTNO, B.AVGSAL FROM EMP A,
(SELECT AVG(SAL) AVGSAL FROM EMP 
GROUP BY DEPTNO ) B
WHERE A.DEPTNO=B.DEPTNO AND A.SAL>B.AVGSAL

Query the name, position, department name, and average salary of employees whose average salary is higher than their own position

SELECT ENAME,E.JOB,D.DNAME A.AVGSLA FROM EMP E,DEPT D , 
(SELECT JOB,AVG(SAL) AVGSAL FROM EMP GROUP BY JOB) A
WHERE E.DEPTNO= D.DEPTNO AND
E.JOB = A.JOB AND 
E.SAL > A.AVGSAL

10. Homework

1. Query the name and average salary of the department whose average salary is over 2500 yuan.

SELECT D.DNAME,AVG(SAL) FROM EMP E,DEPT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY D.DNAME
HAVING AVG(SAL)>2500

2. Query the positions and average wages of employees that do not start with "SA" and whose average salary is more than 2500 yuan, and sort them in descending order of average salary.

SELECT JOB,AVG(SAL)
FROM EMP  
WHERE JOB NOT LIKE 'SA%'
GROUP BY JOB
HAVING AVG(SAL)>2500
ORDER BY AVG(SAL) DESC

3. Query the department name, minimum wage, and maximum wage of the department with more than 2 people, and round the wages to the nearest integer.

SELECT DNAME,ROUND(MIN(SAL),0),ROUND(MAX(SAL),0)
FROM EMP,DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO
GROUP BY DNAME
HAVING COUNT(EMPNO)>2

4. The query position is not SALESMAN, the salary sum is greater than or equal to 2500 and the salary sum of each position.

SELECT JOB,SUM(SAL)
FROM EMP
WHERE JOB<>'SALESMAN'
GROUP BY JOB
HAVING SUM(SAL)>=2500

5. Display the manager number and manager name, the minimum wage of the employees managed by this manager, KING without a manager should also be displayed, excluding the minimum wage less than 3000, sorted by minimum wage from high to low.

SELECT M.EMPNO,MIN(E.SAL)
FROM EMP E
LEFT JOIN EMP M
ON E.MGR = M.EMPNO
GROUP BY M.EMPNO
HAVING MIN(E.SAL)>=3000
ORDER BY MIN(E.SAL) DESC

6. Write a query that shows the difference between the maximum wage and the minimum wage in each department.

SELECT MAX(SAL)-MIN(SAL)
FROM EMP
GROUP BY DEPTNO

7. Query position and manager and any employee position of 10 departments and the same employee name and position of manager, excluding 10 department employees

SELECT MGR,JOB FROM EMP
WHERE (MGR,JOB) =ANY (
SELECT MGR,JOB FROM EMP WHERE DEPTNO=10)
AND DEPTNO<>10

8. Query the position and the name and position of the manager and any employee in the 10 departments or the same employee's position, excluding the employees in the 10 departments

SELECT JOB,MGR FROM EMP
WHERE (JOB IN(
SELECT JOB FROM EMP WHERE DEPTNO=10)
OR  MGR IN(
SELECT MGR FROM EMP WHERE DEPTNO=10))
AND DEPTNO<>10

9. Inquiry about the name and position of the employee whose position and manager are exactly the same as SCOTT or BLAKE, excluding SCOOT and BLAKE themselves.

SELECT E.ENAME,E.JOB FROM EMP E,
     (SELECT JOB,MGR FROM EMP WHERE ENAME IN('SCOTT','BLAKE') ) B
WHERE E.JOB =B.JOB
    AND E.MGR =B.MGR
    AND E.ENAME NOT IN('SCOTT','BLAKE')

10. Query the name and salary of the employee with the highest salary.

SELECT ENAME,SAL FROM EMP WHERE SAL=
(SELECT MAX(SAL) FROM EMP)

2017.9.7

Guess you like

Origin blog.csdn.net/geekerstar/article/details/77885790