Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. SQL Statements
  2.  
  3. Purpose: Practice running SQL statements.
  4.  
  5. Prepares you for: Assignment 4
  6.  
  7. Get Started
  8.  
  9. Log in to the virtual desktop
  10. Start SQL Server 2014 Management Studio
  11. Start SQL Server 2014 Management Studio
  12.  
  13. Note: This activity uses the AdventureWorks2014 database, which is available within the virtual desktop.
  14. Run Statements
  15.  
  16. Run the following SQL statements on SQL Server using the SQL Server Management Studio.
  17.  
  18. Example 1
  19.  
  20. Retrieves all records from the table in descending order.
  21.  
  22. USE AdventureWorks2014;
  23. SELECT *
  24. FROM HumanResources.Department
  25. ORDER BY DepartmentID DESC
  26. Example 2
  27.  
  28. Retrieves records that satisfy the WHERE clause.
  29.  
  30. USE AdventureWorks2014;
  31. SELECT *
  32. FROM HumanResources.Department
  33. WHERE DepartmentID > 4
  34. Example 3
  35.  
  36. Shows the use of the BETWEEN operator.
  37.  
  38. USE AdventureWorks2014;
  39. SELECT
  40. AccountNumber,
  41. SalesOrderID,
  42. OrderDate
  43. FROM Sales.SalesOrderHeader
  44. WHERE
  45. OrderDate BETWEEN '7/1/2011' AND '12/31/2011'
  46. Example 4
  47.  
  48. Shows the use of the AND operator.
  49.  
  50. USE AdventureWorks2014;
  51. SELECT
  52. AccountNumber,
  53. SalesOrderID,
  54. OrderDate
  55. FROM Sales.SalesOrderHeader
  56. WHERE
  57. OrderDate >= '5/1/2011 00:00:00' AND OrderDate<= '12/31/2011'
  58. Example 5
  59.  
  60. Shows the use of the AND and BETWEEN operators together.
  61.  
  62. USE AdventureWorks2014;
  63. SELECT
  64. SalesOrderDetailID,
  65. OrderQty,
  66. ProductID,
  67. ModifiedDate
  68. FROM Sales.SalesOrderDetail
  69. WHERE
  70. ModifiedDate BETWEEN '5/1/2011' AND '12/31/2011' AND
  71. ProductID = 776
  72. Example 6
  73.  
  74. Shows the use of the IN operator.
  75.  
  76. USE AdventureWorks2014;
  77. SELECT
  78. SalesOrderDetailID,
  79. OrderQty,
  80. ProductID,
  81. ModifiedDate
  82. FROM Sales.SalesOrderDetail
  83. WHERE
  84. ProductID IN (776, 778, 747, 809)
  85. Example 7
  86.  
  87. Shows the use of the LIKE operator with a % wildcard.
  88.  
  89. USE AdventureWorks2014;
  90. SELECT *
  91. FROM HumanResources.Department
  92. WHERE
  93. Name LIKE 'Pr%'
  94. Example 8
  95.  
  96. Shows the use of the AS operator with an alias.
  97.  
  98. USE AdventureWorks2014;
  99. SELECT
  100. DepartmentID,
  101. Name AS DepartmentName,
  102. GroupName AS DepartmentGroupName
  103. FROM HumanResources.Department
  104. Example 9
  105.  
  106. Shows the use of INNER JOIN to join two tables based on the condition you specify.
  107.  
  108. USE AdventureWorks2014;
  109. SELECT
  110. p.FirstName,
  111. p.LastName,
  112. ea.EmailAddress
  113. FROM Person.Person AS p
  114. INNER JOIN Person.EmailAddress AS ea
  115. ON p.BusinessEntityID = ea.BusinessEntityID
  116. Example 10
  117.  
  118. Another example of INNER JOIN to join two tables.
  119.  
  120. USE AdventureWorks2014;
  121. SELECT
  122. p.ProductID,
  123. p.Name AS ProductName,
  124. sd.OrderQty,
  125. sd.UnitPrice
  126. FROM Production.Product AS p
  127. INNER JOIN Sales.SalesOrderDetail sd
  128. ON p.ProductID = sd.ProductID
  129. Example 11
  130.  
  131. Shows how UNION can be used to combine two SELECT statements into one result.
  132.  
  133. USE AdventureWorks2014;
  134. SELECT
  135. Name AS ProductName
  136. FROM Production.Product
  137. WHERE
  138. Color = 'Black'
  139. UNION
  140. SELECT
  141. Name AS ProductName
  142. FROM Production.Product
  143. WHERE
  144. Color = 'Silver'
  145. Example 12
  146.  
  147. Uses the CAST function to convert data from one data type to another.
  148.  
  149. USE AdventureWorks2014;
  150. SELECT TOP(10)
  151. SalesOrderNumber, TotalDue,
  152. CAST(TotalDue AS decimal(10,2)) AS TotalDueCast
  153. FROM Sales.SalesOrderHeader;
  154. Example 13
  155.  
  156. Shows the use of OUTER JOIN to join two tables based on the condition you specify.
  157.  
  158. USE AdventureWorks2014;
  159. SELECT p.Name, pr.ProductReviewID
  160. FROM Production.Product p
  161. LEFT OUTER JOIN Production.ProductReview pr
  162. ON p.ProductID = pr.ProductID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement