MySQL database - connection query SQL99

1. Connection query SQL99

1.1 Cross connection

Effect: Its effect is equivalent to not using the where clause to limit the connection conditions when connecting two tables;

SQL99:

select dept.deptno,dname,ename from dept cross join emp; 

 SQL92:

select dept.deptno,dname,ename from dept,emp; 

operation result:

d70f7c62e4714ae7b652a3573f9a6128.png

 1.2 Natural connection

Natural join: Establish a connection based on all columns with the same name in the two tables , and select all rows with equal values ​​in the columns with the same name from the two tables.

Note 1: If the data types of columns with the same name in the two tables are different, an error will occur.

Note 2: It is not allowed to use table names or aliases as prefixes on reference columns.

Note 3: The result of natural connection does not retain duplicate attributes

Example:

select empno, ename, sal, deptno, dname from emp natural join dept

operation result:

61765961466c445486ac6faa4549e4a1.png

 1.3 Using Child Clause

If you do not want to perform an equijoin with reference to all columns of the same name in the joined table, natural joins will not be able to meet the requirements. You can use the USING clause during the connection to set the column (reference column) name used for the equijoin .

The column referenced by the using clause cannot be prefixed with the table name or alias anywhere in SQL.

select e.ename,e.ename,e.sal,deptno,d. dname
	from emp e join dept d 
    using(deptno)
	where deptno=101

 1.4 On child clause

The condition of a natural join is an equijoin based on all columns with the same name in the table. In order to set arbitrary join conditions or specify the columns to be joined , the ON clause needs to be used. The connection conditions are written separately from other query conditions, and the ON clause is used to make the query statement easier to understand.

Example: 

select ename,dname from emp inner join dept 
on emp.deptno=dept.deptno 
where emp.deptno=30;

operation result:

00fc240342ec4dd583b642ebe04282d7.png 

 Example:

select empno, ename, sal, emp.deptno, dname from emp inner join dept 
on (emp.deptno = dept.deptno and sal>1000);

operation result: 

1f5f0c29e7a041d4bee97c63cf8038f4.png

 Example:

select * from dept, emp where dept.deptno = emp.deptno and sal>1000;

operation result: 

bc0ef0490a524d2281b9519b61f61aa2.png

 1.5 Outer joins

Left outer join:

During the connection process of two tables, in addition to returning rows that meet the connection conditions, it also returns rows in the left table that do not meet the conditions. This kind of connection is called a left outer join.

select * from table 1 alias 1 left [outer] join table 2 alias 2 on alias 1.xx=alias 2.xx

select * from emp e left outer join dept d on e.deptno=d.deptno;

select e.ename,e.deptno,d.dname from emp e left 
outer join dept d on e.deptno=d.deptno;

Right outer join:

During the connection process of two tables, in addition to returning rows that meet the connection conditions, it also returns rows in the right table that do not meet the conditions. This kind of connection is called a right outer join.

select * from table 1 alias 1 right [outer] join table 2 alias 2 on alias 1.xx=alias 2.xx

select * from emp e right outer join dept d on e.deptno=d.deptno;

2. Subquery

1) Single row subquery

Example: Query the salary of everyone whose salary is higher than CLEARK

select * from emp
     where sal>(select sal from emp where ename='CLARK');

Example: Query the information of employees who have the same position as SCOTT and were hired earlier than SCOTT 

SELECT  empno, ename, job FROM emp
WHERE job =(SELECT job FROM emp WHERE empno=7788)
AND hiredate < (SELECT hiredate FROM emp WHERE empno=7788);

Example: Query the number and name of employees whose salary is higher than SCOTT or whose employment time is earlier than SCOTT.

select empno,ename,sal,hiredate
from emp
where sal>(select sal from emp where ename='SCOTT') 
or hiredate<(select hiredate from emp where ename='SCOTT')

2)Multi-row subquery

all: Compare with all values ​​returned by the subquery

any: Compare with any value returned by the subquery

in: equal to any one in the list

 Example: Query employee information whose salary is lower than any 'CLERK' salary.

SELECT  empno, ename, job,sal
FROM emp
WHERE sal < ANY (SELECT sal FROM emp WHERE job = 'CLERK') AND job <> 'CLERK';

operation result:

f4e4f2db13264bb5a6af6a7ef99b8fab.png

 Example: Query the number, name and salary of employees whose salary is higher than all 'SALESMAN'.

SELECT  empno, ename,sal
FROM emp
WHERE sal > ALL(SELECT sal FROM emp WHERE job= 'SALESMAN');

operation result:

f16a6dc886f449b6a54f7a3b4fc73378.png

 Example: Query the information of employees in department 20 who have the same position as employees in department 10.

SELECT  empno, ename, job FROM emp
WHERE   job IN (SELECT job FROM emp WHERE deptno=10)
AND deptno =20;

operation result:

02ca1250f77445dbb74eb6f9e48b2656.png

 Example: Query the salary of employees whose salary is neither the highest nor the lowest

select * from emp where sal !=(select max(sal) from emp ) and sal !=
(select min(sal) from emp);

Example: Information about employees who are not salespeople

--法一:
select * from emp where deptno != (select deptno from dept where
dname='sales');
--法二:
select * from emp where deptno in(select deptno from dept where
dname !='sales');

Example: Query employee information and require any employee with a salary higher than department number 10.

--法一:
select * from emp where sal >(select min(sal) from emp where
deptno=10);
--法二:
select * from emp where sal >any (select sal from emp where
deptno=10);

Example: Query the average salary of each department

select * from 
	salgrade s, (select deptno,avg(sal) avg_sal 
			from emp group by deptno) t
	where  t.avg_sal between s.losal and s.hisal;

operation result:

2d0177d6f5094bc98bc70628ed83177d.png 

 

Guess you like

Origin blog.csdn.net/shengshanlaolin_/article/details/128458288