select *from emp where deptno=
(select deptno from emp where ename="smith")
這是查詢出與smith同一部門的員工資訊。
-----------------------------------------------------------------------------------------------------
select *from emp,(select avg(sal) mysal,detno from emp group by deptno) tem
where emp.deptno=tem.deptno and emp.sal>tem.mysal
這是查出高于對應部門平均工資的員工資訊。
---------------------------------------------------------------------------------------------
select *from emp where job in (select distinct job from emp where deptno=10)
這是查詢與部門id為10的部門裡面的工作相同的人員資訊
---------------------------------------------------------------------------------------------
select top*4 from emp order by hiredate
查出表的前四條資訊
select top6 *from emp where empno not in (select top4 empno from emp order by hiredate)
order by hiredate
查出5-10的資訊條,
這裡使用的是先把前4條資訊的empno查詢出來,然後使用not in ,來再查詢前6條,
這樣達到查詢出5-10的資訊條
---------------------------------------------------------------------------------------------
insert into 表名 (字段,字段) select 字段 ,字段 from 表名
簡單的多條資料壓力測試
---------------------------------------------------------------------------------------------
删除資料庫表中的重複資料
如果是查詢的話,思路就是利用distict直接把不唯一的資料查詢出來;
但是如果是永久性删除重複資料的話,這就得:
先用distict把資料庫表裡面的不唯一資料先查詢出來,放進一個臨時表裡面;
然後直接把原表裡面的資料全部删除;
最後把臨時表裡面的不唯一資料全部添加進原表裡面,這樣就達到删除資料庫表中的重複資料。
select distict * into 臨時表名 from 原表名
delect *from 原表名
insert into 原表名 select * from 臨時表名
drop table 臨時表名
---------------------------------------------------------------------------------------------
select replace('XX' , 'xx') from 表名
----------------------------------------------------------------------------------------------
select ename ,datepart(year,hiredate) y,datepart(month,hiredate) m from emp
order by m,y
按照年和月去對職工排序,先優先對月去排序,再對年去排序,如果月相同,再對年排序
這個給datepart取别名的操作很重要,可以用與後面的函數調用
----------------------------------------------------------------------------------------------
select *from emp where sal>(select sal from emp where name='xxx')
查出工資大于名字為‘xxx’的員工資訊
----------------------------------------------------------------------------------------------
select count(*),deptid from emp group by deptid having count(*)>1
查出部門成員大于1的部門,用having count(*)>1作為條件部分
-----------------------------------------------------------------------------------------------
select emp.ename,boss.ename from work emp,work boss where emp.bossid=boss.eid
查詢出員工資訊及其上級的資訊
在這基礎上擴充,查出進入公司的時間比其上司晚的員工資訊:
select emp.ename,emp.hiredate,boss.ename ,boss.hiredate from work emp,work boss where emp.bossid=boss.eid
and emp.hiredate>boss.hiredate
----------------------------------------------------------------------------------------------
列出部門資訊和部門的員工資訊,同時也把沒有員工的部門資訊列出來
select dep.dname,emp.ename from department dep,emp where dep.depid=emp.eid
以上隻能夠查詢出部門資訊和部門的員工資訊。
但是那些沒有員工的部門資訊沒列出來。
這時候需要用到的是右外連接配接,就是以department表為主,右連接配接上emp表,
這樣部門全部都能列出來,然後員工的話看具體情況顯示,沒有就顯示‘NULL’值。
select dep.dname,emp.ename from department emp right jion department dep on
emp.depid=dep.depid
如果是用左外連的話,那就是 emp left jion department
----------------------------------------------------------------------------------------------
select name ,salary from employee where salary >
(select max(salary) from employee where depid=2 )
查詢出工資高于某個部門員工工資的員工資訊
這邊在()裡面的sql語句使用max函數是必須的,因為>這個符号是不能與多條資訊做比較的
。用max的話傳回資料就是一條。
----------------------------------------------------------------------------------------------
select count(*) 總人數,avg(sal)平均工資,avg(datediff(year,hiredate,getdate()))平均服務年限,
deptno from emp group by depid
在SQL server裡面給表起别名加不加as 都oK