mysql query employee information exercises

01. queries each employee number, name, job title.
select empno, ename, job from emp ;

02. check out all the posts, using DISTINCT eliminates duplicate rows off the display.
select DISTINCT job from emp;

03. calculated for each employee number, name, basic salary. Salary = (salary + bonus comm) * 12, (using IFNULL function determines NULL)
SELECT EMPNO AS 'No', ename as 'name', (SAL + ifnull (comm , 0)) * 12 AS ' basic salary' from emp ;

04. Each employee meals a month the company will subsidize 200 yuan, 300 yuan traffic subsidy, salary calculation.
select empno as 'No', ename as 'name', (SAL + ifnull (comm , 0) + 300 + 200) * 12 AS ' basic salary' from emp;

05. All employee information query basic salary higher than 2000.
select * from emp e where e.SAL> 2000;

06. smith's check out the information.
select * from emp e where e.ENAME = 'SMITH';

07. For more information check out all the not CLERK.
! select * from emp e where e.JOB = 'CLERK';

08. Basic information check out all sales staff (SALESMAN), and sales staff wage requirements higher than 1300.
select * from emp e where e.JOB = 'SALESMAN' AND e.sal> 1300;

09. check out the salary range of all employee information between 1500 to 3000 (including 1500 and 3000).
select * from emp e where e.sal> = 1500 and e.sal <= 3000;

10. check out all the information managers or salespeople, and requires basic salary of these people is higher than 1500.
select * from emp e where e.job> = 'salesman' or e.job = 'manager' and e.sal> 1500;

11. Requests check out all the information employees hired in 1981.
E * from EMP WHERE SELECT BETWEEN e.HIREDATE ( '1981-1-01') and ( '1981-12-31');
12. The query information about all employees receive bonuses (COMM is not empty).
select * from emp e where e.comm = ''!;

13. All inquiries receive bonuses than 100 employee information.
select * from emp e where e.comm> 100;

14. queries the employee number of the employee information 7369,7566,9999.
select * from emp e where e.EMPNO = 7369 or e.EMPNO = 7566 or e.EMPNO = 9999;

15. check out all the employee names based on all employee information beginning with A.
select * from emp e where e.ename like 'A%';

16. Query the employee names second letter M of all employee information.
select * from emp e where SUBSTR ( e.ename, 2,1) = 'm';

17. check out all the employee information contains the letter A on the employee's name anywhere.
select * from emp e where e.ENAME like '% A%';

18. check out the information for all employees, sorted by wage demands.
select * from emp e order by e.sal asc;

19. The requests for access to information for all employees, in accordance with the employment date has sort of.
select * from emp e order by e.HIREDATE asc;

20. Query all the employee information, in accordance with the wage descending sort, if the wages are the same, according to the employment date has sort of.
SELECT * FROM emp ORDER BY sal DESC , HIREDATE ASC;

21. The sector inquiry in all 30 employees.
SELECT * from emp where DEPTNO = 30 ;

22. check out all the clerks (CLERK) name, number and department number.
SELECT empno, ename, DEPTNO from emp where job = 'clerk';

23. check out the bonus is higher than the salary of employees.
SELECT * from emp e where e.comm> e.SAL

24. check out 60% higher than the salary of the employee bonus.
SELECT * from emp e where e.comm>  0.6 * e.SAL

25. 20 check out all the clerks (CLERK) details all managers (MANAGER) 10 departments and sectors. United table query
(SELECT * from emp e where e.job = 'manager' and e.DEPTNO = 10) union all (SELECT * from emp b where b.job = 'clerk' and b.DEPTNO = 20)

26. 20 check out all clerical departments in all 10 managers, department, neither the manager and clerk, but not their salaries equal to or greater than 2000
information of all employees.
(E EMP from the SELECT * WHERE e.job! = 'Manager' and 10 and e.SAL e.DEPTNO => = 2000 and e.job! = 'Clerk') All Union
(EMP B from the SELECT * WHERE B.JOB ! = 'clerk' and b.job! = 'manager'and b.DEPTNO = 20 and b.sal> = 2000)

27. collect bonuses check out the work of employees.
select job from emp e where e.COMM!  = ''

28. check out the bonus does not charge or collect a bonus of less than 100 employees.
select * from emp e where e.COMM is  null or e.comm <100

29. queries the name without "R" of employees. Direct inquiries.
select EMPNO, ENAME from emp e where e.ename not like '% R%'

30. check out the name of each employee's job, the leadership of the name.

SELECT a.EMPNO AS 'staff no', a.ENAME AS 'employee name', a.JOB AS 'office employees', b.ename as 'superior name' FROM EMP a, EMP B where a.mgr = b. EMPNO

31. check out all the staff number, name and their immediate superiors number, name, the results displayed in decreasing order of leadership in wages.
SELECT a.EMPNO AS 'staff no', a.ENAME AS 'Employee Name', A.MGR, a.JOB AS 'staff duties', b.ename as' superior name', b.EMPNO as' superior job number ', b.job as' positions higher' the FROM A the EMP, the EMP WHERE a.mgr B = b.EMPNO
the ORDER BY (+ B.SAL the IFNULL (B.COMM, 0)) * 12 is DESC

32. queries the employee name (SALES) working in the sales department, base salary, hire date, department name. (Do not know the sales numbers).
select a.EMPNO, a.sal, a.HIREDATE, b.dname from emp a, (SELECT DEPTNO, dname FROM DEPT WHERE DNAME = 'sales') b where a.DEPTNO = b.DEPTNO ORDER BY a.SAL asc

33. check out the names of all employees, department name and salary.
select a.EMPNO, b.dname, a.sal from emp a, (SELECT DEPTNO, dname FROM DEPT) b where a.DEPTNO = b.DEPTNO ORDER BY a.SAL asc

34. check out the annual salary of all employees, where the department name, according to the annual salary from lowest to highest.
select a.EMPNO, b.dname, (a.sal + IFNULL (a.comm, 0)) * 12 as 'NIANXIN' from emp a, (SELECT DEPTNO, dname FROM DEPT) b where a.DEPTNO = b.DEPTNO ORDER BY NIANXIN asc

35. check out the name of the department where the supervisor and an employee and ask these executives are paid more than 3,000.

SELECT c. *, D.DNAME as 'higher level in the organization.' FROM (SELECT a.ENAME, A.MGR, B.DEPTNO, b.ename as ' the employee's name superiors', b.EMPNO as 'superior job number' A the EMP the FROM, WHERE a.mgr the EMP B = b.EMPNO and b.SAL> a.EMPNO = 3000 and '7566') C 
the LEFT ON c.DEPTNO the JOIN Dept D = d.DEPTNO

36. check out the company's highest and lowest wages.
MIN SELECT (SAL), MAX (SAL) from EMP;
the SELECT (the FROM EMP the ORDER BY the SELECT SAL SAL the LIMIT. 1), (the FROM EMP the SELECT SAL the ORDER BY SAL DESC the LIMIT. 1)

37. check out the number of people in each department, the average wage, only the department number.
select count (a.empno), avg ( sal), d.DEPTNO from emp a, (select deptno from dept) d where a.DEPTNO = d.deptno GROUP BY d.deptno

38. check out the highest and lowest wage for each job.
select max (sal), MIN ( sal), job from emp GROUP BY job

39. The inquiry average wage higher than 2000 jobs, and the number of employees engaged in this position, the average wage.
SELECT * from (select COUNT (EMPNO ) a, AVG (sal) b, job c from emp GROUP BY job) d WHERE db> 2000

40. Queries employee number is greater than its leading number of each employee's name, title, name of leadership.
SELECT a.empno as 'staff no', a.ENAME as 'Employee Name', a.job as 'post', b.ENAME as 'leader name' from emp a, emp b where a.mgr = b.EMPNO and a.EMPNO> a.MGR

41 check out at least a number of employees of all sectors, name, and the statistics of the average wage in these sectors, the minimum wage, maximum wage.
The SELECT
    e.` department number `, e.` department name`, e.` average wage `, e.` minimum wage`, e.` highest wage `
the FROM
    (
        the SELECT
            COUNT (a.empno) AS 'number of departments'
            d.DEPTNO AS 'department number',
            d.DNAME AS 'department name',
            AVG (SAL) AS 'average wage',
            MAX (SAL) AS 'maximum wage',
            min (SAL) AS 'minimum wage'
        the FROM
            emp A ,
            (the SELECT DEPTNO, the DNAME the FROM Dept) D
        the WHERE
            a.DEPTNO = d.deptno
        the GROUP BY
            d.deptno
    ) E
the WHERE
    number e.` sector '> 0,
    
42 / * Query the name of the department and employee information in these sectors (number, average wages), while those who are not employees of the department are listed. * /

43. The inquiry smith wages higher than all employee information.
select a. * from emp a, (select sal from emp b where b.ENAME = 'SMITH') c where a.SAL> c.sal

44. queries the salary and position and the same smith all employee information.  
select a. * from emp a, (select sal, job from emp b where b.ENAME = 'SMITH') c where a.SAL = c.sal and a.job = c.job

45. Queries sectoral responses number, department noun, departments address, number of employees and average wages.
select count (a.empno), avg ( sal), d. * from emp a, (select * from dept) d where a.DEPTNO = d.deptno GROUP BY d.deptno

Two table structure as follows:

DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
  `EMPNO` bigint(20) NOT NULL AUTO_INCREMENT,
  `ENAME` varchar(20) DEFAULT NULL,
  `JOB` varchar(20) DEFAULT NULL,
  `MGR` bigint(20) DEFAULT NULL,
  `HIREDATE` date DEFAULT NULL,
  `SAL` double(7,2) DEFAULT NULL,
  `COMM` double(7,2) DEFAULT NULL,
  `DEPTNO` bigint(2) DEFAULT NULL,
  PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB AUTO_INCREMENT=7935 DEFAULT CHARSET=utf8;

 

TABLE dept` the IF EXISTS `the DROP;
the CREATE TABLE dept`` (
  `DEPTNO` BIGINT (20 is) the AUTO_INCREMENT the NOT NULL,
  ` DNAME` VARCHAR (20 is) the DEFAULT NULL,
  `LOC` VARCHAR (20 is) the DEFAULT NULL,
  a PRIMARY KEY (` the DEPTNO `)
) ENGINE = InnoDB AUTO_INCREMENT = 41 the DEFAULT CHARSET =
utf8; ----------------
Disclaimer: This article is CSDN bloggers 'favorite LANG Xin' original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/LUONIANXIN08/article/details/93636769

Guess you like

Origin www.cnblogs.com/xysun/p/12124930.html