Basic SQL test questions for intermediate engineers

1. Check the employee's full name, email and phone number

SELECT
concat(first_name,',',last_name),email,phone_int
 //将employees表的所有员工的last_name和first_name拼接起来作为Name
FROM employees;

2. Query the full name, monthly salary and annual salary of all employees (monthly salary*12)

SELECT
first_name,last_name,salary,salary*12 
FROM employees;

3. Query the full name, monthly salary and year-end bonus of all employees (annual salary *commission_pct)

SELECT
first_name,last_name,salary,salary*12*ifnull(commission_pct,0)
//IFNULL函数是MySQL控制流函数之一,它接受两个参数,如果不是NULL,则返回第一个参数。否则,IFNULL函数返回第二个参数。
//佣金百分比 (CommissionPCT)但是佣金百分比,数据库里employees表里的一个列。
 FROM employees;

4. Check which departments and positions have employees

SELECT DISTINCT department_id,job_id from employees WHERE department_id is not null
//SELECT DISTINCT用于返回唯一不同的值

5. Query the information of employees who joined the company after 1999

SELECT first_name,last_name,hire_date FROM employees WHERE year(hire_date) >= '1999'

Six, query the company’s boss information

SELECT first_name,last_name FROM employees WHERE manager_id IS NULL

7. Query all employee information, sort by department and annual salary in descending order;

SELECT first_name,last_name,salary*12 as total FROM employees ORDER BY department_id,total DESC
//ORDER BY 关键字用于对结果集按照一个列或者多个列进行排列。
//ORDER BY 关键字默认按照升序进行排序。DESC降序

8. Encrypt the user name, keep the first 3 digits of the employee’s full name, and replace the middle 4 digits with *. If there are extra characters in the name, keep it;

SELECT concat(RPAD(substring(concat(first_name,last_name),1,3),7,'*'),
SUBSTRING(concat(first_name,last_name),8)) 
FROM employees

9. Calculate the employee's name and full salary;

SELECT first_name,last_name,salary,salary*12*(1+ifnull(commission_pct,0)) FROM employees

10. Query the department id of the employee, if there is no department, print "Unassigned Department"

SELECT first_name,last_name,ifnull(department_id,'未分配部门') FROM employees

11. Query the average salary of each department

SELECT department_id,AVG(salary) FROM employees WHERE department_id is not null GROUP BY department_id 

12. Query the departments with average wages higher than 8000 and their average wages

SELECT department_id,AVG(salary) FROM employees WHERE department_id is not null GROUP BY department_id HAVING AVG(salary)>=8000

Thirteen, query the number of times employees have changed jobs

SELECT employee_id,COUNT(employee_id) FROM job_history GROUP BY employee_id

14. Query how many people entered the company in 1995, 96, 97, and 98

SELECT year(hire_date),count(employee_id)
FROM employees
WHERE YEAR(hire_date) in ('1995','1996','1997','1998')
GROUP BY YEAR(hire_date)

15. Output employee name, employee id, and department to which the employee belongs

SELECT first_name,last_name,department_name FROM employees e JOIN departments d ON e.department_id = d.department_id

16. Output employee information, including employee_id, first_name,

    department_id,  department_name   location_id     city
SELECT e.employee_id,e.first_name,d.department_id,d.department_name,l.location_id,l.city
FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id

17. All salaries in the EMPLOYEES table are between the lowest salary and the highest salary in the JOB_GRADES table.

SELECT e.first_name,e.last_name,e.salary
FROM employees e,job_grades j
WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal
SELECT e.first_name,e.last_name,e.salary
FROM employees e JOIN job_grades j ON e.salary BETWEEN j.lowest_sal AND j.highest_sal

18. Query the name, department, address, and city of all employees with bonuses

SELECT e.employee_id,e.first_name,d.department_id,d.department_name,l.location_id,l.city
FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id
WHERE e.commission_pct IS NOT NULL

19. Query the manager information of the employee whose last_name is'Ki'

SELECT first_name,last_name FROM employees WHERE employee_id IN (SELECT manager_id FROM employees WHERE last_name LIKE 'Ki%')
//IN操作符允许where子句中规定多个值
//LIKE操作符用于WHERE子句中搜索列中的指定模式

Twenty, query the company's lowest salary employee information

SELECT first_name,last_name,salary FROM employees WHERE salary = (SELECT MIN(salary) FROM employees)

21. Query the employee with the highest salary of all employees of the company since 1999

SELECT first_name,last_name,hire_date,salary
FROM employees WHERE salary=
(SELECT MAX(salary) FROM employees WHERE year(hire_date)='1999')
AND year(hire_date) = '1999'
//WHERE子句用于过滤记录

.22. Query the information of employees who have done ST_CLERK

SELECT e.first_name,e.last_name,j.job_id FROM employees e JOIN job_history j ON e.employee_id = j.employee_id WHERE j.job_id = 'ST_CLERK'

Guess you like

Origin blog.csdn.net/weixin_44051191/article/details/109294387