Forty MYSQL complex query practice questions (low difficulty)

MYSQL simple query exercises, I want to do a more complex connection as follows:
https://blog.csdn.net/weixin_50843918/article/details/123086590?spm=1001.2014.3001.5501

Get straight to the point

Create a database, create a table, insert data

The code can be copied directly in order

-- 建库
CREATE DATABASE `emp`;
-- 打开库
USE emp;
-- 建dept表
CREATE TABLE `dept`( `deptno` INT(2) NOT NULL, `dname` VARCHAR(14), `loc` VARCHAR(13), CONSTRAINT pk_dept PRIMARY KEY(deptno) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 键emp表
CREATE TABLE `emp` ( `empno` int(4) NOT NULL PRIMARY KEY, `ename` VARCHAR(10), `job` VARCHAR(9), `mgr` int(4), `hiredate` DATE, `sal` float(7,2), `comm` float(7,2), `deptno` int(2), CONSTRAINT fk_deptno FOREIGN KEY(deptno) REFERENCES dept(deptno) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 建salgrade表
CREATE TABLE `salgrade` ( `grade` int, `losal` int, `hisal` int ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入数据
INSERT INTO dept VALUES (10,'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,NULL,20); 
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,'1987-07-13',3000,NULL,20); 
INSERT INTO EMP VALUES (7839,'KING','PRESIDENT',NULL,'1981-11-07',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,'1987-07-13',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);
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);

dept table:
insert image description here
emp table:
insert image description here
salgrade table:
insert image description here

field description

Employee table:
records the basic information of an employee EMP (employee table)
NO ----------- field --------------- type------ -------------Description
1 ---------EMPNO ------ NUMBER(4)---------- Employee Number
2 ---- -----ENAME----- VARCHAR2(10) ---- means employee name
3 ----------JOB ----------VARCHAR2(9) -- --- means job position
4--------- MGR ---------NUMBER(4) means an employee's leader number
5 ------HIREDATE ------ ---DATE------------ means hire date
6 -------SAL ------------NUMBER(7,2) ---- --Indicates monthly salary, salary
7 ------COMM ---------NUMBER(7,2)------ Indicates bonus or commission
8------ DEPTNO--- ----- NUMBER(2)------- Indicates the department number

DEPT (Department table) Department table: Indicates the specific information of a department
NO ------ Field ---------------- Type ------------- ---Description
1------ DEPTNO -------NUMBER(2)--------- Department Number
2 -------DNAME------- - VARCHAR2(14)---- Department Name
3--------- LOC ----------VARCHAR2(13)------ Department Location

BONUS (bonus table) bonus table: Indicates an employee's salary and bonus. NO ------- Field ----- Type ----- Description
1 ---- ----ENAME------- VARCHAR2(10) ------Employee Name
2--------- JOB----------- VARCHAR2(9 )------- Employee Job
3------- SAL------------- NUMBER------------- Employee Salary
4 ------ COMM--------- NUMBER------------- Employee bonus

SALGRADE (salary grade table) A company has a grade system, using this table to represent a salary grade
NO--------- Field---------Type--------- -----------Description
1 ----------GRAD--------- NUMBER--------- Grade Name
2---- ----- LOSAL------- NUMBER- ------- Minimum wage for this grade

Test questions

–SQL practice training one

1. Select the employee in department 30 select * from emp where deptno=30;
2. Retrieve the employee name, monthly income and department number in the emp table select ename,sal,deptno from emp;
3. Retrieve the employee name in the emp table, and employment time (the employment time is displayed according to yyyy-mm-dd) select ename employee name, concat('¥', round(sal 12)) annual income from emp;
4. Retrieve the department number and type of work in the emp table, and remove Repeat row select distinct deptno, job from emp;
5. Retrieve the name of the employee in the emp table and the annual monthly income select ename, sal
12 from emp;
6. Use the name to display the employee's name, and the annual income to display the annual monthly income. select ename,sal from emp where sal>2000;
7. Retrieve the names and monthly income of employees with monthly income greater than 2000 select ename,sal from emp where sal>2000
; Monthly income and employment timeselect ename,sal,hiredate from emp where sal between 1000 and 2000;
9. Retrieve employee names starting with S and monthly incomeselect ename,sal from emp where ename like 'S%';
10. Retrieve emp The name and department number
11 of the employee whose monthly income is 800 or 1250 in the table, and the information of all employees whose position is CLERK in department 20
12. Display the information of all employees whose salary is higher than 2500 or whose position is MANAGER
13. Retrieve the name, monthly income and bonus of the employee with bonus in the
emp table 14. Retrieve the name, monthly income and commission of the employee whose department number is 30 in the emp table, And ask its results to be displayed in ascending order of monthly income, then in descending order of commission
15, list the names, numbers and departments of all clerks
16, find out the employees with higher commission than salary
17, find all managers in department 10 and all managers in department 20 Details of all clerks
18. Find all managers in department 10, all clerks in department 20, details of all employees who are neither managers nor clerks but whose salary is >= 2000
19. Find different jobs of employees who receive bonuses
20. Find employees who do not receive bonuses or receive less than 100 bonuses.
21. Find all employees
who are employed on the third-to-last day of each month
. Employed Employee
24, Show Employee Name Exactly 6 Characters
25, Show Employee Name Without 'R'
26, Show Employee Details, Sort by Name
27, Show Employee Name, Based on Years of Service Oldest employee first
28 Displays the names, jobs and salaries of all employees, sorted in descending order by job, and ascending salary if jobs are the same
29 Displays the names of all employees and the year and month they joined the company, sorted by Ordered by month on which employee was hired, with oldest year items first

30. Display the daily salaries of all employees in a 30-day
month Anywhere in the field, include the names of all employees with "A" 34, display the years of service of all employees in year, month, and day35 , select the names of employees who have bonuses in the company (COMM is not empty, and not 0), Salary and bonus ratio, in reverse order of salary, reverse order of bonus ratio. 36. Select the name and job of the employee who has no manager in the company. 37. Select the name and employment time of the employee who was hired in 1987. 38. Select on the 20th or 10th The name and department number of the employee who works in the department Name and salary of employees who are not in 5000 to 12000 41. Query the name and department number of employees whose employee number is 7934. 42. Query the name and salary of employees whose salary is greater than 1200.










The question is relatively simple, if you need the answer, you can leave a message in the comment area

Guess you like

Origin blog.csdn.net/weixin_50843918/article/details/123446103