- the department table the Create the Table the dept ( deptno int Primary Key , - department number DNAME nvarchar ( 30 ), - a department name LOC nvarchar ( 30 ) - address ); - Employee table the Create the Table emp ( empno int Primary Key , - employee number ename nvarchar ( 30 ), - employee name the Job nvarchar ( 30), - employees work MRG int , - the employee the higher the HireDate datetime , - entry time SAL numeric ( 10 , 2 ), - salaries COMM numeric ( 10 , 2 ), - bonuses deptno int Foreign Key the References the dept (deptno ) - foreign key ); INSERT INTO Dept values ( 10 , ' the ACCOUNTING ' , ' NEW YORK'); insert into dept values (20,'RESEARCH','DALLAS'); insert into dept values (30 ,'SALES','CHICAGO'); insert into dept values (40, 'OPERATIONS','BOSTON'); insert into emp values (7369,'SMITH','CLERK',7902,'1980-12-17',800.00,null,20); insert into emp values(7499,'ALLEN','SALESMAN',7698,'1981-2-20',1600.00,300.00,30); insert into emp values(7521,'WARD','SALESMAN',7698,'1981-2-22',1250.00,500.00,30); insert into emp values(7566,'JONES','MANAGER',7839,'1981-4-2',2975.00,null,20); insert into emp values(7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250.00,1400.00,30); insert into emp values(7698,'BLAKE','MANAGER',7839,'1981-5-1',2850.00,null,30); insert into emp values(7782,'CLARK','MANAGER',7839,'1981-6-9',2450.00,null,10); insert into emp values(7788,'SCOTT','ANALYST',7566,'1987-4-19',3000.00,null,20); insert into emp values(7839,'KING','PRESIDENT',null,'1981-11-17',5000.00,null,10); insert into emp values(7844,'TURNER','SALESMAN',7698,'1981-9-8',1500.00,0.00,30); insert into emp values(7876,'ADAMS','CLERK',7788,'1987-5-23',1100.00,null,20); insert into emp values(7900,'JAMES','CLERK',7698,'1981-12-3',950.00,null,30); insert into emp values(7902,'FORD','ANALYST',7566,'1981-12-3',3000.00,null,20); insert into emp values(7934,'MILLER','CLERK',7782,'1982-1-23',1300.00,null,10);
Subqueries
■ What is a subquery
Subquery is a sql statement embedded in other select statement, also called nested query
■ single-row subquery
Single-row subquery returns only one row of data refers to a sub-query
Consider: how to display all the employees in the same department SMITH?
select * from emp where deptno=(select deptno from emp where ename=’SMITH’);
Multiple-row subqueries
Multi-line sub-query returns multiple rows of data refers to a sub-query
Consider: how to query and name of the same department employee, job, salary, department number
1, first check what post No. 10 departments
select distinct job from emp where deptno=10;
2, displaying his post and have a same employee
select ename,job,sal,deptno from emp where job in(select distinct job from emp where deptno=10)
Fully connected
select * from emp,dept;
Natural query
Natural join: the equivalent Duplicate column removed SELECT student.sno, sname, SSEx, SAGE, sdept, CNO, Grade from Student, SC WHERE student.sno = sc.sno;
Left and right connections
Left connection: left ON , turn left to traverse the table, the query whether there are corresponding records in the right table, if there is a corresponding record, the match or the display null the SELECT student.sno, sname, SSEx, SAGE, sdept, CNO, Grade from Student left the Join SC ON (student.sno = sc.sno); right connection: rigth ON , the right side as a reference in table SELECT student.sno, sname, SSEx, SAGE, sdept, CNO, Grade from Student right the Join SC ON (student.sno = sc.sno);
union union
The operator is used to achieve the result set and two sets. When using this operator, the result set will automatically remove duplicates.
select ename,sal,job from emp where sal>2500 union select ename,sal,job from emp where job='MANAGER';
select * from student where sage>20 union select * from student where sage<22
Two sets of results " Union", "intersecrt", "the except" operation of these two columns of the result set must be the same .
intersect intersection
Using the operator for acquiring the intersection of two sets of results.
select ename,sal,job from emp where sal>2500 intersect select ename,sal,job from emp where job='manager';
select * from student where sage>20 intersect select * from student where sage<22
except difference set
Using the operator for obtaining the difference between two sets of result sets, it will only show the presence of the first set, the second set of data is not present.
select ename,sal,job from emp where sal>2500 minus select ename,sal,job from emp where job='manager';
select * from student where sage>20 except select * from student where sage>22