SQL如何查询员工ID为1001的信息? 如何查询所有员工的姓名和工资? 如何查询所有员工的入职日期和所在部门? 如何查询所有员工的年龄和性别? 如何查询所有男性员工的信息? 如何查询所有工资大于等于5000的员工信息? 如何查询所有工资在4000到6000之间的员工信息? 如何查询所有入职日期在2019年的员工信息? 如何查询所有职位为经理的员工信息? 如何查询所有在部门D1工作的员工信息? 如何查询所有在部门D1或D2工作的员工信息? 如何查询所有员工的工资总和? 如何查询所有员工的平均工资? 如何查询所有部门的员工总数? 如何查询所有部门的平均工资? 如何查询所有部门的最高工资? 如何查询所有部门的最低工资? 如何查询所有员工的最高工资? 如何查询所有员工的最低工资? 如何查询所有员工的工资排名?
时间: 2024-02-12 18:06:42 浏览: 206
以下是对每个问题的SQL查询解答:
1. 如何查询员工ID为1001的信息?
```
SELECT * FROM employees WHERE employee_id = 1001;
```
2. 如何查询所有员工的姓名和工资?
```
SELECT first_name, last_name, salary FROM employees;
```
3. 如何查询所有员工的入职日期和所在部门?
```
SELECT hire_date, department_name FROM employees JOIN departments ON employees.department_id = departments.department_id;
```
4. 如何查询所有员工的年龄和性别?
假设我们有一个名为 `age` 的函数可以计算员工的年龄:
```
SELECT gender, age(birth_date) as age FROM employees;
```
5. 如何查询所有男性员工的信息?
```
SELECT * FROM employees WHERE gender = 'M';
```
6. 如何查询所有工资大于等于5000的员工信息?
```
SELECT * FROM employees WHERE salary >= 5000;
```
7. 如何查询所有工资在4000到6000之间的员工信息?
```
SELECT * FROM employees WHERE salary BETWEEN 4000 AND 6000;
```
8. 如何查询所有入职日期在2019年的员工信息?
```
SELECT * FROM employees WHERE YEAR(hire_date) = 2019;
```
9. 如何查询所有职位为经理的员工信息?
```
SELECT * FROM employees WHERE job_title = 'Manager';
```
10. 如何查询所有在部门D1工作的员工信息?
```
SELECT * FROM employees JOIN departments ON employees.department_id = departments.department_id WHERE department_name = 'D1';
```
11. 如何查询所有在部门D1或D2工作的员工信息?
```
SELECT * FROM employees JOIN departments ON employees.department_id = departments.department_id WHERE department_name IN ('D1', 'D2');
```
12. 如何查询所有员工的工资总和?
```
SELECT SUM(salary) FROM employees;
```
13. 如何查询所有员工的平均工资?
```
SELECT AVG(salary) FROM employees;
```
14. 如何查询所有部门的员工总数?
```
SELECT department_name, COUNT(*) FROM employees JOIN departments ON employees.department_id = departments.department_id GROUP BY department_name;
```
15. 如何查询所有部门的平均工资?
```
SELECT department_name, AVG(salary) FROM employees JOIN departments ON employees.department_id = departments.department_id GROUP BY department_name;
```
16. 如何查询所有部门的最高工资?
```
SELECT department_name, MAX(salary) FROM employees JOIN departments ON employees.department_id = departments.department_id GROUP BY department_name;
```
17. 如何查询所有部门的最低工资?
```
SELECT department_name, MIN(salary) FROM employees JOIN departments ON employees.department_id = departments.department_id GROUP BY department_name;
```
18. 如何查询所有员工的最高工资?
```
SELECT MAX(salary) FROM employees;
```
19. 如何查询所有员工的最低工资?
```
SELECT MIN(salary) FROM employees;
```
20. 如何查询所有员工的工资排名?
```
SELECT first_name, last_name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank FROM employees;
```
注意:以上示例仅供参考,实际实现可能因数据库结构和具体需求而异。
阅读全文