diff --git a/advanced-sql b/advanced-sql new file mode 160000 index 0000000000000000000000000000000000000000..397735feedb529ae759f963f7327c7e84ae1a19f --- /dev/null +++ b/advanced-sql @@ -0,0 +1 @@ +Subproject commit 397735feedb529ae759f963f7327c7e84ae1a19f diff --git "a/\344\275\234\344\270\232/SQLQuery2.sql" "b/\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d1569d5d187439795e0b45e016b48b48f7f2e0aa --- /dev/null +++ "b/\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,33 @@ + --创建表 + --部门信息表(sectionInfo) + create table sectionInfo( + SectionID int primary key identity(1,1), + sectionName varchar(10) not null + ); + insert into sectionInfo values('销售部'),('人事部'),('产品部'),('市场部'),('电商部'); + select * from sectionInfo + + --员工信息表(userInfo) + create table userInfo( + userNo int primary key identity(1,1) not null, + userName varchar(10) not null check(len(userName)>4) unique(userName), + userSex varchar(2) not null check(userSex='男' or userSex='女'), + userAge int not null check(len(userAge)>1 and len(userAge)<100), + userAddress varchar(50) default('湖北'), + userSection int references sectionInfo(SectionID), + ); + insert into userInfo(userName,userSex,userAge,userAddress,userSection) values + ('张三2222','男',22,'湖北',1), + ('李四1111 ','男',24,'湖北',2),('莉莉5555','女',21,'湖北',3), + ('菲菲6666','女',26,'湖北',4),('天天3333','男',25,'湖北',5); + select * from userInfo + --员工考勤表(workInfo) + create table workInfo( + workId int primary key identity(1,1) not null, + userId int references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' + or workDescription='旷工'or workDescription='病假' or workDescription='事假') + ); + insert into workInfo values(1,'2022-03-29 12:25:46','早退'),(2,'2022-03-29 12:25:46','迟到'), + (3,'2022-03-29 12:25:46','旷工'),(4,'2022-03-29 12:25:46','病假'),(5,'2022-03-29 12:25:46','事假'); \ No newline at end of file diff --git "a/\344\275\234\344\270\232/SQLQuery6.sql" "b/\344\275\234\344\270\232/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f7b076e5b63039769991b53c198ecb3e8b884625 --- /dev/null +++ "b/\344\275\234\344\270\232/SQLQuery6.sql" @@ -0,0 +1,46 @@ +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +select * from People P +inner join Department D on P.DepartmentId=D.DepartmentId +where PeopleAddress='武汉' +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select * from People P +inner join Department D on P.DepartmentId=D.DepartmentId +inner join Rank R on P.RankId=R.RankId +where PeopleAddress='武汉' +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 +select D.DepartmentName,count(D.DepartmentId)员工人数,SUM(P.PeopleSalary)员工工资总和,AVG(P.PeopleSalary)平均工资,MAX(P.PeopleSalary)最高工资,MIN(P.PeopleSalary)最低工资 from People P +inner join Department D on P.DepartmentId=D.DepartmentId +group by D.DepartmentName +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 +select D.DepartmentName,COUNT(D.DepartmentId)员工人数,SUM(P.PeopleSalary)员工工资总和,AVG(P.PeopleSalary)平均工资,MAX(P.PeopleSalary)最高工资,MIN(P.PeopleSalary)最低工资 from People P +inner join Department D on P.DepartmentId=D.DepartmentId +group by D.DepartmentName +having AVG(P.PeopleSalary)>=10000 +order by AVG(P.PeopleSalary) desc +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select D.DepartmentName,R.RankName,count(D.DepartmentId)员工人数,SUM(P.PeopleSalary)员工工资总和,AVG(P.PeopleSalary)员工平均工资,max(P.PeopleSalary)最高工资,min(P.PeopleSalary)最低工资 from People P +inner join Department D on P.DepartmentId=D.DepartmentId +inner join Rank R on P.RankId=R.RankId +group by D.DepartmentName, R.RankName +--6.查询出巨蟹 6.22--7.22 的员工信息 +select * from People +where (MONTH(PeopleBirth)=6 and DAY(PeopleBirth)>=22) or (MONTH(PeopleBirth)=7 and DAY(PeopleBirth)<=22) +--7.查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +select *, +( + case + when YEAR(PeopleBirth)%12 =0 then '鼠' + when YEAR(PeopleBirth)%12 =1 then '牛' + when YEAR(PeopleBirth)%12 =2 then '虎' + when YEAR(PeopleBirth)%12 =3 then '兔' + when YEAR(PeopleBirth)%12 =4 then '龙' + when YEAR(PeopleBirth)%12 =5 then '蛇' + when YEAR(PeopleBirth)%12 =6 then '马' + when YEAR(PeopleBirth)%12 =7 then '羊' + when YEAR(PeopleBirth)%12 =8 then '猴' + when YEAR(PeopleBirth)%12 =9 then '鸡' + when YEAR(PeopleBirth)%12 =10 then '狗' + else '猪' + end +)属相 + from People \ No newline at end of file diff --git "a/\347\254\224\350\256\260/9.15\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.md" "b/\347\254\224\350\256\260/9.15\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..976dcefbe9be443765de28fba3185448a2446711 --- /dev/null +++ "b/\347\254\224\350\256\260/9.15\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.md" @@ -0,0 +1,66 @@ +9.15寤哄簱寤鸿〃绾︽潫 + +1. + +if exists (select * from sys.databases where name='DBTEST') + drop database DBTEST + +鍒ゆ柇鏄惁鏈夊悓鍚嶆暟鎹簱锛屽鏋滄湁鍚屽悕鏁版嵁搴撳垯鍒犻櫎鍘熸湁鏁版嵁搴擄紙鎱庨噸鍒犻櫎锛 + +2. + + create database DBTEST 鍒涘缓鏁版嵁搴 + +3. + +create table 琛ㄥ悕锛堬級锛涘垱寤鸿〃 + +4. + +鏂板鏁版嵁 + +insert [into] 琛ㄥ悕(瀛楁鍚) values(鍊) + +[]涓殑鍐呭鍙湁鍙棤 + +鏂板鍒 + +alter table 琛ㄥ悕 add 鍒楀悕 varchar(20) + +濡傛灉娌$粰鍑哄垪鍚嶏紝榛樿鏄寜鐓ч『搴忎竴涓釜娣诲姞 + +5.娣诲姞澶栭敭 + +琛ㄥ唴娣诲姞 + +鍒楀悕 int references 鍏宠仈澶栭敭鐨勮〃鍚(鍒楀悕) + +琛ㄥ娣诲姞 + +淇敼琛ㄧ粨鏋 + +琛ㄥ悕 add constraint 绾︽潫鍚 foreign key(瑕佸紩鐢ㄧ殑瀛楁) references 涓婚敭琛(瀛楁) + +6.鍒犻櫎琛 + +drop table 琛ㄥ悕 + +7.闈炵┖ + +not null + +鑷 + +identity + +8.绾︽潫 + +primary key 涓婚敭 + +default 榛樿 + +unique 鍞竴 + +check 妫鏌 + +娌℃湁鐗瑰畾椤哄簭锛屽悓鏃剁敓鏁 \ No newline at end of file diff --git "a/\347\254\224\350\256\260/\346\237\245\350\257\2421.md" "b/\347\254\224\350\256\260/\346\237\245\350\257\2421.md" new file mode 100644 index 0000000000000000000000000000000000000000..d54ca67cafef4a421deb630aa7bef4e7abf07aca --- /dev/null +++ "b/\347\254\224\350\256\260/\346\237\245\350\257\2421.md" @@ -0,0 +1,86 @@ +--鏌ヨ鍏徃鍛樺伐鎵鍦ㄥ煄甯傦紙涓嶉渶瑕侀噸澶嶆暟鎹級 +--distinct +select distinct PeopleAddress from People + +--鏌ヨ鎵鏈夊憳宸ヤ俊鎭(鏍规嵁宸ヨ祫鎺掑簭(order by)锛岄檷搴忔帓鍒) +select * from People order by PeopleSalary desc + + +--鏌ヨ1980骞翠互鍚庡嚭鐢熺殑鍛樺伐淇℃伅 +select * from People where year(PeopleBirth)>=1980 + + +--鏌ヨ鍑哄法锜 6.22--7.22 鐨勫憳宸ヤ俊鎭 + +--鏌ヨ鍑哄拰璧典簯鍦ㄥ悓涓涓煄甯傜殑浜 + + +select * from People where PeopleAddress=(select peopleAddress from People where PeopleName='璧典簯') + +--鏌ヨ鍑虹數璇濆彿鐮佸紑澶138鐨勫憳宸ヤ俊鎭,绗4浣嶅彲鑳芥槸7锛屽彲鑳8 锛屾渶鍚庝竴涓彿鐮佹槸5 +select * from people +where PeoplePhone like '138[7-9]%5' --789 + +--鏌ヨ鍑虹數璇濆彿鐮佸紑澶133鐨勫憳宸ヤ俊鎭,绗4浣嶆槸2-5涔嬮棿鐨勬暟瀛 锛屾渶鍚庝竴涓彿鐮佷笉鏄2鍜3 +select * from people +where PeoplePhone like '138[2-5]%[^23]' + + +--姹傚嚭骞撮緞姣斿钩鍧囧勾榫勯珮鐨勪汉鍛樹俊鎭 +--骞冲潎骞撮緞锛氫粖骞-鍑虹敓骞翠唤 +select avg(year(getdate())-year(peoplebirth)) 骞冲潎骞撮緞 from People + +select * from people where (year(getdate())-year(peoplebirth))>(select avg(year(getdate())-year(peoplebirth)) 骞冲潎骞撮緞 from People) + +--骞冲潎宸ヨ祫 +select avg(PeopleSalary) 骞冲潎宸ヨ祫 from People + +--淇濈暀灏忔暟鐐瑰悗2浣 +--round(灏忔暟,淇濈暀浣嶆暟) +select round(avg(PeopleSalary),2) 骞冲潎宸ヨ祫 from People + +--convert 02154.13200 decimal(5,2):淇濈暀灏忔暟鍚2浣嶏紝鏈夋晥鏁板瓧鎬诲叡鏄5 + +--convert鍜宑ast 鍙互寮哄埗杞崲鏁版嵁绫诲瀷 +select convert(decimal(13,2),avg(PeopleSalary)) 骞冲潎宸ヨ祫 from People + +--cast +select cast(avg(PeopleSalary) as decimal(13,2)) 骞冲潎宸ヨ祫 from People + + +--鏍规嵁鍛樺伐鎵鍦ㄥ湴鍖哄垎缁勭粺璁″憳宸ヤ汉鏁帮紝鍛樺伐宸ヨ祫鎬诲拰锛屽钩鍧囧伐璧勶紝鏈楂樺伐璧勫拰鏈浣庡伐璧勶紝 +--瑕佹眰绛涢夊嚭鍛樺伐浜烘暟鑷冲皯鍦2浜哄強浠ヤ笂鐨勮褰曪紝骞朵笖1985骞村強浠ュ悗鍑鸿韩鐨勫憳宸ヤ笉鍙備笌缁熻銆 +--count(*):绌哄间篃缁熻 count(瀛楁)锛氬綋鍓嶅瓧娈电殑绌哄间笉缁熻 +select count(*) 鍛樺伐浜烘暟,sum(PeopleSalary) 宸ヨ祫鎬诲拰, max(PeopleSalary) 鏈楂樺伐璧,min(PeopleSalary) 鏈浣庡伐璧 from People +where year(PeopleBirth)<1985 +group by PeopleAddress +having count(*)>=2 + + +--鍒犻櫎鏁版嵁 +delete from People where PeopleName='铚楃墰' + +--鍒犻櫎 +--drop 鍒犻櫎鏁村紶琛 +--truncate鍒犻櫎鏁村紶琛紝琛ㄨ繕鍦 + +--鏍规嵁閮ㄩ棬鍒嗙粍缁熻鍛樺伐浜烘暟锛屽憳宸ュ伐璧勬诲拰锛屽钩鍧囧伐璧勶紝鏈楂樺伐璧勫拰鏈浣庡伐璧勶紝骞冲潎宸ヨ祫鍦10000 浠ヤ笅鐨勪笉鍙備笌缁熻锛屽苟涓旀牴鎹钩鍧囧伐璧勯檷搴忔帓鍒椼 +select count(*) 鍛樺伐浜烘暟, sum(PeopleSalary) 鍛樺伐宸ヨ祫鎬诲拰,convert(decimal(15,2),avg(PeopleSalary)) 骞冲潎宸ヨ祫 from Department de +join People po on de.DepartmentId = po.DepartmentId +group by po.DepartmentId having avg(PeopleSalary)>=10000 +order by 骞冲潎宸ヨ祫 desc + +create table B( + cid int primary key, + cname varchar(20) +) + + +create table A( + sid int, + sname varchar(10), + cid int references B(cid) +) + +select * from A +left join B on A.cid=B.cid \ No newline at end of file