--======================查詢=========================
--select * from 表名函數
--簡單查詢---查詢指定列 select 指定的列 from 表名
select id,name,sex,age from Employee
--注:select age*14 from Employee學習
--簡單查詢--查詢前幾行信息 select top 幾行 * from 表名
select top 5 * from Employeespa
--查詢前6名員工的姓名、性別、年齡
select top 6 name,sex,age from Employee排序
--簡單查詢--帶條件的查詢
--查詢年齡大於25的員工
select * from Employee where age>25
--查詢年齡大於等於25 小於等於30的員工
select * from Employee where age>=25 and age<=30
--查詢年齡大於25 的員工的姓名 年齡 性別 生日
select name,age,sex,birthday from Employee where age>25
select * from Employee where age>25 and sex=0select
---=============對查詢結果進行排序==============
--按年齡升序顯示信息
select * from Employee order by age asc
--按年齡降序顯示
select * from Employee order by age desc統計
--簡單查詢-----對查詢結果去除重複數據 關鍵字 distinct
select distinct age from Employee數據
--簡單查詢------給查詢的列起別名 as as能夠省略
select a.Age 年齡,a.Name 姓名 from Employee a查詢
---=========================模糊查詢=============================
--關鍵字是 like
--模糊查詢要配合 通配符 進行
-- 通配符有四種 :% — [] [^]top
--通配符 %:表示任意匹配 0個 或多個字符
-- 查詢姓'王'的員工信息
select * from Employee where name like '王%' --第一個字爲'王'的
--查詢名字以'三'結尾的
select *from Employee where name like '%王' --最後一個字爲'王'的
-- 名字包含'三'的
select * from Employee where name like '%王%' --有'王'的di
--通配符 _:表示任意一個字符
select * from Employee where name like '張_'
select * from Employee where name like '張__' --- 一個'張'加任意一個字, 一個_ 爲一個字
select * from Employee where name like '_張_'
-- [] 和 [^] 的使用
--1) [...]: 表示在指定範圍內
select * from Employee where Age like '2[0,2,1]' --十位爲2 且 個位是0或2或1 的
--2) [ ^...]: 表示不在指定範圍內
select * from Employee where Age like '[^2,1][6,7]' --不是二十幾 或不是十幾 且 個位是6或7的
--Between ... and ...表示範圍
select * from Employee where age between 20 and 30 --年齡在20和30之間
-- in 的查詢 表示包含在其中的
select * from Employee where age in (15,20,25,30) --查找屬於符合其中之一的數據
-- not in 表示不包含在其中的
select * from Employee where age not in (15,20,25,30) --查找出了這幾個的其餘的數據
--【難題】 查詢沒有員工的部門信息
select * from Department where Id not in (select distinct DepartmentId from Employee)
-- 對 Null 值進行查詢 is null
--查詢沒有身份證號的員工
select * from Employee where CardId is Null
select * from Employee where CardId is not Null
--排序 order by 升序【默認】asc / 降序desc
select * from Employee order by age desc
----==================聚合函數:對數據表的數據進行一些簡單地運算,將結果顯示出來=========
---- sum() 求和函數
select sum(age) from Employee
---- avg() 平均值
select avg(age) from Employee
---- max() 最大值
select max(age) from Employee
---- min() 最小值
select min(age) from Employee
---- count() 計數器
select count(age) from Employee
--=================================================================================================
--======== 聚合函數不能與普通列放在一塊兒被查詢出來,除非都用聚合函數 或者用在分組查詢中 ==========
--======== 如 select max(age),name from 表名 錯誤 ==========
----====== select max(age),count(name) from 表名 正確 ==========
--=================================================================================================
--【難題:查詢年齡最大的員工的信息】
-- 法一 嵌套法
select * from Employee where age=(select max(age) from Employee)
--法二 排序法
select top 1 * from Employee order by age desc
--=================== 分組統計查詢 關鍵字:group by 分組列===================
-- 分別=分組統計出男員工 女員工的平均年齡
select avg(age) from Employee group by Sex
--分組統計出每一個部門的員工數量
select count(name),DepartmentId from Employee group by DepartmentId --注意兩個DepartmentId 先後一致性
--==== 對分組查詢設置條件 關鍵字 having
--分別統計出每一個部門的員工數量 只顯示員工數量大於3人的部門
select count(name),DepartmentId from Employee group by DepartmentId having count(name)>3
--=======================================嵌套查詢========================================================
--查詢年齡大於平均年齡的 員工信息
select * from Employee where Age>(select avg(Age) from Employee)
--查詢年齡大於平均年齡的 員工人數
select count(1) from Employee where Age>(select avg(Age) from Employee)
--查詢至少有一個員工的部門信息 【重點】
--查詢部門信息 where 部門編號 in (查詢員工信息的部門編號)
select * from Department where Id in(select distinct DepartmentId from Employee)
select * from Department where Id not in(select distinct DepartmentId from Employee)
--查詢第3條到第6條的 員工信息
-- 查詢前4條 可是 不是前2條 【重點】
select top 4 * from Employee where Id not in(select top 2 Id from Employee)
--2-7條
select top 6 * from Employee where Id not in (select top 1 Id from Employee)
--======== any 、 some 關鍵字 表示一些、其中一個 =========
--select 字段 from 表1 where 字段 > some (select 字段 from 表2)
-- 大於 some/any 至關於大於 最小值
--查詢出年齡大於任意一個女員工年齡的 男員工
select * from Employee where Sex=1 and age> any (select age from Employee where Sex=0)
select * from Employee where Sex=1 and age> some (select age from Employee where Sex=0)
--all 關鍵字
--大於 all 至關於 大於 最大值
--查詢出年齡大於 全部女員工年齡的 男員工
select * from Employee where Sex=1 and age > all (select age from Employee where sex=0)
---=================================================
-- 次序函數 Row_Number(): 給查詢結果添加一個序號列
select ROW_NUMBER() over(order by id ) as 序號, * from Department
select * , ROW_NUMBER() over(order by id) as 序號 from Department
---==================多表鏈接查詢=================
use DBEmp
--好比:
--查詢僱員的姓名和僱員所在部門名稱【10單元】
select eName,dName from dept d inner join emp e on d.deptNo=e.deptNo group by dName
--鏈接查詢:經過鏈接關鍵字,將兩張或多張數據表 鏈接成一張數據表,做爲數據源,放在select *from 後面
--鏈接查詢: 分爲 內鏈接 外鏈接(左外鏈接、右外鏈接、全外鏈接) 自鏈接
--內鏈接:關鍵字 inner join (inner 能夠省略)
--做用: 又叫等值鏈接,將符合鏈接條件的兩個表的數據查詢出來
select * from dept join emp on dept.deptNo=emp.deptNo
--小結:格式:select * from 表1 join 表2 on 表1.id=表2.表1的id
做者還在學習中,發現錯誤的請在評論區留言。 若是有客友以爲文章還行的話,請點波推薦哦👍。 謝謝大家!!