Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. Select Statement -
  2.  
  3. - Whitespace DOESN'T matter
  4. - '*' selects ALL columns
  5. - 'TOP N' will return the top 'N' rows from table
  6.  
  7. Example:
  8. SELECT
  9. TOP N column_name(s)
  10. FROM
  11. table_name
  12.  
  13.  
  14. - 'DISTINCT' will return only unique values, not counting duplicates
  15. + if done on multiple columns, it will return all unique combinations of these columns
  16.  
  17. Example:
  18. SELECT
  19. column_name(s)
  20. FROM
  21. table_name
  22.  
  23.  
  24. ---------------------------------------------------------------------------
  25.  
  26.  
  27. Filtering data -
  28.  
  29. - Primarily done using the 'WHERE' clause
  30. + statements that evaluate to 'true' or 'false' for each row
  31.  
  32. Example:
  33. SELECT
  34. column_name(s)
  35. FROM
  36. table_name
  37. WHERE
  38. condition(s)
  39.  
  40. - Comparing strings with wildcards, use the 'LIKE' keyword
  41. + The '%' character represent 0, 1, or more of any character
  42. - WHERE column1 LIKE 'Foo%' ; will match anything that starts with 'Foo'
  43. - WHERE column1 LIKE '%Bar' ; will match anything that ends with 'Bar'
  44. + The '_' character represents a single instance of any character
  45. - WHERE column1 LIKE '_bc' ; will match anything that starts with a single character, followed by 'bc'
  46.  
  47. Example:
  48. SELECT
  49. column_name(s)
  50. FROM
  51. table_name
  52. WHERE
  53. column LIKE 'value%'
  54. OR
  55. column LIKE '_value'
  56.  
  57. - Checking Null values
  58. + Normal operators DO NOT WORK on null values
  59. + To check for null: WHERE column1 IS NULL
  60. + To check for NOT null: WHERE column1 IS NOT NULL
  61.  
  62. Example:
  63. SELECT
  64. column_name(s)
  65. FROM
  66. table_name
  67. WHERE
  68. column value IS NULL
  69. OR
  70. column value IS NOT NULL
  71.  
  72.  
  73. - Comparing to a collection
  74. + Use the 'IN' keyword, followed by the collection
  75.  
  76. Example:
  77. SELECT
  78. column_name(s)
  79. FROM
  80. table_name
  81. WHERE
  82. column IN (value1, value2, ...)
  83.  
  84. - Comparing to a range
  85. + You can compare against a range of values
  86. + To do this, use 'BETWEEN' keyword, followed by the min and max values
  87.  
  88. Example:
  89. SELECT
  90. column_name(s)
  91. FROM
  92. table_name
  93. WHERE
  94. column BETWEEN value1 AND value2
  95.  
  96. - Combining where conditions
  97. + You can combine more than 1 codition in a single where clause
  98. + Use any combination ofthe 'AND' and 'OR' keywords
  99. + Use parenthese () to group conditions together and control logic
  100.  
  101. Example:
  102. SELECT
  103. column_name(s)
  104. FROM
  105. table_name
  106. WHERE
  107. (column1 = value1 AND column2 = value2) OR column3 = value3
  108.  
  109.  
  110. ---------------------------------------------------------------------------
  111.  
  112.  
  113. Sorting data -
  114. - Assume all result sets are unsorted
  115. - Use the 'ORDER BY' keyword to sort the data
  116. - Use 'ASC' or 'DESC' to control the sort direction
  117.  
  118. Example:
  119. SELECT
  120. column_name(s)
  121. FROM
  122. table_name
  123. ORDER BY
  124. column ASC
  125. OR
  126. column DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement