一、实验目的:
掌握嵌套查询使我们可以用多个简单查询构成复杂的查询,从而增强SQL的查询能力。
二、实验内容
嵌套查询:
SELECT EmployeeID,EmployeeName
FROM Employee
WHERE DepartmentID=
(
SELECT DepartmentID
FROM Employee
WHERE EmployeeName='章宏'
)
连接查询
SELECT E2.EmployeeID ,E2.EmployeeName
FROM Employee E1 JOIN Department D
ON E1.EmployeeName='章宏'
JOIN Employee E2
ON E2.DepartmentID=E1.DepartmentID
GROUP BY E2.EmployeeID,E2.EmployeeName
SELECT EmployeeName,Sex,Salary
FROM Employee
WHERE BirthDate=
(
SELECT MAX(BirthDate)
FROM Employee
SELECT EmployeeName,Salary
FROM Employee
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employee
嵌入查询:
SELECT EmployeeName,Salary
FROM Employee
WHERE EmployeeID NOT IN
(
SELECT EmployeeID
FROM Sell_Order
)
连接查询:
SELECT E.EmployeeName,E.Salary
FROM Employee E LEFT OUTER JOIN Sell_Order S
ON (E.EmployeeID=S.EmployeeID)
WHERE S.SellOrderID IS NULL
嵌套查询:
SELECT CompanyName,Address
FROM Customer
WHERE CustomerID IN
(
SELECT CustomerID
FROM Sell_Order
WHERE ProductID=
(
SELECT ProductID
FROM Product
WHERE ProductName='牛奶'
)
)
连接查询:
SELECT C.CompanyName,C.Address
FROM Customer C JOIN Sell_Order S
ON C.CustomerID=S.CustomerID
JOIN Product P
ON S.ProductID=P.ProductID AND P.ProductName='牛奶'
嵌套查询:
SELECT CompanyName,Phone
FROM Customer
WHERE CustomerID IN
(
SELECT CustomerID
FROM Sell_Order
GROUP BY CustomerID
HAVING SUM(SellOrderNumber)>400
)
连接查询:
SELECT C.CompanyName,C.Phone
FROM Customer C JOIN Sell_Order S
ON C.CustomerID=S.CustomerID
GROUP BY C.CompanyName,C.Phone
HAVING SUM(S.SellOrderNumber)>400
)