-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_entity.sql
More file actions
171 lines (156 loc) · 3.85 KB
/
Copy pathsingle_entity.sql
File metadata and controls
171 lines (156 loc) · 3.85 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
SET
search_path TO classicmodels;
-- =====================================================
-- Prepare a list of offices sorted by country, state, city.
-- =====================================================
SELECT
*
FROM
offices
ORDER BY
country,
state,
city;
-- =====================================================
-- How many employees are there in the company?
-- =====================================================
SELECT
COUNT(DISTINCT employeenumber)
FROM
employees;
-- =====================================================
-- What is the total of payments received?
-- =====================================================
SELECT
SUM(amount) AS total_payments
FROM
payments;
-- =====================================================
-- List the product lines that contain 'Cars'
-- =====================================================
SELECT
productline
FROM
productlines
WHERE
productline LIKE '%Cars%';
-- =====================================================
-- Report total payments for October 28, 2004.
-- =====================================================
SELECT
SUM(amount) AS october_28_2004_payments
FROM
payments
WHERE
paymentdate = '2004-10-28';
-- =====================================================
-- Report those payments greater than $100,000.
-- =====================================================
SELECT
checknumber,
amount
FROM
payments
WHERE
amount > 100000
ORDER BY
amount DESC;
-- =====================================================
-- List the products in each product line.
-- =====================================================
SELECT
productname,
productline
FROM
products
ORDER BY
productline;
-- =====================================================
-- How many products in each product line?
-- =====================================================
SELECT
productline,
COUNT(productname) AS products_count
FROM
products
GROUP BY
productline
ORDER BY
products_count DESC;
-- =====================================================
-- What is the minimum payment received?
-- =====================================================
SELECT
*
FROM
payments
WHERE
amount = (
SELECT
MIN(amount)
FROM
payments
);
-- =====================================================
-- List all payments greater than twice the average payment.
-- =====================================================
SELECT
*
FROM
payments
WHERE
amount > (
SELECT
2 * AVG(amount)
FROM
payments
);
-- =====================================================
-- What is the average percentage markup of the MSRP on buyPrice?
-- =====================================================
SELECT
ROUND(AVG(100.0 * (msrp - buyprice) / buyprice), 2) AS avg_percentage_markup
FROM
products;
-- =====================================================
-- How many distinct products does ClassicModels sell?
-- =====================================================
SELECT
COUNT(productcode) AS num_products
FROM
products;
-- =====================================================
-- Report the name and city of customers who don't have sales representatives?
-- =====================================================
SELECT
customername,
city
FROM
customers
WHERE
salesrepemployeenumber IS NULL;
-- =====================================================
-- What are the names of executives with VP or Manager in their title?
-- =====================================================
SELECT
firstname || ' ' || lastname AS full_name,
jobtitle
FROM
employees
WHERE
jobtitle ~* '\mVP\M'
OR jobtitle ~* '\mManager\M';
-- =====================================================
-- Which orders have a value greater than $5,000?
-- =====================================================
SELECT
ordernumber,
SUM(quantityordered * priceeach) AS order_amount
FROM
orderdetails
GROUP BY
ordernumber
HAVING
SUM(quantityordered * priceeach) > 5000
ORDER BY
order_amount DESC;