练习4:常见函数

博客介绍了SQL中单行函数和分组函数的应用。单行函数部分包含显示系统时间、查询员工信息及工资提升结果、姓名排序与长度计算等示例;分组函数部分则有查询工资最值、平均值、总和,计算入职时间间隔天数,统计特定部门员工个数等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.单行函数

1.显示系统时间(注:时间+日期)

select now();

2.查询员工号,姓名,工资,以及工资提高20%之后的结果(new salary)

select employee_id,last_name,salary,salary*1.2 as 'new salary' from employees;

3.将员工的姓名按首字母排序,并写出姓名的长度

select last_name,length(last_name) from employees order by substr(last_name,1,1) asc;

4.做一个查询,产生下面的结果
<last_name> earns monthly but wants <salary*3>

Dream salary
K_ing earns 24000 monthly but wants 72000

select concat(last_name,'earns',salary,'monthly but wants',salary*3) as 'Dream salary' from employees where last_name = 'K_ing';

5.使用case-when ,按照下面的条件
job grade
ad_pres A
st_man B
it_prog C
sa_red D
st_clerk E
产生下面的结果
last_name job_id grade
king ad_pres A

select job_id as job,
case job_id 
when 'ad_pres' then 'A'
when 'st_man' then 'B'
when 'it_prog' then 'C'
when 'sa_red' then 'D'
when 'st_clerk' then 'E'
end as grade
from employees
where job_id = 'ad_pres';

2.分组函数

1.查询公司员工工资的最大值,最小值,平均值,总和

select max(salary)最大值,min(salary) 最小值,avg(salary) 平均值,sum(salary) 总和,count(salary) 个数 from employees;

2.查询员工表中的最大入职时间和最小入职时间的相差天数(diffrence)

注:datediff()函数查询日期之间间隔。ps select datediff(now(),'1997-9-27');

select datediff(max(hiredate),min(hiredate)) as diffrence from employees;

3.查询部门编号为90的员工个数

select count(*) from employees where department_id = 90;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值