-
Notifications
You must be signed in to change notification settings - Fork 0
/
excercises_subquery.sql
171 lines (152 loc) · 4.46 KB
/
excercises_subquery.sql
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
USE TSQLFundamentals2008
-- 1
-- Write a query that returns all orders placed on the last day of
-- activity that can be found in the Orders table
-- Tables involved: TSQLFundamentals2008 database, Orders table
SELECT orderid, orderdate, custid, empid
FROM Sales.Orders
WHERE orderdate = (
SELECT MAX(orderdate)
FROM Sales.Orders
);
-- 2 (Optional, Advanced)
-- Write a query that returns all orders placed
-- by the customer(s) who placed the highest number of orders
-- * Note: there may be more than one customer
-- with the same number of orders
-- Tables involved: TSQLFundamentals2008 database, Orders table
DECLARE @max INT =
(
SELECT MAX(custidcount.count)
FROM (
SELECT COUNT(custid) AS count
FROM Sales.Orders
GROUP BY custid
) AS custidcount
)
SELECT custid, orderid, orderdate, empid
FROM Sales.Orders
WHERE custid IN (
SELECT custid
FROM Sales.Orders as Q
GROUP BY custid
HAVING COUNT(Q.custid) = @max
)
ORDER BY custid;
-- SELECT custid, orderid, orderdate, empid
-- FROM Sales.Orders AS O
-- WHERE O.custid IN (
-- SELECT custid
-- FROM Sales.Orders AS O
-- GROUP BY O.custid
-- HAVING COUNT(*) = (
-- SELECT TOP(1) COUNT(*) AS numordini
-- FROM Sales.Orders AS O2
-- GROUP BY O2.custid
-- ORDER BY COUNT(*) DESC
-- )
-- )
-- 3
-- Write a query that returns employees
-- who did not place orders on or after May 1st, 2008
-- Tables involved: TSQLFundamentals2008 database, Employees and Orders tables
SELECT empid, firstname, lastname
FROM HR.Employees
WHERE empid NOT IN (
SELECT O.empid
FROM Sales.Orders as O
WHERE O.orderdate >= '20080501'
);
-- 4
-- Write a query that returns
-- countries where there are customers but not employees
-- Tables involved: TSQLFundamentals2008 database, Customers and Employees tables
SELECT DISTINCT country
FROM Sales.Customers
WHERE country NOT IN (
SELECT E.country
FROM HR.Employees as E
);
-- 5
-- Write a query that returns for each customer
-- all orders placed on the customer's last day of activity
-- Tables involved: TSQLFundamentals2008 database, Orders table
SELECT custid, orderid, orderdate, empid
FROM Sales.Orders as O1
WHERE orderdate = (
SELECT MAX(customerorderdates.orderdate)
FROM (
SELECT O2.orderdate
FROM Sales.Orders as O2
WHERE O2.custid = O1.custid
GROUP BY O2.orderdate
) AS customerorderdates
)
ORDER BY custid;
-- 6
-- Write a query that returns customers
-- who placed orders in 2007 but not in 2008
-- Tables involved: TSQLFundamentals2008 database, Customers and Orders tables
SELECT C.custid, C.companyname
FROM Sales.Customers AS C
WHERE C.custid IN (
SELECT O.custid
FROM Sales.Orders as O
WHERE YEAR(O.orderdate) = '2007'
) AND C.custid NOT IN (
SELECT O.custid
FROM Sales.Orders as O
WHERE YEAR(O.orderdate) = '2008'
)
-- SELECT C.custid, C.companyname
-- FROM Sales.Customers AS C
-- WHERE EXISTS (
-- SELECT *
-- FROM Sales.Orders AS O
-- WHERE O.custid = C.custid AND YEAR(O.orderdate) = '2007'
-- ) AND NOT EXISTS (
-- SELECT *
-- FROM Sales.Orders AS O
-- WHERE O.custid = C.custid AND YEAR(O.orderdate) = '2008'
-- )
-- 7 (Optional, Advanced)
-- Write a query that returns customers
-- who ordered product 12
-- Tables involved: TSQLFundamentals2008 database,
-- Customers, Orders and OrderDetails tables
SELECT custid, companyname
FROM Sales.Customers
WHERE custid IN (
SELECT custid
FROM Sales.Orders
WHERE orderid IN (
SELECT orderid
FROM Sales.OrderDetails
WHERE productid = '12'
)
);
-- SELECT custid, companyname
-- FROM Sales.Customers AS C
-- WHERE EXISTS (
-- SELECT *
-- FROM Sales.Orders AS O
-- WHERE O.custid = C.custid AND EXISTS (
-- SELECT *
-- FROM sALES.OrderDetails AS OD
-- WHERE OD.orderid = O.orderid AND OD.productid = 12
-- )
-- )
-- 8 (Optional, Advanced)
-- Write a query that calculates a running total qty
-- for each customer and month
-- Tables involved: TSQLFundamentals2008 database, Sales.CustOrders view
SELECT custid, ordermonth, qty, SUM(qty) OVER(PARTITION BY custid ORDER BY ordermonth) AS runqty
FROM Sales.CustOrders
ORDER BY custid, ordermonth
-- SELECT custid, ordermonth, qty, (
-- SELECT SUM(CO2.qty)
-- FROM Sales.CustOrders AS CO2
-- WHERE CO2.custid = CO1.custid AND CO2.ordermonth <= CO1.ordermonth
-- ) AS runqty
-- FROM Sales.CustOrders AS CO1
-- ORDER BY custid, ordermonth