# Built table Create Table Department ( ID int, name VARCHAR ( 20 is ) ); Create Table Employee ( ID int Primary Key AUTO_INCREMENT, name VARCHAR ( 20 is ), Sex enum ( ' MALE ' , ' FEMALE ' ) Not null default ' MALE ' , Age int, the dep_id int ); # insert data iNSERT INTO Department values ( 200 is, ' technical ' ), ( 201, ' HR '), ( 202, ' sales ' ), ( 203, ' operation ' ); INSERT INTO Employee (name, Sex, Age, the dep_id) values ( ' Egon ' , ' MALE ' , 18,200 ), ( ' Alex ' , ' FEMALE ' , 48,201 ), ( ' wupeiqi ' , ' MALE ' , 38,201 ), ( ' yuanhao ' , 'female', 28,202 ), ( ' liwenzhou ' , ' MALE ' , 18,200 ), ( ' jingliyang ' , ' FEMALE ' , 18,204 ) ;
En:
select * from employee inner join department on employee.dep_id=department.id;
Record retention, including the connection of the left table on the basis of: Left connection
select * from employee left join department on employee.dep_id=department.id;
Right Connections:
select * from employee right join department on employee.dep_id=department.id;
Full outer connector: reserved table do not correspond to the right and left inner connection on the basis of the recording
select * from employee left join department on employee.dep_id=department.id
union
select * from employee right join department on employee.dep_id=department.id;
# Query sector average age greater than 30 years old
select department.name,avg(age) from employee inner join department on employee.dep_id=department.id group by department.name having avg(age)>30;
SELECT statement to define the order of keywords
The execution order of the SELECT statement keywords
FROM execution
ON execution
Implementation of external lines