The database has been built:
dept:
emp:
1. Connect query
1. Equal connection:
[Example 2-27] Query the employee number, name, salary, department number and address of the employee whose salary is greater than or equal to 3000. The results are sorted by department number.
SELECT empno,ename,sal,deptno FROM emp WHERE sal>=3000 ORDER BY deptno;
2. Connect yourself:
[Example 2-28] Query the name of the employee working in department 21 and the name of the administrator in the emp table
SELECT e.ename 雇员,m.ename 管理员 FROM emp e,emp m WHERE m.empno=e.mgr AND e.deptno=21;
Note: The alias AS can be saved
result:
3. Unequal connection:
[Example 2-29] The salary grade information is stored in the salgrade table, and the information about the salary and salary grade of the employee working in department number 20 is queried.
SELECT e.ename,e.sal,s.grade
FROM emp e,salgrade s
WHERE e.sal BETWEEN s.losal AND s.hisal
AND e.deptno=20;
4. Left outer connection:
FROM 表1 LEFT 【OUTER】 JOIN 表2
ON Table 1. Column = Table 2. Column
SELECT dept.deptno,emp.deptno,ename,empno
FROM dept LEFT JOIN emp
ON dept.deptno=emp.deptno
WHERE dept.deptno=2 OR dept.deptno=3;
result:
Note: 1.outer can be omitted;
2. If the right table information does not match, the right table fills in the blank
Example:
SELECT dept.deptno,emp.deptno,ename,empno
FROM dept LEFT JOIN emp
ON dept.deptno=emp.deptno
WHERE dept.deptno=6 OR dept.deptno=3;
result:
5. Right outer connection:
FROM 表1 RIGHT 【OUTER】 JOIN 表2
ON Table 1. Column = Table 2. Column
Note: If the left table information does not match, the left table fills in the blank
[Example 2-31] Example.
SELECT empno,ename,emp.deptno,dept.deptno
FROM emp RIGHT OUTER JOIN dept
ON emp.deptno=dept.deptno
WHERE dept.deptno=2 OR dept.deptno=3;
result:
Second, the sub-query (select statement nesting)
1. Subquery that returns a single value:
[Example 2-32] Query the employee ID, name, salary, and post information of the same employee as the Draw job.
SELECT empno,ename,sal,Job FROM emp WHERE Job='Draw';
[Example 2-33] Query information about employees whose salary is greater than the average salary and who have the same job position as Draw.
SELECT * FROM emp WHERE sal>(SELECT AVG(sal) FROM emp) AND Job='Draw';
错误:SELECT * FROM emp WHERE sal>AVG(sal) AND Job='Draw';
Note: The aggregate function cannot be directly followed in the where clause;
2. Subquery that returns multiple values
When using multi-valued subqueries in the WHERE clause, you must use multi-valued comparison operators: [NOT] IN, [NOT] EXISTS, ANY, ALL, where ALL and ANY must be used in combination with comparison operators.
Multi-valued subquery using IN operator
[Example 2-34] Query the employee ID, name, position and salary information of the employee with the highest salary, excluding employees with CLERK and PRESIDENT
SELECT Job,MAX(sal) FROM emp GROUP BY Job HAVING Job
NOT IN('Draw','Pant');
Multi-valued subquery using ALL operator
[Example 2-35] Query employee information higher than the salary of all employees in department 2.
SELECT * from emp WHERE sal>ALL(SELECT sal FROM emp WHERE deptno=2);
Multi-valued subquery using ANY operator
[Example 2-36] Query employee information higher than the salary of any employee in department 10 .
SELECT * FROM emp WHERE sal>ANY(SELECT sal FROM emp WHERE deptno=2);
Multi-row query using EXISTS operator
[Example 2-37] Query information about the name, department number, salary, and position of employees working in NEW YORK.
SELECT ename,deptno,sal,Job FROM emp WHERE EXISTS(SELECT loc FROM emp WHERE loc='NEW YORK');
Three, combined query
The syntax of UNION is:
SELECT statement 1
UNION [ALL]
SELECT statement 2
SELECT empno,ename,deptno,job FROM emp WHERE job='MANAGER'
UNION
SELECT empno,ename,deptno,job FROM emp WHERE deptno=10;