-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery2.sql
156 lines (137 loc) · 4 KB
/
SQLQuery2.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
--1.
SELECT c.name Country,p.name Province
FROM Person.CountryRegion c JOIN Person.StateProvince p ON c.CountryRegionCode = p.CountryRegionCode
--2.
SELECT c.name Country,p.name Province
FROM Person.CountryRegion c JOIN Person.StateProvince p ON c.CountryRegionCode = p.CountryRegionCode
WHERE c.name IN ('Germany','Canada')
--3.
SELECT p.ProductName,o.OrderDate
FROM Orders o
JOIN [Order Details] d ON o.OrderID = d.OrderID
JOIN Products p ON d.ProductID = p.ProductID
WHERE o.OrderDate >= DATEADD(YEAR,-25,GETDATE())
--4.
SELECT p.ProductName,Count(o.OrderDate) Sales
FROM Orders o
JOIN [Order Details] d ON o.OrderID = d.OrderID
JOIN Products p ON d.ProductID = p.ProductID
WHERE o.OrderDate >= DATEADD(YEAR,-25,GETDATE())
GROUP BY p.ProductName
--5.
SELECT City,Count(CustomerID) numCustomer
FROM Customers
GROUP BY City
--6.
SELECT City,Count(CustomerID) numCustomer
FROM Customers
GROUP BY City
HAVING Count(CustomerID) > 2
--7.
SELECT c.ContactName, Count(d.Quantity) numProducts
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN [Order Details] d ON o.OrderID = d.OrderID
GROUP BY c.ContactName
--8.
SELECT c.CustomerID, Count(d.Quantity) numProducts
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN [Order Details] d ON o.OrderID = d.OrderID
GROUP BY c.CustomerID
HAVING Count(d.Quantity)>100
--9.
SELECT s.CompanyName [Supplier Company Name], sh.CompanyName [Shipping Company Name]
FROM Suppliers s CROSS JOIN Shippers sh
--10.
SELECT p.ProductName,o.OrderDate
FROM Orders o
JOIN [Order Details] d ON o.OrderID = d.OrderID
JOIN Products p ON d.ProductID = p.ProductID
ORDER BY o.OrderDate
--11.
SELECT e.FirstName+' '+e.LastName [Name1], m.FirstName+' '+m.LastName [Name2]
FROM Employees e JOIN Employees m ON e.Title = m.Title
WHERE e.EmployeeID != m.EmployeeID
--12.
SELECT m.FirstName + ' ' + m.LastName AS Manager, COUNT(e.EmployeeID) numEmployees
FROM Employees e INNER JOIN Employees m ON e.ReportsTo = m.EmployeeID
GROUP BY m.FirstName + ' ' + m.LastName
HAVING COUNT(e.EmployeeID) > 2
--13.
SELECT City, CompanyName, ContactName, 'Constomer' [Type]
FROM Customers
UNION ALL
SELECT City, CompanyName, ContactName, 'Supplier' [Type]
FROM Suppliers
ORDER BY City
--14.
SELECT City
FROM Customers
UNION
SELECT City
FROM Employees
--15.
--a
SELECT City
FROM Customers
WHERE City NOT IN (SELECT City FROM Employees)
--b
SELECT c.City
FROM Customers c
LEFT JOIN Employees e ON c.City=e.City
WHERE e.City IS NULL
--16.
SELECT p.ProductName, Count(d.Quantity) Quantity
FROM Products p LEFT JOIN [Order Details] d ON p.ProductID = d.ProductID
GROUP BY p.ProductName
--17.
--a.
SELECT City, Count(CustomerID) numCustomers
FROM Customers
GROUP BY City
HAVING Count(CustomerID)=2
UNION
SELECT City, Count(CustomerID) numCustomers
FROM Customers
GROUP BY City
HAVING Count(CustomerID)>2
--b.
SELECT City, Count(CustomerID) numCustomers
FROM Customers
GROUP BY City
HAVING Count(CustomerID)>=2
--18.
SELECT c.City,Count(p.ProductID)num
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN [Order Details] d ON o.OrderID = d.OrderID
JOIN Products p ON d.ProductID = p.ProductID
JOIN (
SELECT DISTINCT c.City,p.CategoryID
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN [Order Details] d ON o.OrderID = d.OrderID
JOIN Products p ON d.ProductID = p.ProductID
)dd ON c.City = dd.City
GROUP BY c.City
HAVING Count(p.ProductID)>2
--19.
SELECT TOP 5 p.ProductID,SUM(d.Quantity) Quantity, AVG(d.UnitPrice) AveragePrice, RANK() OVER (ORDER BY SUM(d.Quantity)) RNK
FROM Products p
JOIN [Order Details] d ON p.ProductID = d.ProductID
JOIN Orders o ON o.OrderID = d.OrderID
GROUP BY p.ProductID
--20.
SELECT City
FROM (
SELECT d.ShipCity, Count(d.OrderID)
FROM Orders d
GROUP BY d.ShipCity
) s1 JOIN
(
SELECT e.City, d.EmployeeID
FROM Orders d
JOIN Employees e ON e.EmployeeID = d.EmployeeID
) s2 ON s1.ShipCity = s2.City
--21. Update the table using distinct clause