Real mysql query column

A. Data Preparation

- department table
dept department table (DEPTNO department number / dname department name / loc Location)
Create Table dept (
DEPTNO numeric (2),
DNAME VARCHAR (14),
LOC VARCHAR (13 is)
);
INSERT INTO dept values (10, 'the ACCOUNTING ',' NEW YORK ');
INSERT INTO Dept values (20 is,' s', 'DALLAS');
INSERT INTO Dept values (30, 'the SALES', 'CHICAGO');
INSERT INTO Dept values (40, 'OPERATIONS' , 'the BOSTON');
- wage scale
salgrade wage scale (grade level / losal minimum / hisal this highest level of this class)
Create table salgrade (
grade numeric,
Losal numeric,
hisal numeric
);

insert into salgrade values (1, 700, 1200);
insert into salgrade values (2, 1201, 1400);
insert into salgrade values (3, 1401, 2000);
insert into salgrade values (4, 2001, 3000);
insert into salgrade values (5, 3001, 9999);

- Staff table
emp table employees (empno number of employees / ename employee name / job work / mgr higher number / hiredate hire date / sal salary / comm commission / deptno department number)
Salary = salary + commission
the Create the Table emp (
empno numeric (4 ) Not null,
ename VARCHAR (10),
Job VARCHAR (. 9),
MGR numeric (. 4),
HireDate datetime,
SAL numeric (. 7, 2),
COMM numeric (. 7, 2),
DEPTNO numeric (2)
);
INSERT INTO values EMP (7369, 'SMITH', 'the CLERK', 7902, '1980-12-17', 800, null, 20 is);
INSERT INTO EMP values (7499, 'ALLEN', 'SALESMAN', 7698, '1981- 02-20 ', 1600, 300, 30);
INSERT INTO EMP values (7521,' WARD ',' SALESMAN ', 7698,' 1981-02-22 ', 1250, 500, 30);
INSERT INTO EMP values (7566 , 'JONES', 'MANAGER' , 7839, '1981-04-02', 2975, null, 20);
insert into emp values (7654, ‘MARTIN’, ‘SALESMAN’, 7698, ‘1981-09-28’, 1250, 1400, 30);
insert into emp values (7698, ‘BLAKE’, ‘MANAGER’, 7839, ‘1981-05-01’, 2850, null, 30);
insert into emp values (7782, ‘CLARK’, ‘MANAGER’, 7839, ‘1981-06-09’, 2450, null, 10);
insert into emp values (7788, ‘SCOTT’, ‘ANALYST’, 7566, ‘1982-12-09’, 3000, null, 20);
insert into emp values (7839, ‘KING’, ‘PRESIDENT’, null, ‘1981-11-17’, 5000, null, 10);
insert into emp values (7844, ‘TURNER’, ‘SALESMAN’, 7698, ‘1981-09-08’, 1500, 0, 30);
insert into emp values (7876, ‘ADAMS’, ‘CLERK’, 7788, ‘1983-01-12’, 1100, null, 20);
insert into emp values (7900, ‘JAMES’, ‘CLERK’, 7698, ‘1981-12-03’, 950, null, 30);
insert into emp values (7902, ‘FORD’, ‘ANALYST’, 7566, ‘1981-12-03’, 3000, null, 20);
insert into emp values (7934, ‘MILLER’, ‘CLERK’, 7782, ‘1982-01-23’, 1300, null, 10);

Second, the inquiry practice

1. check out the department number for all employees of the 30 numbers and names

select empno,ename from emp where deptno=30;

2. Identify the department number to 10 all managers, and department number 20 for the details of all the salesperson.

select empno,ename,job,hiredate,sal,deptno
from emp where
(emp.job = 'MANAGER' and emp.deptno = 10) or
(emp.job = 'SALESMAN' and emp.deptno = 20);

3. read detailed information about all employees, with wages sorted in descending order, if wages using the same entry date in ascending order

select empno,ename,job,hiredate,sal,deptno
from emp
order by emp.sal desc, emp.hiredate asc;

4. List the minimum salary greater than 1500 variety of work and the number of employees engaged in this work.

select e.job,count(*) as 员工人数
from emp e 
group by job having min(sal)>1500;

5. List the names of employees in the sales department, assuming sales department does not know the number.

select e.ename from emp e
where e.deptno =(select deptno from dept where dname='SALES');

6. query name starts with S \ S ending \ S contains characters \ The second letter L

SELECT emp.ename FROM emp WHERE emp.ename LIKE 'S%';
SELECT emp.ename FROM emp WHERE emp.ename LIKE '%S';
SELECT emp.ename FROM emp WHERE emp.ename LIKE '%S%';
SELECT emp.ename FROM emp WHERE emp.ename LIKE '_L%';

7. each job inquiries highest wages, the minimum wage, the number of

select emp.job,max(sal),min(sal),count(1) as 人数
from emp group by emp.job;

8. List salary higher than the company average salary of all employees number, employee name, where the name of the department, superiors, wages, wage scale

select e.empno as 工号,e.ename as 姓名,
d.dname as 部门,
m.ename as 上级领导,
(e.sal+ifnull(e.comm,0)) as 工资,
s.grade as 薪水等级
from emp e 
  left join dept d on e.deptno=d.deptno
  left join emp m on  e.mgr=m.empno
  left join salgrade s on e.sal between s.losal and s.hisal
where (e.sal+ifnull(e.comm,0)) > (select avg(sal+ifnull(comm,0)) from emp);

9. List all salaries higher than 30 departments in the / salary of any employee names and salaries of employees, the department name.

all:用法
select e.ename, e.sal, d.dname
from emp e, dept d
where e.deptno=d.deptno and sal > all(select sal from emp where deptno=30)
或者
select e.ename,e.sal,d.dname
from emp as e
left join  dept as d on  e.deptno=d.deptno 
where  e.sal >(select max(sal)  from emp group by deptno having deptno=30);

any:用法
select e.ename, e.sal, d.dname
from emp e, dept d
where e.deptno=d.deptno and sal > any(select sal from emp where deptno=30)
或者
select e.ename,e.sal,d.dname
from emp as e
left join  dept as d on  e.deptno=d.deptno 
where  e.sal >(select max(sal)  from emp group by deptno having deptno=30);
或者
select e.ename,e.sal,d.dname
from emp as e
left join  dept as d on  e.deptno=d.deptno 
where  e.sal >(select min(sal)  from emp group by deptno having deptno=30);

Guess you like

Origin blog.csdn.net/whiteblacksheep/article/details/94337317