Skip to content

Commit

Permalink
Merge branch 'edits' of https://github.com/aliaalia/sql-tutorial into…
Browse files Browse the repository at this point in the history
… gh-pages

Conflicts:
	chapter2.html
	chapter3.html
  • Loading branch information
matthias2342 committed Nov 2, 2014
2 parents 56e693c + 0051abf commit 5824c54
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
6 changes: 5 additions & 1 deletion chapter2.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ <h2>Constraints</h2>
<pre>select name, residence from person where age > 30;</pre>
<p>For the different type of data, different constrains make sense. For all kind of data, we can check for equal <code>=</code> and not equal <code>!=</code>. For numbers, one can also compare for <em>larger</em> <code>></code>, <em>larger or equal</em> <code>>=</code>, <em>smaller</em> <code><</code>, <em>smaller or equal</em> <code><=</code>.</p>

<<<<<<< HEAD
<p>For text, we use <code>like</code>, to find entries where the text value in a column contains a string, where you can use the <code>%</code> as a wild-card for unknown parts of a word. E.g. if you search for <code>'%an%'</code>, the following words would fit: B<strong>an</strong>ana, f<strong>an</strong>tasy. Note that in SQLite, the <code>like</code> operator is <em>case insensitive</code> by default.

=======
<p>For text, we use <code>like</code>, to find entries where the text value in a column contains a string. The string should be surrounded by single quotes <code>'</code>, and you can use the <code>%</code> as a wild-card for unknown parts of a word. E.g. if you search for <code>'%an%'</code>, the following words would fit: B<strong>an</strong>ana, f<strong>an</strong>tasy, but not <strong>An</strong>na, since this is a Capital A.</p>
>>>>>>> 0051abf36686309bb132c5e0d7ed6717a9bb879b
<p class=question>3.) Get all people whose names contains "oh".</p>
<p>In SQLite the >,<,= operators for dates, do an <em>lexicographical</em> comparison, not a <em>numeric</em> one. This means that if there is an inconsistent representation of dates, the comparison might fail even though the two values represent the same dates. E.g. if once the format <code>"2014-10-23"</code> and once the format <code>"2014/10/16"</code> is used. Note that here double quotes <code>"</code> are used instead of single quotes <code>'</code>.</p>
<p>In SQLite the <code>></code>,<code><</code>,<code>=</code> operators for dates, do an <em>lexicographical</em> comparison, not a <em>numeric</em> one. This means that if there is an inconsistent representation of dates, the comparison might fail even though the two values represent the same dates. E.g. if once the format <code>"2014-10-23"</code> and once the format <code>"2014/10/16"</code> is used. Note that here double quotes <code>"</code> are used instead of single quotes <code>'</code>.</p>
There is a table named <code>flight</code> containg flight data. The column with the dates is called <code>date</code>.
<p class=question>4.) Get all flights before October 20.</p>

Expand Down
15 changes: 11 additions & 4 deletions chapter3.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body>

<h1>The theft of the Mona Lisa</h1>
<p>Now we can start to use our new skills to solve a criminal case: one of the most startling art thefts in history: On October the 23rd, 2014 the Mona Lisa was stolen from the Louvre in Paris. There is no trace of the thieves. You are working for Interpol and want to find out who is responsible for this crime. Due to new regulations you have access to any data of any international companies and states, like register of residents, mobile phone text messages or bank data. So, using these resources, find the thieves!</p>
<p>Now we can start to use our new skills to solve a criminal case: one of the most startling art thefts in history: On October the 23rd, 2014 the Mona Lisa was stolen from the Louvre in Paris. There is no trace of the thieves. You are working for Interpol and want to find out who is responsible for this crime. Due to new regulations you have access to any data of many international companies and states, like registers of residents, mobile phone text messages, and bank data. So, using these resources, find the thieves!</p>

<h2>Which tables are there? - System tables</h2>
<p>So, first you need to get an overview of what kind of data is actually available, i.e. you want to know what tables are in the database. The sqlite database contains are table named <code>sqlite_master</code> which holds meta information. We can get the names of the tables by an ordinary SQL query:</p>
Expand All @@ -25,7 +25,7 @@ <h2>How to find the thief</h2>
<ol>
<li>Find out, who was in Paris at that time.</li>
<li>The thief was probably not working alone. Is there any suspicious communication during the time in question?</li>
<li>If you find the people responsible, who was the actual wire-puller and who was "only" executing henchman?</li>
<li>If you find the people responsible, who was the actual wire-puller and who was "only" the henchman?</li>
</ol>

<h2>Who was in Paris?</h2>
Expand All @@ -37,14 +37,14 @@ <h2>Who was in Paris?</h2>
<p class=question>17.) Get all names that did a journey from Paris after 23.10.2014.</p>
<p class=question>18.) Get all names, that did a journey to Paris before 23.10.2014, where this name is also in an entry for a journey from Paris after the 23.10.2014.</p>
<p class=question>19.) Get all names of persons who live in Paris or spent their time in Paris on 23.10.2014 (according to the travel data).</p>
<p>Congratulations! You have reduced the number reduced the number of suspects tremendously!<p>
<p>Congratulations! You have reduced the number reduced the number of suspects tremendously!</p>

<h2>How information of different tables are cleanly connected: Key constraints</h2>
<p>The local police will pay these people a visit to ask for an alibi for the time in question. So you need a list of names and their residence. Repeat the select from before, but this time query for name as well as for residence.</p>
<p>Now, check the result. There is something wrong! In the list of conspicuous persons, there is one, who actually never was in Paris, i.e. who lives neither in Paris nor was traveling there.</p>
<p class=question>20.) Who is it? Hint: Perform a select on the flight table where the name is one of those who do not live in Paris, and check, and then check for every person-residence pair if there is a corresponding flight. For one is not. Who? Why is this person turning up in that list? Look at your <code>where</code> statement.</p>

<p>To prevent mistakes like this, data is usually more structured: People who create a table, usually define a column (or the combination of several columns) which is <em>unique</em> for every entry. This column is called the <em>primary key</em> (PK). Other tables which are referencing entries of this table have a corresponding column, containing the same value as the primary key column of the referenced table. This corresponding column is called </em>foreign key</em> (FK). For example, in our <code>person</code> table the field <code>id</code> is the primary key. This id is used in the <code>phone_contract</code> table as a foreign key <code>person_id</code>.</p>
<p>To prevent mistakes like this, data is usually more structured: People who create a table, usually define a column (or the combination of several columns) which is <em>unique</em> for every entry. This column is called the <em>primary key</em> (PK). Other tables which are referencing entries of this table have a corresponding column, containing the same value as the primary key column of the referenced table. This corresponding column is called <em>foreign key</em> (FK). For example, in our <code>person</code> table the field <code>id</code> is the primary key. This id is used in the <code>phone_contract</code> table as a foreign key <code>person_id</code>.</p>

<p align=center><img src="pkfk.png" alt="Primary key/foreign key relationship" width=508></p>

Expand All @@ -60,12 +60,19 @@ <h2>How information of different tables are cleanly connected: Key constraints</

<h2>Who is the thief? - Order by and group by</h2>
<p>To find out who is the thief, check the text messages stored by the mobile phone providers.</p>
<<<<<<< HEAD

<p class=question>23.) How is the name of the table containing the text messages and the one containing phone contracts?</p>
<p class=question>24.) Get all text messages  which where sent between 2014-10-20 and 2014-10-25.
<p class=question>25.) Get all phne contract ids where the <code>phone_contract.person_id</code> is equal to the id of one of the suspects.</p>
<p class=question>26.) Get all text messages where the sent date is the 23.10.2014 and the sender or receiver is one of our suspects.</p>

=======
<p class=question>23.) What are the names of the table containing the text messages and the one containing phone contracts?</p>
<p class=question>24.) Get all text messages  which where sent between 2010-10-20 and 2010-10-25.
<p class=question>25.) Get all contract ids where the <code>contract.name_id</code> is equal to one of the persons from the results of question 15.</p>
<p class=question>26.) Get all text messages where the sent date is the 23.10.2014 and the <code>contract_sender_id</code> is equal to the contract ids where the <code>contract.name_id</code> is equal to one of the persons from the results of question 15.</p>
>>>>>>> 0051abf36686309bb132c5e0d7ed6717a9bb879b
<p>You see that you got all the required information but the output looks kind of chaotic. You can order an result set according to a column with an <code>order by</code> phrase. The query to get all text messages from 21.10.2014 ordered by time reads</p>
<pre>
select message from messages where sent like '2014-10-21%'
Expand Down
3 changes: 2 additions & 1 deletion chapter3_solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ <h1>Solutions - The theft of the Mona Lisa</h1>
<p class=answer>After having found the ids of the two thieves you can use</p>
<pre>
select * from person
where id = 101 or id = 106;
where id = 100 or id = 106;
</pre>
</li>

<li><p class=answer>Philipp and Sarah</p></li>

</ol>

<p><a href="chapter3.html">Back to Tutorial</a></p>


</body>
Expand Down
2 changes: 1 addition & 1 deletion chapter4.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ <h2>Who got the money? - Aggregate Functions</h2>
group by account_number_fk;
</pre>
<p>Note that <code>group by</code> can also be used for more than one column.</p>
<p>These information would have provided an alternative way to identify suspected persons: Anyone who has gotten more than, for instance 20.000€, during the time period in question on their bank account would have been a suspect. In this case, we want to formulate a condition on the result of an aggregated function. Here, we cannot use a <code>where</code> clause, instead we use the <code>having</code> clause:
<p>These information would have provided an alternative way to identify suspected persons: Anyone who has gotten more than, for instance 20.000€, during the time period in question in their bank account would have been a suspect. In this case, we want to formulate a condition on the result of an aggregated function. Here, we cannot use a <code>where</code> clause, instead we use the <code>having</code> clause:
<pre>
select column_name, aggregate_function(column_name)
from table_name
Expand Down
2 changes: 2 additions & 0 deletions chapter4_solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ <h1>Solutions - Who was the string puller?</h1>

</ol>

<p><a href="chapter4.html">Back to Tutorial</a></p>

</body>


Expand Down
2 changes: 2 additions & 0 deletions chapter5_solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ <h1>Solutions - Advanced Topics</h1>

</ol>

<p><a href="chapter5.html">Back to Tutorial</a></p>

</body>


Expand Down
2 changes: 2 additions & 0 deletions chapter6_solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ <h1>Solutions - Entering data</h1>

</ol>

<p><a href="chapter6.html">Back to Tutorial</a></p>

</body>


Expand Down

0 comments on commit 5824c54

Please sign in to comment.