\documentclass[a4paper, 11pt]{article}
\usepackage[english]{babel}
%\usepackage[dutch]{babel}
\usepackage{url}


%handy commands
\newcommand{\tab}{\hspace*{2em}}
\newcommand{\tc} [1] {\textsc{#1}}


\title{Assignment 3 - SQL and QBE \\Databases for IN}

\author{Sander van Veen \\ 6167969 \\ \url{sandervv@gmail.com}}

\begin{document}

\maketitle
%table of contents is not really needed here
%\tableofcontents

\setcounter{section}{6}
\section{SQL}

\begin{enumerate}
%7.1
\item{Write a statement for creating the "City" table}

\tc{CREATE TABLE} `City` ( \\
\tab `Name` varchar(35) NOT NULL default '', \\
\tab `Country` varchar(4) NOT NULL default '', \\
\tab `Province` varchar(35) NOT NULL default '', \\
\tab `Population` int(11) default NULL, \\
\tab `Longitude` float default NULL, \\
\tab `Latitude` float default NULL, \\
\tab PRIMARY KEY  (`Name`,`Country`,`Province`) \\
) \tc{ENGINE=MyISAM DEFAULT CHARSET=latin1};

%7.2
\item{Write a statement for deleting the "City" table}

\tc{DROP TABLE }`City` ;

%7.3
\item{Write a statement for deleting the column "Population" from the "City" table}

\tc{ALTER TABLE} `City` \tc{DROP COLUMN} `Population` ;

%7.4
\item{Select all languages spoken in the country "Belgium" and sort them alphabetically (Hint: Use the full
name "Belgium" as the part of your answer)}

\tc{SELECT} `Name` \\
\tc{FROM} `Language` \\
\tc{WHERE} `Country` = "B" \\
\tc{ORDER BY} `Name` ASC ;

%7.5
\item{Select the name of all of the mountains of the country "USA" (the country code of United States of
America) which are more than 2000 meters high}

\tc{SELECT} `Name` \\
\tc{FROM} `Mountain` \\
\tc{WHERE} `Name` \tc{IN} ( \\
\tab \tc{SELECT} `Mountain` \\
\tab \tc{FROM} `geo\_Mountain` \\
\tab \tc{WHERE} `Country` = "USA" \\
) \tc{AND} `Height` $>$ 2000 ;

%7.6
\item{Display the name and the height of all mountains that are located on islands}

SELECT I.Mountain, M.Height \\
FROM `MountainOnIsland` as I \\
LEFT JOIN Mountain as M ON I.Mountain = M.Name ;

%7.7
\item{Display the names of all cities in the country "USA" (the country code of United States of America) and remove possible duplicates}

SELECT DISTINCT Name \\
FROM `City` \\
WHERE Country = ``USA'' ;

%7.8
\item{Display the names of those countries that have deserts or mountains or both of them}

SELECT `Name` \\
FROM `Country` \\
WHERE Code IN ( \\
\tab SELECT DISTINCT `Country` \\
\tab FROM `geo\_Desert` \\
\tab UNION \\
\tab SELECT DISTINCT `Country` \\
\tab FROM `geo\_Mountain` \\
) ;

%7.9
\item{Calculate the total population of the world}

SELECT sum( `Population` ) as `world\_population` \\
FROM `Country` ;

%7.10
\item{Find the number of mountains per each country (by country code)}

SELECT `Country`, count(`Mountain`) as `total\_mountains` \\
FROM `geo\_Mountain` \\
GROUP BY `Country` ;

\setcounter{enumi}{11}
%7.12
\item{Find the rivers that pass through more than three provinces}

SELECT `River`, count(`Province`) as `through\_provinces` \\ 
FROM `geo\_River` \\
GROUP BY `River` \\
HAVING `through\_provinces` $>$ 3 ;

%7.13
\item{Display the names of the two countries that have the longest border}

SELECT `Name` \\
FROM `Country` \\
WHERE `Code` = ( \\
\tab SELECT `Country1` \\
\tab FROM `borders` \\
\tab ORDER BY `Length` DESC \\
\tab LIMIT 1 \\
) \\
OR `Code` = ( \\
\tab SELECT `Country2` \\
\tab FROM `borders` \\
\tab ORDER BY `Length` DESC \\
\tab LIMIT 1 \\
) \\
LIMIT 2;

%7.14
\item{Display the name of the country that has the maximum GDP}

SELECT `C`.`Name` \\
FROM `Economy` as `E` \\
LEFT JOIN `Country` as `C` ON `C`.`Code` = `E`.Country \\
ORDER BY `E`.`GDP` DESC \\
LIMIT 1;

%7.15
\item{Display the name of countries whose people do not speak English}

SELECT `Name` \\
FROM `Country` \\
WHERE `Code` NOT IN ( \\
\tab SELECT `Country` \\
\tab FROM `Language` \\
\tab WHERE `Name` = "English" \\
);

\pagebreak

\setcounter{enumi}{16}

%7.17
\item{Write a statement for inserting a record about a new organization in "Amsterdam"}

%AfDB  	African Development Bank  	Abidjan  	CI  	Cote dIvoire  	1963-08-04
INSERT INTO `Organizations` ( `Abbreviation`, `Name`, 	`City`, `Country`, 	`Province`, `Established` ) \\
VALUES ( "adad", "Adad Ltd", "Amsterdam", "NL", "Noord-Holland", "2010-03-03" ) ;


%7.18
\item{Write a statement for updating the record that you created in question 7.17 by changing the establishment date of the organization}

UPDATE `Organizations` \\
SET `Established` = "2010-03-04" \\
WHERE `Abbreviation` = "adad" ;

%7.19
\item{Write a statement for deleting the record that you created in question 7.17}

DELETE FROM `Organizations` \\
WHERE `Abbreviation` = "adad" ;

\end{enumerate}

\section{QBE}
\begin{enumerate}
%8.1
\item{Select the names of all countries}

%	l, c or r	: justify left, center or right
% |	vertical line
%	||	double vertical line
%	&	column separator
%	\\	start new row
%	\hline	horizontal line

\begin{tabular}{|c|c|c|c|c|c|}
\hline
	Name	&	Code	&	Capital	&	Province	&	Area	&	Population	 \\
\hline
	P.		& 			&			&				&			& \\
\hline
\end{tabular}

%8.2
\item{Find the names of the languages that are spoken in the countries where the mountain called "Everest"
is located}

\begin{tabular}{|c|c|c|c|}
\hline
  geo\_Mountain	& Mountain & 	Country & Province	 \\
\hline
	& Everest  & \_x		& \\
\hline
\end{tabular}


\begin{tabular}{|c|c|c|c|}
\hline
	Languages & Country & Name & Percentage	 \\
\hline
	 & \_x  & P.\_y		& \\
\hline
\end{tabular}

\setcounter{enumi}{3}
%8.4
\item{Find all countries that have at least two spoken languages}

\begin{tabular}{|c|c|c|c|}
\hline
	Languages & Country & Name & Percentage	 \\
\hline
	 & \_x  & \_y		& \\
	 & \_x  & $\neg$\_y		& \\
\hline
\end{tabular}


\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
Country & Name & Code & Capital & Province & Area & Population \\
\hline
 &	P.\_z & \_x  & & & & \\
\hline
\end{tabular}

%8.5
\item{Find the names of those languages that are spoken in at least two countries}

\begin{tabular}{|c|c|c|c|}
\hline
	Languages & Country & Name & Percentage	 \\
\hline
	 & \_x  & P.\_y		& \\
	 & $\neg$\_x  & \_y		& \\
\hline
\end{tabular}

%8.6
\item{Find the total population of Asia}

\begin{tabular}{|c|c|c|}
\hline Continent & Name & Area \\ 
\hline  & Asia & \_x \\ 
\hline \end{tabular} 

\begin{tabular}{|c|c|c|c|c|c|c|}
\hline Country & Name & Code & Capital & Province & Area & Population \\ 
\hline  &  &  &  &  & \_x & P.SUM.UNQ. \\ 
\hline 
\end{tabular} 

%8.7
\item{Find the total number of religions}

\begin{tabular}{|c|c|c|c|}
\hline Religion & Country & Name & Percentage \\ 
\hline  &  & P.UNQ.CNT. &  \\ 
\hline 
\end{tabular} 

%8.8
\item{Find the names of the countries, as well as the types of government in those countries, that have the population growth higher than three percents}

\begin{tabular}{|c|c|c|c|}
\hline Population & Country & Population\_Growth & Infant\_Mortality \\ 
\hline  & \_x & \_y &  \\ 
\hline 
\end{tabular} 

\begin{tabular}{|c|}
\hline Conditions \\ 
\hline \_y $>$ 3  \\ 
\hline
\end{tabular} 

\begin{tabular}{|c|c|c|c|c|c|c|}
\hline Country & Name &	Code &	Capital &	Province &	Area &	Population \\
\hline 		   & P.\_z & \_x	 & 			& 			 &  &   \\ 
\hline 
\end{tabular}


\begin{tabular}{|c|c|c|c|c|}
\hline Politics & Country & Indepencence & Dependent & Government \\
\hline 		   & \_x	 & 			& 			 & P.\_a \\ 
\hline 
\end{tabular}  

%8.9
\item{Find the name of neighboring countries for Switzerland}

\begin{tabular}{|c|c|c|c|}
\hline borders & Country1 & Country2 & Length \\
\hline & Switzerland & P.\_x & \\
\hline 
\end{tabular}  

\begin{tabular}{|c|c|c|c|}
\hline borders & Country1 & Country2 & Length \\
\hline & P.\_x & Switzerland & \\
\hline 
\end{tabular}  


%8.10
\item{Write a QBE query for changing the inflation in France to three percent}

\begin{tabular}{|c|c|c|c|c|c|c|}
\hline Economy & Country & GDP & Agriculture & Service & Industry & Inflation \\ 
\hline  & F &  &  &  &  & U.3 \\ 
\hline 
\end{tabular}

%8.11
\item{Write a QBE query for inserting a new organization into the organization table}

\begin{tabular}{|c|c|c|c|c|c|c|}
\hline Organization & Abbreviation & Name &  City & Country & Province & Established \\
\hline I. & AbAb & Abla Abra & & & & \\
\hline
\end{tabular}

%8.12
\item{Write a QBE query for deleting the organization you inserted in the question 8.12}

\begin{tabular}{|c|c|c|c|c|c|c|}
\hline Organization & Abbreviation & Name &  City & Country & Province & Established \\
\hline D. & AbAb & Abla Abra & & & & \\
\hline
\end{tabular}

\setcounter{enumi}{13}
%8.14
\item{Display the names of the countries that have no mountains passing through them}

\begin{tabular}{|c|c|c|c|}
\hline  geo\_Mountain & Mountain & Country & Province \\
\hline  &      &  \_x & \\
\hline
\end{tabular}

\begin{tabular}{|c|c|c|c|c|c|c|}
\hline Country & Name  &   Code  &  Capital & Province & Area & Population \\
\hline  & P.\_y & $\neg$\_x & & & & \\
\hline
\end{tabular}



\end{enumerate}

\section*{Bonus question 2}
\textbf{Write the following queries in SQL:}

\begin{enumerate}
%bq-2.1
\item{Display the name of those countries which both have their GDP more than the one in "Poland" and do not have "English" as a spoken language. Sort the results alphabetically}

\texttt{SELECT Name \\
FROM Country \\
WHERE Code IN ( \\
\tab  SELECT E.Country \\
\tab  FROM `Economy` AS E \\
\tab  INNER JOIN `Language` AS L \\
\tab  ON `E`.`Country` = `L`.`Country` \\
\tab  WHERE `E`.`GDP` > ( \\
\tab \tab \tab SELECT `GDP` FROM `Economy` WHERE `Country` = "PL" \\
\tab \tab ) AND `L`.Name != "English" \\
)\\
ORDER BY Name}

%bq-2.2
\item{Display the names of those countries that are neighbors of "France" but do not have "French" as a spoken language}

\texttt{SELECT Name \\
FROM Country \\
WHERE Code IN ( \\
\tab SELECT Country \\
\tab FROM borders  \\
\tab INNER JOIN Language \\
\tab ON Country != "F" \\
\tab \tab AND ( Country1 = Country OR Country2 = Country ) \\
\tab WHERE ( Country1 = "F" OR Country2 = "F" ) \\
\tab \tab AND Country NOT IN ( \\ 
\tab \tab \tab SELECT Country FROM Language WHERE Name = "French" \\
\tab \tab ) \\
)}

\end{enumerate}

\textbf{Write the following query in QBE}:

\begin{enumerate}
\setcounter{enumi}{2}
%bq-2.3
\item{Display the names of all countries through which the longest river in the world passes}

First, I created this SQL query: \\
\texttt{SELECT Name \\
FROM Country \\
WHERE Code IN ( \\
\tab SELECT Country \\
\tab FROM geo\_River \\
\tab WHERE River = ( \\
\tab \tab SELECT Name \\
\tab \tab FROM River \\
\tab \tab WHERE Length = ( \\
\tab \tab \tab SELECT Max(Length) FROM River \\
\tab \tab ) \\
\tab ) \\
) }

Followed by the QBE-query:

\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline River & Name & River & Lake & Sea & Len & SLong & SLat & Mtns & SAlt & ELong & ELat \\ 
\hline  &  &  &  &  & Max.\_l &  &  &  &  &  &  \\ 
\hline 
\end{tabular} 

\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline River & Name & River & Lake & Sea & Len & SLong & SLat & Mtns & SAlt & ELong & ELat \\ 
\hline  & \_x &  &  &  & \_y &  &  &  &  &  &  \\ 
\hline 
\end{tabular} 

\begin{tabular}{|c|}
\hline Conditions \\ 
\hline \_y = Max.\_l  \\ 
\hline
\end{tabular} 

\begin{tabular}{|c|c|c|c|}
\hline geo\_River & River & Country & Province \\ 
\hline  & \_y & \_z &  \\ 
\hline 
\end{tabular} 

\begin{tabular}{|c|c|c|c|c|c|c|}
\hline Country & Name & Code & Capital & Province & Area & Population \\ 
\hline  & P.\_n & \_z &  &  &  &  \\ 
\hline 
\end{tabular} 

Note: Because the QBE-tables are too large to fit on a single page, I used abbreviations for the following names of the table columns: \\
``Len'' $\rightarrow$ ``Length'' \\
``SLong'' $\rightarrow$ ``SourceLongitude'' \\
``SLat'' $\rightarrow$ ``SourceLatitude'' \\
``Mnts'' $\rightarrow$ ``Mountains'' \\
``SAlt'' $\rightarrow$ ``SourceAltitude'' \\
``ELong'' $\rightarrow$ ``EstuaryLongitude''\\
``ELat'' $\rightarrow$ ``EstuaryLatitude''

\end{enumerate}

\end{document}
