I will write my solution to the code challenge here, along with notes, diagrams, etc.
You have a database with the following tables:
tblDimDate
- a table of dates, i.e., a calendartblOrder
- a table of 'Orders', also referred to as CampaignstblAdvertiserLineItem
- a table of 'Advertiser Line Items' (ALI for short).
Each ALI is a component of a campaign.
Therefore, the relation of tblAdvertiserLineItem to tblOrder is many-to-one, with the foriegn key relationship described below.
Use the sample data and schema descriptions below to provide the following queries:
Write an SQL query to return all months in the current year for which there are exactly 30 days.
SELECT iYear, sMonth, COUNT(iMonthDay) FROM tblDimDate GROUP BY iYear, sMonth HAVING iYear = YEAR(CURDATE()) AND COUNT(iMonthDay) = 30;
tblDimDate
should have one row (one date) for every date between the first date and the last date in the table. Write a SQL query to determine how many dates are missing, if any, between the first date and last date. You do not need to supply a list of the missing dates.
SELECT day
FROM (SELECT generate_series(MIN(DateDay),
MAX(DateDay),
day) AS day
FROM tblDimDate) AS all_dates
WHERE day NOT IN
(SELECT dayDate
FROM tblDimDate);
SELECT
DATE_FORMAT(
ADDDATE('2008-01-01', @num:=@num+1),
'%Y-%m-%d'
) day
FROM
tblDimDate,
(SELECT @num:=-1) num
WHERE day NOT IN dayDate;
Write an SQL query to identify all orders scheduled to run in November 2021, for which there are not yet any records in tblAdvertiserLineItem
.
Write an SQL query to count total number of campaigns in tblOrder
grouped by campaign duration.
Campaign duration would be the number of days between dateStart
and dateEnd
.
Database design:
What are the advantages and disadvantages of creating and using normalized tables?
What are the advantages and disadvantages of creating and using non-normalized tables?