You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| BOTTM | Bottom-Dollar Markets | 12227,40 | Very High |
738
+
739
+
**52. Some Northwind employees are planning a business trip, and would like to visit as many suppliers and customers as possible. For their planning, they’d like
740
+
to see a list of all countries where suppliers and/or customers are based.**
741
+
```SQL
742
+
SELECT Country FROM Suppliers
743
+
UNION
744
+
SELECT Country FROM Customers
745
+
ORDER BY Country
746
+
```
747
+
**Result:**
703
748
749
+
| Country |
750
+
|-------------|
751
+
| Argentina |
752
+
| Australia |
753
+
| Austria |
754
+
| Belgium |
755
+
| Brazil |
756
+
| Canada |
757
+
| Denmark |
758
+
| Finland |
759
+
| France |
760
+
| Germany |
761
+
| Ireland |
762
+
| Italy |
763
+
| Japan |
764
+
| Mexico |
765
+
| Netherlands |
766
+
| Norway |
767
+
| Poland |
768
+
| Portugal |
769
+
| Singapore |
770
+
| Spain |
771
+
| Sweden |
772
+
| Switzerland |
773
+
| UK |
774
+
| USA |
775
+
| Venezuela |
776
+
777
+
**53. The employees going on the business trip don’t want just a raw list of countries, they want more details. We’d like to see one column with the suppliers country and other with customers country.**
778
+
```SQL
779
+
WITH SupplierCountries AS(
780
+
SELECT
781
+
DISTINCT Country
782
+
FROM Suppliers
783
+
),
784
+
CustomerCountries AS(
785
+
SELECT
786
+
DISTINCT Country
787
+
FROM Customers
788
+
)
789
+
SELECT
790
+
s.CountryAs SupplierCountries,
791
+
c.CountryAS CustomerCountry
792
+
FROM SupplierCountries s
793
+
FULL OUTER JOIN Customers c
794
+
ONs.Country=c.Country
795
+
GROUP BYs.Country, c.Country
796
+
```
797
+
**Result:**
704
798
799
+
| SupplierCountries | CustomerCountry |
800
+
|-------------------|-----------------|
801
+
| NULL | Argentina |
802
+
| NULL | Austria |
803
+
| NULL | Belgium |
804
+
| NULL | Ireland |
805
+
| NULL | Mexico |
806
+
| NULL | Poland |
807
+
| NULL | Portugal |
808
+
| NULL | Switzerland |
809
+
| NULL | Venezuela |
810
+
| Australia | NULL |
811
+
| Brazil | Brazil |
812
+
| Canada | Canada |
813
+
| Denmark | Denmark |
814
+
| Finland | Finland |
815
+
| France | France |
816
+
| Germany | Germany |
817
+
| Italy | Italy |
818
+
| Japan | NULL |
819
+
| Netherlands | NULL |
820
+
| Norway | Norway |
821
+
| Singapore | NULL |
822
+
| Spain | Spain |
823
+
| Sweden | Sweden |
824
+
| UK | UK |
825
+
| USA | USA |
826
+
827
+
**54. The output of the above is improved, but it’s still not ideal. What we’d really like to see is the country name, the total suppliers, and the total customers.**
828
+
```SQL
829
+
WITH SupplierCountries AS (
830
+
SELECT
831
+
Country,
832
+
Count(*) AS Total
833
+
FROM Suppliers
834
+
GROUP BY Country
835
+
),
836
+
CustomerCountries AS (
837
+
SELECT
838
+
Country,
839
+
Count(*) AS Total
840
+
FROM Customers
841
+
GROUP BY Country
842
+
)
843
+
SELECT
844
+
ISNULL(SupplierCountries.Country, CustomerCountries.Country) AS Country,
845
+
ISNULL(SupplierCountries.Total,0) AS TotalSuppliers,
846
+
ISNULL(CustomerCountries.Total,0) AS TotalCustomers
**55. Looking at the Orders table—we’d like to show details for each order that was the first in that particular country, ordered by OrderID. So, we need one row per ShipCountry, and CustomerID, OrderID, and OrderDate should be of the first order from that country.**
882
+
```SQL
883
+
WITH CTE as(
884
+
SELECT
885
+
ShipCountry,
886
+
CustomerID,
887
+
OrderID,
888
+
CONVERT(DATE, OrderDate) AS OrderDate,
889
+
ROW_NUMBER() OVER (PARTITION BY ShipCountry ORDER BY ShipCountry, OrderID) AS OrderNumber
**56. There are some customers for whom freight is a major expense when ordering from Northwind. However, by batching up their orders, and making one larger order instead of multiple smaller orders in a short period of time, they could reduce their freight costs significantly.
927
+
Show those customers who have made more than 1 order in a 5 day period. The sales people will use this to help customers reduce their costs.**
928
+
```SQL
929
+
SELECT TOP(10)
930
+
InitialOrder.CustomerID,
931
+
InitialOrder.OrderIDAS InitialOrderID,
932
+
InitialOrder.OrderDateAS InitialOrderDate,
933
+
NextOrder.OrderIDAS NextOrderID,
934
+
NextOrder.OrderDateAS NextOrderDate,
935
+
DATEDIFF(dd, InitialOrder.OrderDate, NextOrder.OrderDate) AS DaysBetween
0 commit comments