-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHavingAndJoin.sql
58 lines (42 loc) · 1.68 KB
/
HavingAndJoin.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
--Using having clause and join
use BikeStores
--reading products with total revenue higher than 80000
select product_id,
CAST(SUM(quantity*list_price*(1-discount)) as money) as [Total Revenue]
from sales.order_items
group by product_id
having SUM(quantity*list_price*(1-discount)) > 80000
order by product_id
--reading employee names and customerID's using join
select ss.first_name+SPACE(1)+ss.last_name as [Full Name],
customer_id
from sales.orders so
join sales.staffs ss on so.staff_id=ss.staff_id
group by customer_id,ss.first_name+SPACE(1)+ss.last_name
--Using another database to practice
use Northwind
--Reading products that have been sold order by order dates
select p.ProductID,
p.ProductName,
p.UnitPrice,
CAST(DAY(o.OrderDate) as varchar)+'.'+CAST(MONTH(o.OrderDate) as varchar)+'.'+CAST(YEAR(o.OrderDate) as varchar) as [Order Date]
from Products p
join [Order Details] od on p.ProductID=od.ProductID
join Orders o on od.OrderID=o.OrderID
order by 4 asc
--Reading orders with a revenue of higher than 5000
select ProductID,
CAST(SUM(UnitPrice*Quantity*(1-Discount)) as smallmoney) as [Total Purchase Revenue]
from [Order Details]
group by [ProductID]
having SUM(UnitPrice*Quantity*(1-Discount)) > 5000
order by [Total Purchase Revenue] asc
--Reading number of countries grouped by category name that have been made a sell
select CategoryName,
Count(distinct o.ShipCountry) as [Number of Countries]
from Categories c
join Products p on p.CategoryID=c.CategoryID
join [Order Details] od on p.ProductID=od.ProductID
join Orders o on od.OrderID=o.OrderID
group by CategoryName
order by 2 desc