員工部門表綜合查詢SQL
阿新 • • 發佈:2018-12-26
[sql] view plain copy print? copy print?
- –資料庫的表設計如下:
- –部門:部門編號,部門名稱,地址;
- –員工:員工編號,員工名字,職務,管理編號,入職日期,薪資,獎金,部門編號;
- –建立部門表:
- CREATETABLE dept(
- deptno INTPRIMARYKEY,dname VARCHAR(20),loc VARCHAR(20)
- );
[sql] view plain copy print?--資料庫的表設計如下: --部門:部門編號,部門名稱,地址; --員工:員工編號,員工名字,職務,管理編號,入職日期,薪資,獎金,部門編號; --建立部門表: CREATE TABLE dept( deptno INT PRIMARY KEY,dname VARCHAR(20),loc VARCHAR(20) );
- –建立員工表:
- CREATETABLE emp(empno INTPRIMARYKEY,ename VARCHAR(20) NOTNULL,
- job VARCHAR(20) CHECK (job IN(‘CLERK’,‘SALESMAN’,‘MANAGER’,‘ANALYST’)),mgp INT,
- hiredate DATETIME ,sal DECIMAL(10,2),comm DECIMAL(10,2),
- deptno INT,
- CONSTRAINT pk_we FOREIGNKEY (deptno) REFERENCES dept (deptno)
- )
--建立員工表:
CREATE TABLE emp(empno INT PRIMARY KEY,ename VARCHAR(20) NOT NULL,
job VARCHAR(20) CHECK (job IN('CLERK','SALESMAN','MANAGER','ANALYST')),mgp INT,
hiredate DATETIME ,sal DECIMAL(10,2),comm DECIMAL(10,2),
deptno INT,
CONSTRAINT pk_we FOREIGN KEY (deptno) REFERENCES dept (deptno)
)
[sql] view plain- –部門表中插入資料:
- INSERTINTO dept VALUES (10,‘ACCOUNTING’,‘NEWTORK’);
--部門表中插入資料:
INSERT INTO dept VALUES (10,'ACCOUNTING','NEWTORK');
[sql] view plain copy print?- INSERTINTO dept VALUES (20,‘RESEARCH’,‘DALLAS’);
INSERT INTO dept VALUES (20,'RESEARCH','DALLAS');
[sql] view plain copy print?- INSERTINTO dept VALUES (30,‘SALES’,‘CHICAGO’);
INSERT INTO dept VALUES (30,'SALES','CHICAGO');
[sql] view plain copy print?- INSERTINTO dept VALUES (40,‘OPERATIONS’,‘BOSTON’);
INSERT INTO dept VALUES (40,'OPERATIONS','BOSTON');
[sql] view plain copy print?- –員工表中插入資料:
- insertinto empvalues(7369,‘SMITH’,‘CLERK’,7902,‘1980-12-17’,1640,NULL,20);
--員工表中插入資料:
insert into empvalues(7369,'SMITH','CLERK',7902,'1980-12-17',1640,NULL,20);
[sql] view plain copy print?- insertinto emp values(7499,‘ALLEN’,‘SALESMAN’,7698,‘1981-2-20’,11400,300,30);
insert into emp values(7499,'ALLEN','SALESMAN',7698,'1981-2-20',11400,300,30);
[sql] view plain copy print?- insertinto empvalues(7521,‘WARD’,‘SALESMAN’,7698,‘1981-2-22’,5200,500,30);
insert into empvalues(7521,'WARD','SALESMAN',7698,'1981-2-22',5200,500,30);
[sql] view plain copy print?- insertinto empvalues(7566,‘JOENS’,‘MANAGER’,7839,‘1981-4-2’,7015,NULL,20);
insert into empvalues(7566,'JOENS','MANAGER',7839,'1981-4-2',7015,NULL,20);
[sql] view plain copy print?- insertinto emp values(7654,‘MARTIN’,‘SALESMAN’,7698,‘1981-9-28’,5200,1400,30);
insert into emp values(7654,'MARTIN','SALESMAN',7698,'1981-9-28',5200,1400,30);
[sql] view plain copy print?- insertinto empvalues(7698,‘BLAKE’,‘MANAGER’,7839,‘1981-5-1’,5900,NULL,30);
insert into empvalues(7698,'BLAKE','MANAGER',7839,'1981-5-1',5900,NULL,30);
[sql] view plain copy print?- insertinto empvalues(7782,‘CLARK’,‘MANAGER’,7839,‘1981-6-9’,2470,NULL,10);
insert into empvalues(7782,'CLARK','MANAGER',7839,'1981-6-9',2470,NULL,10);
[sql] view plain copy print?- insertinto emp values(7788,‘SCOTT’,‘ANALYST’,7566,‘1987-4-19’,3040,NULL,20);
- (1) 查詢獎金高於工資的20%的員工資訊
- select * from emp whereisnull(comm,0)>sal*0.2;
insert into emp values(7788,'SCOTT','ANALYST',7566,'1987-4-19',3040,NULL,20);
(1) 查詢獎金高於工資的20%的員工資訊
select * from emp where isnull(comm,0)>sal*0.2;
[sql] view plain copy print?- (2) 查詢10號部門中工種為MANAGER和20部門中工種為CLERK的員工的資訊
- select * from emp where job=‘MANAGER’and deptno=10 unionselect * from emp where job=‘CLERK’and deptno=20;
- –
- select * from emp where (job=‘MANAGER’and deptno=10) or (job=‘CLERK’and deptno=20);
(2) 查詢10號部門中工種為MANAGER和20部門中工種為CLERK的員工的資訊
select * from emp where job='MANAGER' and deptno=10 unionselect * from emp where job='CLERK' and deptno=20;
--
select * from emp where (job='MANAGER' and deptno=10) or (job='CLERK' and deptno=20);[sql] view plain copy print?- (3) 查詢所有員工工資與獎金的和
- select ename,sal+isnull(comm,0) 實發工資 from emp;
(3) 查詢所有員工工資與獎金的和
select ename,sal+isnull(comm,0) 實發工資 from emp;
[sql] view plain copy print?- (4) 查詢沒有獎金或獎金低於100的員工資訊
- select * from empwhere comm isnullor comm<100;
(4) 查詢沒有獎金或獎金低於100的員工資訊
select * from empwhere comm is null or comm<100;
[sql] view plain copy print?- (5) 查詢各月倒數第3天(倒數第2天)入職的員工資訊
- select * from emp where DATENAME(day,hiredate+3)=1;
(5) 查詢各月倒數第3天(倒數第2天)入職的員工資訊
select * from emp where DATENAME(day,hiredate+3)=1;
[sql] view plain copy print?- (6) 查詢工齡大於或等於25年的員工資訊。
- select ename 姓名,hiredate 僱用日期,datediff(year,hiredate,getdate()) 工齡
- from emp
- where datediff(year,hiredate,getdate())>=25;
(6) 查詢工齡大於或等於25年的員工資訊。
select ename 姓名,hiredate 僱用日期,datediff(year,hiredate,getdate()) 工齡
from emp
where datediff(year,hiredate,getdate())>=25;
[sql] view plain copy print?- (7) 查詢員工資訊,要求以首字母大寫的方式顯示所有員工的姓名
- selectupper(SUBSTRING(ename,1,1))+lower(substring(ename,2,(len(ename)-1)))from emp;
(7) 查詢員工資訊,要求以首字母大寫的方式顯示所有員工的姓名
select upper(SUBSTRING(ename,1,1))+lower(substring(ename,2,(len(ename)-1)))from emp;
[sql] view plain copy print?- (8) 查詢員工名正好為6個字元的員工的資訊
- select ename from emp where len(ename)=6;
(8) 查詢員工名正好為6個字元的員工的資訊
select ename from emp where len(ename)=6;
[sql] view plain copy print?- (9) 查詢員工名字中不包含字母“S”的員工
- select ename from emp where ename notlike‘%S%’;
(9) 查詢員工名字中不包含字母“S”的員工
select ename from emp where ename not like '%S%';
[sql] view plain copy print?- (10) 查詢員工姓名的第二字母為“M”的員工資訊。
- select ename from emp where ename like‘_M%’;
(10) 查詢員工姓名的第二字母為“M”的員工資訊。
select ename from emp where ename like '_M%';
[sql] view plain copy print?- (11) 查詢所有員工姓名的前三個字元
- select ename 員工姓名,substring(ename,1,3)員工姓名的前三個字元 from emp;
(11) 查詢所有員工姓名的前三個字元
select ename 員工姓名,substring(ename,1,3)員工姓名的前三個字元 from emp;
[sql] view plain copy print?- (12) 查詢所有員工的姓名,如果包含字母“S”,則用“s”替換
- –返回被替換了指定子串的字串
- –REPLACE (<string_expression1>,<string_expression2>,<string_expression3>)
- –用string_expression3替換在string_expression1 中的子串string_expression2。
- selectreplace(ename,‘S’,’s’) from emp;
(12) 查詢所有員工的姓名,如果包含字母“S”,則用“s”替換
--返回被替換了指定子串的字串
--REPLACE (<string_expression1>,<string_expression2>,<string_expression3>)
--用string_expression3替換在string_expression1 中的子串string_expression2。
select replace(ename,'S','s') from emp;
[sql] view plain copy print?- (13) 查詢在2月份入職的所有員工資訊
- select * from emp where datename(mm,hiredate)=2;
(13) 查詢在2月份入職的所有員工資訊
select * from emp where datename(mm,hiredate)=2;
[sql] view plain copy print?- (14) 查詢所有員工入職以來的工作期限,用“XX年XX月XX日”的形式表示。
- select ename,datename(yy,hiredate)+‘年’+datename(mm,hiredate)+‘月’+datename(dd,hiredate)+‘日’ 工作期限 from emp;
(14) 查詢所有員工入職以來的工作期限,用“XX年XX月XX日”的形式表示。
select ename,datename(yy,hiredate)+'年'+datename(mm,hiredate)+'月'+datename(dd,hiredate)+'日' 工作期限 from emp;
[sql] view plain copy print?- (15) 查詢至少有一個員工的部門資訊。
- select d.dname,count(empno) 部門人數
- from emp e
- rightjoin dept d on d.deptno=e.deptno
- groupby d.dname,e.deptno
- havingcount(empno)>=1;
(15) 查詢至少有一個員工的部門資訊。
select d.dname,count(empno) 部門人數
from emp e
right join dept d on d.deptno=e.deptno
group by d.dname,e.deptno
having count(empno)>=1;
[sql] view plain copy print?- (16) 查詢所有員工的姓名及其直接上級的姓名。
- select ename 員工的姓名,(
- select ename from emp e2 where e2.empno=e1.mgp
- ) 直接上級
- from emp e1;
(16) 查詢所有員工的姓名及其直接上級的姓名。
select ename 員工的姓名,(
select ename from emp e2 where e2.empno=e1.mgp
) 直接上級
from emp e1;
[sql] view plain copy print?- (17) 查詢入職日期早於其直接上級領導的所有員工資訊
- select ename 員工的姓名,hiredate入職日期,(
- select ename from emp e2 where e2.empno=e1.mgp
- ) 直接上級,(
- select hiredate from emp e2 where e2.empno=e1.mgp
- ) 直接上級入職日期
- from emp e1
- where e1.hiredate<(select hiredate
- from emp e2 where e2.empno=e1.mgp
- );
(17) 查詢入職日期早於其直接上級領導的所有員工資訊
select ename 員工的姓名,hiredate入職日期,(
select ename from emp e2 where e2.empno=e1.mgp
) 直接上級,(
select hiredate from emp e2 where e2.empno=e1.mgp
) 直接上級入職日期
from emp e1
where e1.hiredate<(select hiredate
from emp e2 where e2.empno=e1.mgp
);
[sql] view plain copy print?- (18) 查詢所有部門及其員工資訊,包括那些沒有員工的部門
- select dept.dname,emp.ename
- from dept
- leftouterjoin emp on emp.deptno=dept.deptno;
- (19) 查詢所有員工及其部門資訊,包括那些還不屬於任何部門的員工
- select dept.dname,emp.ename
- from emp
- leftouterjoin dept on emp.deptno=dept.deptno;
(18) 查詢所有部門及其員工資訊,包括那些沒有員工的部門
select dept.dname,emp.ename
from dept
left outer join emp on emp.deptno=dept.deptno;
(19) 查詢所有員工及其部門資訊,包括那些還不屬於任何部門的員工
select dept.dname,emp.ename
from emp
left outer join dept on emp.deptno=dept.deptno;
[sql] view plain copy print?- (20) 查詢所有工種為CLERK的員工的姓名及其部門名稱
- select dept.dname,emp.ename,emp.job
- from emp
- leftouterjoin dept on emp.deptno=dept.deptno
- where job=‘CLERK’;
(20) 查詢所有工種為CLERK的員工的姓名及其部門名稱
select dept.dname,emp.ename,emp.job
from emp
left outer join dept on emp.deptno=dept.deptno
where job='CLERK';