中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql 子查詢高級經典例題

發布時間:2020-02-29 09:07:27 來源:網絡 閱讀:310 作者:wx5d21d5e6e5ab1 欄目:MySQL數據庫

查詢部門平均工資是最低部門的信息
法1:先找到平均工資和最低平均工資相等的部門編號,然后再作為篩選條件匹配部門表

SELECT d.*
FROM departments d
WHERE d.department_id=(
    SELECT department_id
    FROM employees
    GROUP BY department_id
    HAVING AVG(salary) =
    (
        SELECT MIN(a)
        FROM(
            SELECT AVG(salary) a,department_id
            FROM employees
            GROUP BY department_id
                 )b
    )
)

法2:通過排序然后LIMIT直接找到工資最低的部門標號,然后匹配部門表

SELECT d.*
FROM departments d
WHERE d.department_id=(
    SELECT department_id
    FROM employees
    GROUP BY department_id
    ORDER BY AVG(salary) ASC
    LIMIT 1
);

查詢平均工資最低的部門信息和該部門的平均工資
法:將department表和帶有最低平均工資的表連接起來,再查詢

SELECT d.*,a
FROM departments d
INNER JOIN(
    SELECT AVG(salary) a,department_id
    FROM employees
    GROUP BY department_id
    ORDER BY AVG(salary) ASC
    LIMIT 1
) b
ON d.department_id=b.department_id;

查詢平均工資最高的job信息

SELECT *
FROM jobs
WHERE jobs.`job_id`=(
    SELECT job_id
    FROM employees e
    GROUP BY e.job_id
    ORDER BY AVG(salary) DESC
    LIMIT 1
);

查詢平均工資高于公司平均工資的部門有些
法:查找平均工資高于公司平均工資的表,然后和部門表連接

    SELECT department_name
FROM departments d
INNER JOIN(
    SELECT AVG(salary),department_id
    FROM employees
    GROUP BY department_id
    HAVING AVG(salary)>(
        SELECT AVG(salary)
        FROM employees
    )
) a
WHERE d.department_id=a.department_id;

查詢出公司中所有manager的詳細信息在employees表中

SELECT *
FROM employees
WHERE employee_id IN(
    SELECT manager_id
    FROM employees
);

查詢各個部門中最高工資中的那個部門的最低工資是多少

SELECT MIN(e.salary)
FROM employees e
WHERE e.department_id=(
    SELECT department_id
    FROM employees
    GROUP BY department_id
    ORDER BY MAX(salary) DESC
    LIMIT 1
)

查詢平均工資最高的部門的manager的詳細信息:last_name,department_id,email,salary

SELECT e.last_name,e.department_id,e.email,e.salary
FROM employees e
INNER JOIN departments d
ON d.manager_id=e.employee_id
WHERE d.department_id=(
    SELECT department_id
    FROM employees
    GROUP BY department_id
    ORDER BY AVG(salary) DESC
    LIMIT 1
)
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

鞍山市| 六安市| 文山县| 湖北省| 古浪县| 临潭县| 桃园市| 红原县| 湘乡市| 运城市| 措勤县| 怀集县| 辽阳县| 康平县| 北碚区| 土默特左旗| 宁晋县| 罗江县| 阿鲁科尔沁旗| 晋中市| 东光县| 嘉禾县| 临洮县| 乌拉特前旗| 四川省| 金川县| 天水市| 沭阳县| 锡林浩特市| 秭归县| 宁乡县| 巩义市| 永和县| 奇台县| 陆丰市| 禹州市| 集贤县| 昭苏县| 佛山市| 博客| 淄博市|