Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. -- FROM clause subquery
  2.  
  3. -- Return the values for five products with the
  4.  
  5. -- lowest-fat content in the Dairy department
  6.  
  7.  
  8.  
  9. SELECT fat_content
  10.  
  11. FROM (
  12.  
  13. SELECT DISTINCT fat_content
  14.  
  15. FROM product_dimension
  16.  
  17. WHERE department_description
  18.  
  19. IN ('Dairy') ) AS food
  20.  
  21. ORDER BY fat_content
  22.  
  23. LIMIT 5;
  24.  
  25.  
  26.  
  27. -- vmart_query_02.sql
  28.  
  29. -- WHERE clause subquery
  30.  
  31. -- Asks for all orders placed by stores located in Massachusetts
  32.  
  33. -- and by vendors located elsewhere before March 1, 2003:
  34.  
  35.  
  36.  
  37. SELECT order_number, date_ordered
  38.  
  39. FROM store.store_orders_fact orders
  40.  
  41. WHERE orders.store_key IN (
  42.  
  43. SELECT store_key
  44.  
  45. FROM store.store_dimension
  46.  
  47. WHERE store_state = 'MA')
  48.  
  49. AND orders.vendor_key NOT IN (
  50.  
  51. SELECT vendor_key
  52.  
  53. FROM public.vendor_dimension
  54.  
  55. WHERE vendor_state = 'MA')
  56.  
  57. AND date_ordered < '2003-03-01';
  58.  
  59.  
  60.  
  61. -- vmart_query_03.sql
  62.  
  63. -- Noncorrelated subquery
  64.  
  65. -- Requests female and male customers with the maximum
  66.  
  67. -- annual income from customers
  68.  
  69.  
  70.  
  71. SELECT customer_name, annual_income
  72.  
  73. FROM public.customer_dimension
  74.  
  75. WHERE (customer_gender, annual_income) IN
  76.  
  77. (SELECT customer_gender, MAX(annual_income)
  78.  
  79. FROM public.customer_dimension
  80.  
  81. GROUP BY customer_gender);
  82.  
  83.  
  84.  
  85. -- vmart_query_04.sql
  86.  
  87. -- IN predicate
  88.  
  89. -- Find all products supplied by stores in MA
  90.  
  91.  
  92.  
  93. SELECT DISTINCT s.product_key, p.product_description
  94.  
  95. FROM store.store_sales_fact s, public.product_dimension p
  96.  
  97. WHERE s.product_key = p.product_key
  98.  
  99. AND s.product_version = p.product_version
  100.  
  101. AND s.store_key IN (
  102.  
  103. SELECT store_key
  104.  
  105. FROM store.store_dimension
  106.  
  107. WHERE store_state = 'MA')
  108.  
  109. ORDER BY s.product_key;
  110.  
  111.  
  112.  
  113. -- vmart_query_05.sql
  114.  
  115. -- EXISTS predicate
  116.  
  117. -- Get a list of all the orders placed by all stores on
  118.  
  119. -- January 2, 2003 for the vendors with records in the
  120.  
  121. -- vendor_dimension table
  122.  
  123.  
  124.  
  125. SELECT store_key, order_number, date_ordered
  126.  
  127. FROM store.store_orders_fact
  128.  
  129. WHERE EXISTS (
  130.  
  131. SELECT 1
  132.  
  133. FROM public.vendor_dimension
  134.  
  135. WHERE public.vendor_dimension.vendor_key = store.store_orders_fact.vendor_key)
  136.  
  137. AND date_ordered = '2003-01-02';
  138.  
  139.  
  140.  
  141. -- vmart_query_06.sql
  142.  
  143. -- EXISTS predicate
  144.  
  145. -- Orders placed by the vendor who got the best deal
  146.  
  147. -- on January 4, 2004
  148.  
  149.  
  150.  
  151. SELECT store_key, order_number, date_ordered
  152.  
  153. FROM store.store_orders_fact ord, public.vendor_dimension vd
  154.  
  155. WHERE ord.vendor_key = vd.vendor_key
  156.  
  157. AND vd.deal_size IN (
  158.  
  159. SELECT MAX(deal_size)
  160.  
  161. FROM public.vendor_dimension)
  162.  
  163. AND date_ordered = '2004-01-04';
  164.  
  165.  
  166.  
  167. -- vmart_query_07.sql
  168.  
  169. -- Multicolumn subquery
  170.  
  171. -- Which products have the highest cost,
  172.  
  173. -- grouped by category and department
  174.  
  175.  
  176.  
  177. SELECT product_description, sku_number, department_description
  178.  
  179. FROM public.product_dimension
  180.  
  181. WHERE (category_description, department_description, product_cost) IN (
  182.  
  183. SELECT category_description, department_description,
  184.  
  185. MAX(product_cost) FROM product_dimension
  186.  
  187. GROUP BY category_description, department_description);
  188.  
  189.  
  190.  
  191. -- vmart_query_08.sql
  192.  
  193. -- Using pre-join projections to answer subqueries
  194.  
  195. -- between online_sales_fact and online_page_dimension
  196.  
  197.  
  198.  
  199. SELECT page_description, page_type, start_date, end_date
  200.  
  201. FROM online_sales.online_sales_fact f, online_sales.online_page_dimension d
  202.  
  203. WHERE f.online_page_key = d.online_page_key
  204.  
  205. AND page_number IN
  206.  
  207. (SELECT MAX(page_number)
  208.  
  209. FROM online_sales.online_page_dimension)
  210.  
  211. AND page_type = 'monthly' AND start_date = '2003-06-02';
  212.  
  213. -- vmart_query_09.sql
  214.  
  215. -- Equi join
  216.  
  217. -- Joins online_sales_fact table and the call_center_dimension
  218.  
  219. -- table with the ON clause
  220.  
  221.  
  222.  
  223. SELECT sales_quantity, sales_dollar_amount, transaction_type, cc_name
  224.  
  225. FROM online_sales.online_sales_fact
  226.  
  227. INNER JOIN online_sales.call_center_dimension
  228.  
  229. ON (online_sales.online_sales_fact.call_center_key
  230.  
  231. = online_sales.call_center_dimension.call_center_key
  232.  
  233. AND sale_date_key = 156)
  234.  
  235. ORDER BY sales_dollar_amount DESC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement