如何构造此SQL SELECT查询?

时间:2012-02-08 14:46:37

标签: sql tsql

我有两张桌子,我需要进行一定的选择。

第一个表是员工,其中包含名称 * Departament_ID *和 Salary 第二个表是 Departament ,其中包含 Departament ID 和* Departament_Name *

我的SQL脚本必须为我提供名称,最大salay 最低工资,其中Departament_ID为'30'< / p>

4 个答案:

答案 0 :(得分:0)

这样的事情可以解决问题(注意:所有拼写都与你在第一篇文章中提到的完全一样)。

   SELECT d.Department_Name AS name,
          MAX(e.salary) AS max_salary,
          MIN(e.salary) AS min_salary
     FROM Department d
LEFT JOIN Employe e ON d.Department_ID = e.Department_ID
    WHERE d.Department_ID = 30

答案 1 :(得分:0)

SELECT d.department_name,
       MIN(e.salary) AS 'Minimum Salary',
       MAX(e.salary) AS 'Maximum Salary'
FROM department d,employee e
WHERE d.department_id=30 AND d.department_id=e.department_id
GROUP BY d.department_name

答案 2 :(得分:0)

SELECT  max(sal) as MaximumSalary
       ,min(sal)  as MinimumSalary
       ,department.Departament_Name
       ,employee.Name
FROM    employee
INNER JOIN department ON
            employee.departmentid = department.department_id
WHERE department.department_id = 31
GROUP BY department.department_id

max和min函数将为您提供salary列的employee表的最大值和最小值。这将要求您使用GROUP BY,因为他们需要从一组数据中计算最大值和最小值,在这种情况下,这是根据部门的最大值/最小值。我们按部门ID分组,因为它是表格的唯一键。内连接在这里加入你拥有的两个表。这是一个内部联接,因为我们希望将部门纳入查询。

答案 3 :(得分:0)

   SELECT d.Department_Name AS name,
      MAX(e.salary) AS max_salary,
      MIN(e.salary) AS min_salary
   FROM Department d
   Inner JOIN Employee ON d.Department_ID = e.Department_ID
   WHERE d.Department_ID = 30