Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
131 views10 pages

DBS1 Assignment 2: SQL: Deadline

This document outlines the requirements and tasks for an SQL assignment. It includes: 1) The deadline for submission is Sunday November 22nd at 23:59. The assignment has multiple parts to be completed by certain dates. 2) Part 1 involves writing SQL DDL statements to implement the schema for a database on US presidents, including integrity constraints. Test statements must be written. 3) Part 2 involves writing SELECT queries on the president database to retrieve specific information, using functions and joins. 4) Parts 3 and 4 involve more advanced SELECT queries using GROUP BY and other techniques. Sample expected results are provided for each query.

Uploaded by

Stefan Hehe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views10 pages

DBS1 Assignment 2: SQL: Deadline

This document outlines the requirements and tasks for an SQL assignment. It includes: 1) The deadline for submission is Sunday November 22nd at 23:59. The assignment has multiple parts to be completed by certain dates. 2) Part 1 involves writing SQL DDL statements to implement the schema for a database on US presidents, including integrity constraints. Test statements must be written. 3) Part 2 involves writing SELECT queries on the president database to retrieve specific information, using functions and joins. 4) Parts 3 and 4 involve more advanced SELECT queries using GROUP BY and other techniques. Sample expected results are provided for each query.

Uploaded by

Stefan Hehe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DBS1 Assignment 2: SQL

Deadline
• This assignment needs to be submitted the latest on Sunday November 22th at 23h59.
o Groups are the same as in the previous assignment.
o Repositories can be created in https://classroom.github.com/g/XJcSP06k
• Importantly, try to finish…
o Part 1 before Tuesday October 13th.
o Part 2 before Tuesday November 10th.
o Part 3 before Tuesday November 17th.
o (Part 4 is published on Tuesday November 17th.)

PART 1: Data Definition Language


Study the relational model shown below. Carefully think of which data types you select for each column
and how to implement entity integrity (primary key) and referential integrity (foreign key) conditions.
Besides entity integrity and referential integrity conditions, additionally you have to implement the
following static integrity conditions:
• Attribute constraint: a president’s party should be in {’DEMOCRATIC’, ’REPUBLIC’, ’WHIG’,
’FEDERALIST’, ’DEMO-REP’}
• Tuple constraint: for all presidents born after 1800, the party can never be ’WHIG’.
• Tuple constraint: for all marriages applies that if the spouse age is higher than 60, then the number of
children is zero.
• Tuple constraint: for all marriages applies that if the marriage year is before 1800 then the age of the
president’s spouse should be equal to or higher than 21 and if the marriage year is in or after 1800
then the age of the president’s spouse should be equal to or higher than 18.
Given all integrity conditions described above, write appropriate SQL DDL statements to implement the
database schema, write test statements (i.e. insert and update statements) to verify that the integrity
conditions have been implemented correctly.
1.2 Diagram of the President Database

ADMIN_VPRES ADMIN_ID VICE_PRES_NAME

ADMINISTRATION ID ADMIN_NR PRES_ID YEAR_INAUGURATED

ELECTION ELECTION_YEAR CANDIDATE VOTES WINNER_LOSER_INDIC

PRES_HOBBY PRES_ID HOBBY

PRES_MARRIAGE PRES_ID SPOUSE_NAME SPOUSE_AGE NR_CHILDREN MARRIAGE_YEAR

PRESIDENT ID NAME BIRTH_YEAR YEARS_SERVED DEATH_AGE PARTY STATE_ID_BORN

STATE ID NAME ADMIN_ID YEAR_ENTERED

PART 2: SELECT Queries


Figure 1.1: Relational model of President Database.

This task is on querying a database. Therefore, prepare the database first:


• Create a new database, e.g. named presidents.
• Download and run the script president_schema.sql. This defines the schema for the
(large) president database.
• Download and run the script president_data.sql. This inserts data into the (large)
president database.

Your task now is to retrieve certain information from the president database. The relational model for the
database is show below. In each exercise you need to make use of the “SELECT” command in SQL to display
the data you want while using “WHERE” to specify which4
specific data should be displayed. For these tasks
you will also make use of the various aggregate functions in SQL, for example “AVG” or “SUM”. See the
slides for help on SQL queries and functions. The expected results for each executed query can be found
below the task itself.
1.2 Diagram of the President Database

ADMIN_VPRES ADMIN_ID VICE_PRES_NAME

ADMINISTRATION ID ADMIN_NR PRES_ID YEAR_INAUGURATED

ELECTION ELECTION_YEAR CANDIDATE VOTES WINNER_LOSER_INDIC

PRES_HOBBY PRES_ID HOBBY

PRES_MARRIAGE PRES_ID SPOUSE_NAME SPOUSE_AGE NR_CHILDREN MARRIAGE_YEAR

PRESIDENT ID NAME BIRTH_YEAR YEARS_SERVED DEATH_AGE PARTY STATE_ID_BORN

STATE ID NAME ADMIN_ID YEAR_ENTERED

1 Simple Queries Figure 1.1: Relational model of President Database.

1 In which year did Hillary Clinton (then ‘RODHAM H’) marry her husband, one of the presidents
of the US?

1975

2 Which states entered the US in 1845?

FLORIDA
TEXAS

3 In which election years did ‘NIXON R M’ win the elections?

1968
1972

4 How many votes did ‘OBAMA B’ receive in his election in 2008?

4
365

5 What are the names of the election candidates in 1968?

NIXON R M
HUMPHREY H H
WALLACE G C

6 When was ‘ROOSEVELT T’ born and how old was he when he died?

1858 60

2 Queries using Functions

7 Determine the average number of years served by each president.


5

8 Determine the minimum number of votes for a winner of presidential elections in the 19th
century (1800-1899).
73

9 How many presidential elections occurred in the 20th century?


24

10 How many states joined the federation between 1875 and 1925?
11

11 How many states brought forth a president born before 1900?


14

12 How many presidents were still living in 1950?


13
3 JOIN Queries

1 Determine names and election results (election year and number of votes) for all democratic
presidents taking part in elections after 1900 and born in a state that joined the federation after
1800.
name | election_year | votes
-------------+---------------+-------
TRUMAN H S | 1948 | 303
OBAMA B | 2012 | 332
OBAMA B | 2008 | 365
CLINTON W J | 1992 | 370
CLINTON W J | 1996 | 379
JOHNSON L B | 1964 | 486

2 Determine spouses’ names for all presidents born in states that joined the federation after 1850.
spouse_name
-------------
WARREN E B
ROBINSON M

3 Determine for all states that joined the federation after 1800 the name of the state, year of
accession and the names of all presidents born in this state with at least four years of tenure.
name | year_entered | name
------------+--------------+----------------
OHIO | 1803 | GRANT U S
OHIO | 1803 | HAYES R B
OHIO | 1803 | HARRISON B
OHIO | 1803 | MCKINLEY W
OHIO | 1803 | TAFT W H
IOWA | 1846 | HOOVER H C
MISSOURI | 1821 | TRUMAN H S
TEXAS | 1845 | EISENHOWER D D
TEXAS | 1845 | JOHNSON L B
CALIFORNIA | 1850 | NIXON R M
ILLINOIS | 1818 | REAGAN R
ARKANSAS | 1836 | CLINTON W J
HAWAII | 1959 | OBAMA B

4 Select president name, his birth-year and all election results, selecting only republican
presidents who took part in elections between 1950 and 1980.
name | birth_year | election_year | candidate | votes | wl_indic
----------------+------------+---------------+----------------+-------+-----------
EISENHOWER D D | 1890 | 1956 | EISENHOWER D D | 457 | W
EISENHOWER D D | 1890 | 1952 | EISENHOWER D D | 442 | W
NIXON R M | 1913 | 1972 | NIXON R M | 520 | W
NIXON R M | 1913 | 1968 | NIXON R M | 301 | W
NIXON R M | 1913 | 1960 | NIXON R M | 219 | L
FORD G R | 1913 | 1976 | FORD G R | 240 | L
REAGAN R | 1911 | 1980 | REAGAN R | 489 | W

4 Nested Queries

1 Determine the name and party of all presidents who served longer than the average of years of
all presidents born between 1850 and 1900 (ordered by president name).
name | party
----------------+------------
BUSH G W | REPUBLICAN
CLEVELAND G | DEMOCRATIC
CLINTON W J | DEMOCRATIC
EISENHOWER D D | REPUBLICAN
GRANT U S | REPUBLICAN
JACKSON A | DEMOCRATIC
JEFFERSON T | DEMO-REP
MADISON J | DEMO-REP
MONROE J | DEMO-REP
OBAMA B | DEMOCRATIC
REAGAN R | REPUBLICAN
ROOSEVELT F D | DEMOCRATIC
ROOSEVELT T | REPUBLICAN
TRUMAN H S | DEMOCRATIC
WASHINGTON G | FEDERALIST
WILSON W | DEMOCRATIC

2 Determine the name and birth year of all presidents who died older than the president born
before 1800 who died oldest.
name | birth_year
----------+------------
REAGAN R | 1911
FORD G R | 1913

3 Determine the name and birth year of all democratic presidents born before 1800, who had at
least 1 child.
name | birth_year
-------------+------------
VAN BUREN M | 1782
PART 3: SELECT Queries with GROUP BY

1 Determine each election year with at least three candidates who each obtained at least 20
votes. Select the election year, number of candidates and least number of votes.
election_year candidate minimum_votes
1824 4 37
1892 3 22
1948 3 39
1968 3 46

2 Determine ids and number of hobbies of all presidents having at least five hobbies.
pres_id number_hobbies
25 7
29 5
33 5

3 Determine, for each election after 1900 with at least 3 candidates, election year and total of
votes cast.
election_year votes_cast
1912 531
1924 531
1948 531
1956 531
1960 537
1968 538
1972 538

4 Select, of all elections after 1900 with 2 candidates, the maximum difference between the
winning number of votes and the loser’s number of votes. HINT: After FROM you can
use a query instead of a table name as follows:
SELECT * FROM (SELECT * FROM TABLE) alias_for_inner_query_result_table
max
515
5 Determine, for each election after 1850, with more than two candidates, in which the winner
obtained at least 80% of all votes, election year and number of candidates.
election_year count
1872 5
1912 3
1956 3
1972 3

6 Determine for each president married more than once his id and the greatest as well as the least
number of children born in his marriages.
pres_id maxchild minchild
10 8 7
13 2 0
23 2 1
25 5 1
27 3 0
40 2 2
44 3 1

7 Determine the ids of all presidents who married no spouse younger than 30 and were married
exactly once.
pres_id
28
32
37
42
8 Determine, for all presidents whose woman were younger than 30, ids and average number of
children per marriage (rounded). Select only presidents having more than 4 children on average.
pres_id average
2 5
3 6
9 10
10 8
12 6
17 5
19 8
20 7
22 5
31 6
39 6

9 Determine, for all presidents who married only woman older than 30, id and the sum of the
number of children in all their marriages.
pres_id sumchild
23 3
32 1
42 2

10 Determine the ids of all presidents who had no children in any of their marriages.
pres_id
4
7
28
1
11

11 Determine the ids of all presidents who were married more than once and had at least two
children in each of their marriages.
pres_id
10
40
12 List ages above 85 years and the number of presidents dying at the respective age.
death_age numberpres
88 1
90 2
93 2

You might also like