Database query learning (1)

The following exercises include query tables, query columns, query designated columns, group by and order by (ASC and DESC), having, removing duplicates, querying the first few (a) records, between and and, in and collections, fuzzy search, Aggregate functions (count, sum, avg, max, min), field extraction

Based on the CompanySales database.
Please refer to my blog for the database script. Below is the link to the
CompanySales database script link

--1. 查询所有商品信息
select *from Product
--2. 查询所有商品的商品编号、商品名称、单价、库存量
select ProductID,ProductName,Price,ProductStockNumber from Product;
--3. 查询前2种商品的商品编号、商品名称、单价
select Top 2  ProductID,ProductName,Price from Product order by ProductID
--4. 查询前百分之五种商品的商品编号、商品名称、单价
select Top 5 percent  ProductID,ProductName,Price from Product order by ProductID
--5. 查询采购订单中的商品采购自哪几家公司,只显示供应商编号即可(不显示重复值)
select   distinct ProviderID from Provider
--6. 公司打算将商品涨价,请你计算一下如果商品全部涨价5%的价格是多少,查询结果包括商品名称、原单价、涨价后单价,请用汉字显示列标题。
select  ProductName,Price,Price*(1+0.05) as 现价 from Product
--7. 查询1号员工处理了哪些采购订单
select *from Purchase_Order where EmployeeID =1; 
--8. 查询商品表中单价在20至50之间的商品信息(用两种方法写条件)
select *from Product where Price between 20 and 50
select * from Product where price >= 20 and price <=50
--9. 查询员工表中所有姓吴并且名字为三个字的员工信息
select *from Employee where  EmployeeName like '吴_ _'
--10. 查询员工表中所有名字中含有“辉”字的员工信息
select *from Employee where  EmployeeName like '%辉%'
--11. 统计一下商品表中一共有多少种商品
select COUNT(*) from Product;
select COUNT (*) as 共有 from Product; 
--12. 请统计商品表中商品的平均单价和库存总量
select AVG(Price),sum(ProductStockNumber) from Product;
--13. 在销售订单表中统计一共销售了多少种商品(同一商品多个订单只算一种)
SELECT COUNT(DISTINCT(ProductID)) FROM Sell_Order 
--14. 查询价格最高的前2种商品
SELECT TOP 2 * FROM Product ORDER BY Price DESC; 
select Top 2  ProductID,ProductName,Price from Product order by Price DESC
--15. 在采购订单表中统计每位员工负责的采购订单数量,并按数量由大到小排序
select EmployeeID, count(PurchaseOrderNumber)as D from Purchase_order 
order by D DESC
SELECT * FROM Purchase_order  ORDER BY PurchaseOrderNumber DESC; 
--16. 统计销售订单表中每种商品的销售总数量
SELECT ProductID, SUM(SellOrderNumber) FROM Sell_Order GROUP BY ProductID; 
--17. 在销售订单表中统计员工负责订单的数量,显示出订单数至少2笔的员工号
SELECT EmployeeID FROM Sell_Order GROUP BY 
EmployeeID HAVING COUNT(*) >= 2; 
--18. 查询哪些员工在公司就职超过18年
select EmployeeName  from Employee
where Year(GETDATE())-year(HireDate)>=18
select EmployeeName, Year(GETDATE())-year(HireDate) from Employee
where Year(GETDATE())-year(HireDate)>=18
--1.查询Customer中所有客户的信息
select *from Customer
--2.查询Customer中CompanyName,ContactName,Address列
select Customer.CompanyName,Customer.ContactName,Customer.Address from Customer
--3.查询Customer表中前五名客户的CompanyName,ContactName,Address列
select Top 5 CompanyName,ContactName,Address from Customer
--4.查询Customer表中前5%客户的CompanyName,ContactName,Address列
select Top 5 percent CompanyName,ContactName,Address from Customer
--5.查询employee表查询所有员工的DepartmentID,并取消重复记录
select  distinct DepartmentID from Employee
--6.查询employee表查询所有员工的EmployeeName,Sex,并在姓名列上显示员工姓名
select EmployeeName as "员工姓名", Sex from Employee
--7.查询employee表查询所有员工的EmployeeName,Sex,并在姓名列上显示员工姓名,性别列上显示列名
select EmployeeName as "员工姓名", Sex as "性别" from Employee
--【例题8】从员工表employee中查询所有员工的工资Salary在提高10%后的金额,将提高后的工资列标题命名为“提高后工资”。
select  Salary,Salary*(1+0.10) as "提高后工资"  from Employee
--【例题9】统计公司有多少名员工。 
select COUNT(*) from Employee
--【例题10】从员工表employee中查询所有员工的最高和最低工资信息。 
select MAX(Salary) as "最高",MIN(Salary) as "最低"from Employee
--11.在CompanySales,Sell_Order表中查找编号为1,5,7的员工接受订单的信息
select *from Sell_Order where EmployeeID=1 OR EmployeeID=5 OR EmployeeID=7
Select *from Sell_order where EmployeeID in(1,5,7)
--12.在CompanySales,Sell_Order表中查找编号不为1,5,7的员工接受订单的信息
Select *from Sell_order where EmployeeID not in(1,5,7)
select *from Sell_Order where EmployeeID!=1 and EmployeeID!=5 and EmployeeID!=7
--注意用and连接,此处特别容易弄混淆

--13查询员工表姓李和章的员工
--特别注意:like与in不能搭配使用,可用字段提取在用in判断
select *from Employee where  EmployeeName like '李%' OR  EmployeeName like '章%' 
select *from Employee where  EmployeeName like '[李,章]%' 
select *from Employee where LEFT( EmployeeName,1) in ('李','章')
select *from Employee where Substring(EmployeeName,1,1)in ('李','章')
--14查询员工表不姓李的员工
select *from Employee where employeename like '[^李]%'
select *from Employee where substring(employeename,1,1) <>'李' 
select *from Employee where employeename not like '李%'
select *from Employee where LEFT( EmployeeName,1) !='李%'

If you have any questions, please contact me QQ:1207787189

Guess you like

Origin blog.csdn.net/sxh06/article/details/104769663