CREATE TABLE T_Employee (FNumber VARCHAR(20),FName VARCHAR(20),FAge INT,FSalary NUMERIC(10,2),PRIMARY KEY (FNumber));
INSERT INTO T_Employee(FNumber,FName,FAge,FSalary) VALUES('DEV001','Tom',25,8300);
INSERT INTO T_Employee(FNumber,FName,FAge,FSalary) VALUES('DEV002','Jerry',28,2300.80);
INSERT INTO T_Employee(FNumber,FName,FAge,FSalary) VALUES('SALES001','John',23,5000);
INSERT INTO T_Employee(FNumber,FName,FAge,FSalary) VALUES('SALES002','Kerry',28,6200);
INSERT INTO T_Employee(FNumber,FName,FAge,FSalary) VALUES('SALES003','Stone',22,1200);
INSERT INTO T_Employee(FNumber,FName,FAge,FSalary) VALUES('HR001','Jane',23,2200.88);
INSERT INTO T_Employee(FNumber,FName,FAge,FSalary) VALUES('HR002','Tina',25,5200.36);
INSERT INTO T_Employee(FNumber,FName,FAge,FSalary) VALUES('IT001','Smith',28,3900);
INSERT INTO T_Employee(FNumber,FAge,FSalary) VALUES('IT002',27,2800);
select *from T_employee
--查询最大年龄人的资料
select * from T_employee where fage=(select max(fage) from T_employee )
--查询工资最高的员工资料
select *from T_employee where fsalary=(select max(fsalary) from T_employee)
--查询年龄最小的员工资料
select * from T_employee where fage=(select min(fage) from T_employee )
--查询工资最低的员工资料
select *from T_employee where fsalary=(select min(fsalary) from T_employee)
--查询单位员工的平均工资
select avg(fsalary) from T_employee
--查询25岁以下的员工的平均工资
select avg(fsalary) from T_employee where fage<=25
--计算每月所有员工的工资总和
select sum(fsalary) from T_employee
--计算公司工资在5000元(含)以上的有多少人
select count(*) from T_employee where fsalary>=5000
--计算公司25岁(含)以下的有多少个员工
select count(*) from T_employee where fage<=25
--打印一张员工资料表,要求按照年龄从小到大排序,如果年龄相同,则再按照工资降序进行排序
select *from T_employee order by fage asc,fsalary desc
--按照工资的由高到低排序,(25岁以下含)
select *from T_employee where fage<=25 order by fsalary desc
--查询由dve+3个任意字符的工号
select *from T_employee where fnumber like 'dev%'
--找出名字第二位为o的所有人的资料
select *from T_employee where fname like '_o%'
--找出所有开发部的员工资料
select *from T_employee where fnumber like 'dev%'
--找出公司年龄为25和23的员工资料
select *from T_employee where fage in(23,25)
--找出所有>=25岁的员工资料,下面的写法对吗?
select *from T_employee where fage>=25 order by fage desc
--找出年龄在25到30岁之间的所有员工
select *from T_employee where fage>=25 and fage<=30 order by fage desc
--在分组的情况下,select 后面只能参与分组的字段和聚合函数,20-30之间
--先执where符选,再分组,最后进行聚合函数的运
select fage as '年龄',count(*) as '人数' from T_employee where fage between 20 and 30 group by fage
--在上一题上, 只找出这个年龄阶段人数>=3的年龄
--执行顺序:先执where符选,再分组,最后进行聚合函数(count(*))的运算,再执行having
select fage as '年龄',count(*) as '人数' from T_employee where fage between 20 and 30 group by fage having count(*)>=3
--查找公司工资最高的前3名
select top 3*from T_employee order by fsalary desc
--查找公司工资最高的前30%名
select top 30 percent *from T_employee order by fsalary desc
--消除的是结果集中,完全相同的记录
select distinct fname,fage,fsalary from T_employee
--联合两个表,两个表的列一定要相同,对应的列一定要相容.
select id,name,age from student union select fnumber,fname,fage from T_employee
--联合两个表,两个表的列一定要相同,对应的列类型一定要相容.(两个表中,有相同数据)
select id,name,age from student union all select fnumber,fname,fage from T_employee
--列出所有员工的资料,并且在最后一行显示员工工资总和
select fnumber,fname,convert(varchar(6),fage)as '年龄',fsalary from T_employee union all select '合计','','',(select sum(fsalary) from T_employee)