SQL题:
查看下面的SQL,
select sale as saleroom
, comm as commission
from tb_emp
where saleroom > 3000;
请问是否能执行成功?请给出理由。
解答:
不能;
因为列的别名不能在where条件使用,具体原因:where子句比select子句先执行,在where执行前列的别名还没有生效。
引自:【每日一练:SQL】where子句使用列别名的解答——debimeng
然而 from后面可以加别名。
因为from是第一个执行的语句。
例如:
– 练习:查询每位员工的年薪(基本工资12):empno,ename,年薪,设置新的表 别名。
select * ,
sal12 as nianxin ,
sal+10000 as tiaoxin
from emp as emp1;