Homework1 SQL
Home Work1
The Address is homework1:sql makesure you have sqlite3 (version > 3.25)
Q1:
SELECT CategoryName
FROM Category
ORDER BY CategoryName ASC;
Q2:
SELECT DISTINCT ShipName, substr(ShipName, 0, instr(ShipName, '-')) as PreHyphen
FROM 'Order'
WHERE ShipName LIKE '%-%'
ORDER BY ShipName ASC;
Q3:
SELECT Id, ShipCountry,
CASE
WHEN ShipCountry IN ('USA', 'Mexico','Canada')
THEN 'NorthAmerica'
ELSE 'OtherPlace'
END
FROM 'Order'
WHERE Id >= 15445
ORDER BY Id ASC
LIMIT 20;
Q4:
SELECT CompanyName, round(delayCnt * 100.0 / cnt, 2) AS pct
FROM (
SELECT ShipVia, COUNT(*) AS cnt
FROM 'Order'
GROUP BY ShipVia
) AS totalCnt
INNER JOIN (
SELECT ShipVia, COUNT(*) AS delaycnt
FROM 'Order'
WHERE ShippedDate > RequiredDate
GROUP BY ShipVia
) AS delayCnt
ON totalCnt.ShipVia = delayCnt.ShipVia
INNER JOIN Shipper on totalCnt.ShipVia = Shipper.Id
ORDER BY pct DESC;
Q5:
SELECT CategoryName
, COUNT(*) AS CategoryCount
, ROUND(AVG(UnitPrice), 2) AS AvgUnitPrice
, MIN(UnitPrice) AS MinUnitPrice
, MAX(UnitPrice) AS MaxUnitPrice
, SUM(UnitsOnOrder) AS TotalUnitsOnOrder
FROM Product INNER JOIN Category on CategoryId = Category.Id
GROUP BY CategoryId
HAVING CategoryCount > 10
ORDER BY CategoryId;
Q6:
SELECT pname, CompanyName, ContactName
FROM (
SELECT pname, min(OrderDate), CompanyName, ContactName
FROM (
SELECT Id AS pid, ProductName AS pname
FROM Product
WHERE Discontinued != 0
) as discontinued
INNER JOIN OrderDetail on ProductId = pid
INNER JOIN 'Order' on 'Order'.Id = OrderDetail.OrderId
INNER JOIN Customer on CustomerId = Customer.Id
GROUP BY pid
)
ORDER BY pname ASC;
Q7:
SELECT
Id
, OrderDate
, PrevOrderDate
, ROUND(julianday(OrderDate) - julianday(PrevOrderDate), 2)
FROM (
SELECT Id
, OrderDate
, LAG(OrderDate, 1, OrderDate) OVER (ORDER BY OrderDate ASC) AS PrevOrderDate
FROM 'Order'
WHERE CustomerId = 'BLONP'
ORDER BY OrderDate ASC
LIMIT 10
);
Q8:
WITH expenditures AS (
SELECT
IFNULL(c.CompanyName, 'MISSING_NAME') AS CompanyName,
o.CustomerId,
ROUND(SUM(od.Quantity * od.UnitPrice), 2) AS TotalCost
FROM 'Order' AS o
INNER JOIN OrderDetail od on od.OrderId = o.Id
LEFT JOIN Customer c on c.Id = o.CustomerId
GROUP BY o.CustomerId
),
quartiles AS (
SELECT *, NTILE(4) OVER (ORDER BY TotalCost ASC) AS ExpenditureQuartile
FROM expenditures
)
SELECT CompanyName, CustomerId, TotalCost
FROM quartiles
WHERE ExpenditureQuartile = 1
ORDER BY TotalCost ASC
Q9:
SELECT RegionDescription, FirstName, LastName, bday
FROM
(
SELECT RegionId AS rid, MAX(Employee.Birthdate) AS bday
FROM Employee
INNER JOIN EmployeeTerritory ON Employee.Id = EmployeeTerritory.EmployeeId
INNER JOIN Territory ON TerritoryId = Territory.Id
GROUP BY RegionId
)
INNER JOIN (
SELECT FirstName, LastName, Birthdate, RegionId, EmployeeId
FROM Employee
INNER JOIN EmployeeTerritory ON Employee.Id = EmployeeTerritory.EmployeeId
INNER JOIN Territory ON TerritoryId = Territory.Id
)
ON Birthdate = bday AND rid = RegionId
INNER JOIN Region ON Region.Id = RegionId
GROUP BY EmployeeId
ORDER BY rid;
Q10:
with p as (
select Product.Id, Product.ProductName as name
from Product
inner join OrderDetail on Product.id = OrderDetail.ProductId
inner join 'Order' on 'Order'.Id = OrderDetail.OrderId
inner join Customer on CustomerId = Customer.Id
where DATE(OrderDate) = '2014-12-25' and CompanyName = 'Queen Cozinha'
group by Product.id
),
c as (
select row_number() over (order by p.id asc) as seqnum, p.name as name
from p
),
flattened as (
select seqnum, name as name
from c
where seqnum = 1
union all
select c.seqnum, f.name || ', ' || c.name
from c join
flattened f
on c.seqnum = f.seqnum + 1
)
select name from flattened
order by seqnum desc limit 1;