T-SQL學習中--子查詢

SQLSELECT productid, productname, unitprice
FROM Production.Products
WHERE unitprice =
  (SELECT MIN(unitprice)
   FROM Production.Products);

檢索Production.Products表中價格最低的產品的productid, productname, unitprice。code

SQLSELECT productid, productname, unitprice
FROM Production.Products
WHERE supplierid IN
  (SELECT supplierid
   FROM Production.Suppliers
   WHERE country = N'Japan');

檢索Production.Products表中由日本供貨商供應的商品的productid, productname, unitprice。產品

SQLSELECT categoryid, productid, productname, unitprice
FROM Production.Products AS P1
WHERE unitprice =
  (SELECT MIN(unitprice)
   FROM Production.Products AS P2
   WHERE P2.categoryid = P1.categoryid);

檢索Production.Products表中同類產品中價格最低的產品。至關於先檢索SELECT categoryid ,MIN(unitprice) FROM production.products GROUP BY categoryid,而後再找出這些價格對應的產品的productid和productname。it

SQLSELECT custid, companyname
FROM Sales.Customers AS C
WHERE EXISTS
  (SELECT *
   FROM Sales.Orders AS O
   WHERE O.custid = C.custid
     AND O.orderdate = '20070212');

檢索在2007年2月12日下過訂單的客戶的客戶編號、公司名。至關於下面這句SQL:io

SQLSELECT custid, companyname 
FROM Sales.Customers AS C 
WHERE custid IN 
    (SELECT custid FROM Sales.Orders
    WHERE orderdate = '20070212');
SQLSELECT custid, companyname
FROM Sales.Customers AS C
WHERE NOT EXISTS
  (SELECT *
   FROM Sales.Orders AS O
   WHERE O.custid = C.custid
     AND O.orderdate = '20070212');

檢索沒有在2007年2月12日下過訂單的客戶的客戶編號、公司名。至關於下面這句SQL:table

SQLSELECT custid, companyname 
FROM Sales.Customers AS C 
WHERE custid NOT IN 
    (SELECT custid FROM Sales.Orders
    WHERE orderdate = '20070212');
SQLSELECT
  ROW_NUMBER() OVER(PARTITION BY categoryid
                    ORDER BY unitprice, productid) AS rownum,
  categoryid, productid, productname, unitprice
FROM Production.Products;

檢索Production.Products表的categoryid, productid, productname, unitprice,並把它按商品類的分區塊,取得每一個商品在各自的商品類中的價格排名(同類商品中最便宜的排在前面)。ast

SQLSELECT categoryid, productid, productname, unitprice
FROM (SELECT
        ROW_NUMBER() OVER(PARTITION BY categoryid
                          ORDER BY unitprice, productid) AS rownum,
        categoryid, productid, productname, unitprice
      FROM Production.Products) AS D
WHERE rownum <= 2;

取得Production.Products表中同類產品價格最低和次低的商品的categoryid, productid, productname, unitprice。
下面的SQL語句與上一條SQL語句等階date

SQLWITH C AS
(
  SELECT
    ROW_NUMBER() OVER(PARTITION BY categoryid
                      ORDER BY unitprice, productid) AS rownum,
    categoryid, productid, productname, unitprice
  FROM Production.Products
)
SELECT categoryid, productid, productname, unitprice
FROM C
WHERE rownum <= 2;
SQLWITH EmpsCTE AS
(
  SELECT empid, mgrid, firstname, lastname, 0 AS distance
  FROM HR.Employees
  WHERE empid = 9

  UNION ALL

  SELECT M.empid, M.mgrid, M.firstname, M.lastname, S.distance + 1 AS distance
  FROM EmpsCTE AS S
    JOIN HR.Employees AS M
      ON S.mgrid = M.empid
)
SELECT empid, mgrid, firstname, lastname, distance
FROM EmpsCTE;

取得HR.Employees表中工號爲9的員工及其上司、上司的上司……的工號,姓名,以及與之的層級關係distance。結果以下表:grid

empid mgrid firstname lastname distance
9 5 Zoya Dolgopyatova 0
5 2 Sven Buck 1
2 1 Don Funk 2
1 NULL Sara Davis 3

HR.Employee整個數據表的數據以下:數據

empid mgrid firstname lastname
1 NULL Sara Davis
2 1 Don Funk
3 2 Judy Lew
4 3 Yael Peled
5 2 Sven Buck
6 5 Paul Suurs
7 5 Russell King
8 3 Maria Cameron
9 5 Zoya Dolgopyatova
相關文章
相關標籤/搜索