注册 登录
编程论坛 SQL Server论坛

关键字执行顺序之select-having疑问

tan_qinliang 发布于 2023-01-14 16:45, 791 次点击
网上查询出来的关键字执行顺序都是FROM、ON 、JOIN、WHERE、GROUP BY、AGG_FUNC、WITH、HAVING、SELECT
HAVING 再SELECT前
        SELECT AVG(salary)avg_sal,d.*
        FROM employees e
        JOIN departments d
        ON e.department_id=d.department_id
        GROUP BY department_id
        HAVING avg_sal=(
        
            SELECT MIN(avg_sal.avg)
            FROM (
            
                SELECT AVG(salary) avg
                FROM employees
                GROUP BY department_id
               
                ) avg_sal
        
            );
这段SQL  并没有报错  HAVING后的avg_sal是SELECT中取得别名。
按网上说的顺序,这里avg_sal应该找不到。
有没有大佬讲解讲解。
0 回复
1