Advertisement
TareSedam

SQL Azemovic

Mar 14th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. --Join
  2. --Single result(tabela koja postoji samo u runtime-u)
  3. --najcesce se koristi preko FK i PK (jer ima smisla)
  4. -- inner join // vraca iste zapise
  5. -- outer join // iste i razlicite zapise (left or right)
  6. -- UNION kombinuje rezultate dvije ili vise SELECT komandi u jedan izlaz
  7. --EXCEPT eliminiΕ‘e sve zapise iz prvog seta koji postoje u drugom rezultatu
  8. --INTERSECT proizvodi izlaz tako Ε‘to identifikuje iste zapise u oba skupa podataka
  9. --TOP omogucava limitranje izlaza na odredjeni broj zapisa (u komb sa ORDER BY)
  10. --TABLESAMPLE vraca slucajno odabrane zapise (u komb sa PERCENT ili ROWS)
  11. USE AdventureWorks2014
  12. GO
  13. SELECT P.ProductNumber, R.Comments
  14. FROM Production.Product P
  15. INNER JOIN Production.ProductReview R
  16. ON P.ProductID = R.ProductID
  17. --2:
  18.  
  19. SELECT P.ProductNumber, R.Comments
  20. FROM Production.Product P
  21. LEFT OUTER JOIN Production.ProductReview R
  22. ON P.ProductID = R.ProductID
  23. ORDER BY R.Comments DESC
  24.  
  25. --3:
  26. SELECT P.ProductNumber, R.Comments
  27. FROM Production.Product P
  28. RIGHT OUTER JOIN Production.ProductReview R
  29. ON P.ProductID = R.ProductID
  30.  
  31. --4:
  32. SELECT P.ProductNumber AS Broj, R.Comments
  33. FROM Production.Product P
  34. FULL OUTER JOIN Production.ProductReview R
  35. ON P.ProductID = R.ProductID
  36.  
  37. --5:
  38. SELECT P.ProductNumber, R.Comments
  39. FROM Production.Product P
  40. CROSS JOIN Production.ProductReview R
  41.  
  42. --6:
  43. SELECT P.Name AS Produkt, L.Name AS Lokacija, I.Quantity AS Kvantitet
  44. FROM Production.Product P
  45. INNER JOIN Production.ProductInventory I
  46. ON P.ProductID=I.ProductID
  47. INNER JOIN Production.Location L
  48. ON L.LocationID = I.LocationID
  49.  
  50. --7:
  51. SELECT P1.Name, P1.ListPrice
  52. FROM Production.Product P1
  53. INNER JOIN Production.Product AS P2
  54. ON P1.ProductSubcategoryID=P2.ProductSubcategoryID
  55. GROUP BY P1.Name, P1.ListPrice
  56. HAVING P1.ListPrice > AVG (P2.ListPrice)
  57.  
  58. --8:
  59. SELECT P.Name AS Product, V.Name AS Vendor, M.Name AS Measure
  60. FROM Production.Product P
  61. INNER JOIN Purchasing.ProductVendor PV
  62. ON P.ProductID = PV.ProductID
  63. INNER JOIN Purchasing.Vendor V
  64. ON PV.BusinessEntityID=V.BusinessEntityID
  65. INNER JOIN Production.UnitMeasure M
  66. ON M.UnitMeasureCode=PV.UnitMeasureCode
  67.  
  68. --9:
  69. SELECT ProductModelID AS [Product ID],
  70. ModifiedDate AS [Date of modification]
  71. FROM Production.ProductModelProductDescriptionCulture
  72. UNION
  73. SELECT ProductModelID AS [Product ID],
  74. ModifiedDate AS [Date of modification]
  75. FROM Production.ProductModel
  76.  
  77. --10:
  78.  
  79. SELECT ProductModelID AS [Product ID],
  80. ModifiedDate AS [Date of modification]
  81. FROM Production.ProductModelProductDescriptionCulture
  82. EXCEPT
  83. SELECT ProductModelID AS [Product ID],
  84. ModifiedDate AS [Date of modification]
  85. FROM Production.ProductModel
  86.  
  87. --11:
  88.  
  89. SELECT ProductModelID AS [Product ID],
  90. ModifiedDate AS [Date of modification]
  91. FROM Production.ProductModelProductDescriptionCulture
  92. INTERSECT
  93. SELECT ProductModelID AS [Product ID],
  94. ModifiedDate AS [Date of modification]
  95. FROM Production.ProductModel
  96.  
  97. --12:
  98. SELECT TOP 10 P.Name AS Product, L.Name AS Location, I.Quantity
  99. FROM Production.Product P
  100. INNER JOIN Production.ProductInventory I
  101. ON P.ProductID=I.ProductID
  102. INNER JOIN Production.Location L
  103. ON L.LocationID=I.LocationID
  104. ORDER BY I.Quantity ASC
  105.  
  106. --13:
  107. SELECT Name AS Product
  108. FROM Production.Product TABLESAMPLE (40 PERCENT)
  109.  
  110. --Q1:
  111. SELECT CR.Name AS Region, SP.Name AS Drzava, COUNT(PA.StateProvinceID) AS UKUPNO
  112. FROM Person.CountryRegion AS CR INNER JOIN Person.StateProvince AS SP
  113. ON CR.CountryRegionCode=SP.CountryRegionCode
  114. INNER JOIN Person.Address AS PA
  115. ON SP.StateProvinceID=PA.StateProvinceID
  116. GROUP BY CR.Name, SP.Name
  117. ORDER BY CR.Name
  118.  
  119. --Q2:
  120. USE pubs
  121. SELECT A.au_fname+A.au_fname
  122. FROM dbo.authors AS A
  123. INNER JOIN dbo.titles AS T
  124. ON A.au_id = T.pub_id
  125.  
  126.  
  127. --Q3:
  128. USE NORTHWND
  129. SELECT P.ProductName AS Produkt, P.UnitPrice AS Cijena, SUM(P.UnitsInStock) AS [Na stanju],
  130. COUNT (OD.ProductID) AS 'Prodano'
  131. FROM Products AS P INNER JOIN [Order Details] AS OD
  132. ON P.ProductID=OD.ProductID
  133.  
  134. --Q4:
  135. SELECT K.Country, K.City, K.CompanyName, COUNT (OrderID) AS "Ukupno narudzbi:"
  136. FROM dbo.Customers AS K INNER JOIN Orders AS O
  137. ON K.CustomerID=O.CustomerID
  138. GROUP BY Country, City, CompanyName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement