-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab_01_SQL_Query_Fundamentals.sql
More file actions
147 lines (124 loc) · 5.24 KB
/
Copy pathLab_01_SQL_Query_Fundamentals.sql
File metadata and controls
147 lines (124 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- ------------------------------------------------------------------
-- 0). First, How Many Rows are in the Products Table?
-- ------------------------------------------------------------------
SELECT COUNT(*) AS Num_Products FROM northwind.products;
-- ------------------------------------------------------------------
-- 1). Product Name and Unit/Quantity
-- ------------------------------------------------------------------
SELECT product_name
, quantity_per_unit
FROM northwind.products;
-- ------------------------------------------------------------------
-- 2). Product ID and Name of Current Products
-- ------------------------------------------------------------------
SELECT id AS product_id
, product_name
FROM northwind.products
WHERE discontinued != 1;
# better to negate (above says does != 1) bc could be null, but really only discontinued if = 1
-- ------------------------------------------------------------------
-- 3). Product ID and Name of Discontinued Products
-- ------------------------------------------------------------------
SELECT id AS product_id
, product_name
FROM northwind.products
WHERE discontinued != 0;
-- ------------------------------------------------------------------
-- 4). Name & List Price of Most & Least Expensive Products
-- ------------------------------------------------------------------
SELECT product_name
, list_price
FROM northwind.products
WHERE list_price = (SELECT MIN(list_price) FROM northwind.products)
OR list_price = (SELECT MAX(list_price) FROM northwind.products);
# aggregate functions (min / max are examples) need to be in select statements
-- ------------------------------------------------------------------
-- 5). Product ID, Name & List Price Costing Less Than $20
-- ------------------------------------------------------------------
SELECT product_name
, list_price
FROM northwind.products
WHERE list_price < 20.00
ORDER BY list_price DESC;
-- ------------------------------------------------------------------
-- 6). Product ID, Name & List Price Costing Between $15 and $20
-- ------------------------------------------------------------------
# SELECT id AS product_id
SELECT id AS product_id
, product_name
, list_price
FROM northwind.products
WHERE list_price BETWEEN 15 AND 20
ORDER BY list_price DESC;
-- ------------------------------------------------------------------
-- 7). Product Name & List Price Costing Above Average List Price
-- ------------------------------------------------------------------
SELECT product_name
, list_price
FROM northwind.products
WHERE list_price > (SELECT AVG(list_price) FROM northwind.products)
ORDER BY list_price DESC;
-- ------------------------------------------------------------------
-- 8). Product Name & List Price of 10 Most Expensive Products
-- ------------------------------------------------------------------
SELECT product_name
, list_price
FROM northwind.products
ORDER BY list_price DESC
LIMIT 10;
-- ------------------------------------------------------------------
-- 9). Count of Current and Discontinued Products
-- ------------------------------------------------------------------
UPDATE northwind.products SET discontinued = 1 WHERE id = 95;
SELECT CASE discontinued
WHEN 1 THEN 'yes'
ELSE 'no'
END AS is_discontinued
, COUNT(*) as product_count
FROM northwind.products
GROUP BY discontinued;
UPDATE northwind.products SET discontinued = 0 WHERE id = 95;
-- ------------------------------------------------------------------
-- 10). Product Name, Units on Order and Units in Stock
-- Where Quantity In-Stock is Less Than the Quantity On-Order.
-- ------------------------------------------------------------------
SELECT product_name
, reorder_level AS units_in_stock
, target_level AS units_on_order
FROM northwind.products
WHERE reorder_level < target_level;
-- ------------------------------------------------------------------
-- EXTRA CREDIT -----------------------------------------------------
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
-- 11). Products with Supplier Company & Address Info
-- ------------------------------------------------------------------
SELECT p.product_name
, p.list_price AS product_list_price
, p.category AS product_category
, s.company AS supplier_company
, s.address AS supplier_address
, s.city AS supplier_city
, s.state_province AS supplier_state_province
, s.zip_postal_code AS supplier_zip_postal_code
FROM northwind.suppliers s
INNER JOIN northwind.products p
ON s.id = p.supplier_ids;
# inner join - only see rows in table where match occurs
-- ------------------------------------------------------------------
-- 12). Number of Products per Category With Less Than 5 Units
-- ------------------------------------------------------------------
SELECT category
, COUNT(*) as units_in_stock
FROM northwind.products
GROUP BY category
HAVING units_in_stock < 5;
-- ------------------------------------------------------------------
-- 13). Number of Products per Category Priced Less Than $20.00
-- ------------------------------------------------------------------
SELECT category
, COUNT(*) AS product_count
FROM northwind.products
WHERE list_price < 20.00
GROUP BY category
ORDER BY category;