SQL MAJOR ASSIGNMENT SOLUTION
1. Create a table “Station” to store information about weather
observation stations:
CREATE TABLE STATION
(ID INTEGER PRIMARY KEY,
CITY CHAR(20),
STATE CHAR(2),
LAT_N NUMBER,
LONG_W NUMBER);
2. Insert the following records into the table:
INSERT INTO STATION VALUES (13, 'Phoenix', 'AZ', 33, 112);
INSERT INTO STATION VALUES (44, 'Denver', 'CO', 40, 105);
INSERT INTO STATION VALUES (66, 'Caribou', 'ME', 47, 68);
3. Execute a query to look at table STATION in undefined order.
SELECT * FROM STATION
OUTPUT
ID CITY STATE LAT_N LONG_W
Phoeni
13 AZ 33 112
x
4
Denver CO 40 105
4
66 Caribou ME 47 68
4. Execute a query to select Northern stations (Northern latitude >
39.7).
SELECT * FROM STATION
WHERE LAT_N > 39.7 ;
OUTPUT
ID CITY STATE LAT_N LONG_W
4
Denver CO 40 105
4
Caribo
66 ME 47 68
u
5. Create another table, ‘STATS’, to store normalized temperature and
precipitation data:
CREATE TABLE STATS
(
ID NUMBER,
MONTH NUMBER NOT NULL,
TEMP_F NUMBER NOT NULL,
RAIN_I NUMBER NOT NULL
);
6. Populate the table STATS with some statistics for January and July:
INSERT INTO STATS VALUES (13, 1, 57.4, 0.31);
INSERT INTO STATS VALUES (13, 7, 91.7, 5.15);
INSERT INTO STATS VALUES (44, 1, 27.3, 0.18);
INSERT INTO STATS VALUES (44, 7, 74.8, 2.11);
INSERT INTO STATS VALUES (66, 1, 6.7, 2.10);
INSERT INTO STATS VALUES (66, 7, 65.8, 4.52);
7. Execute a query to display temperature stats (from STATS table) for
each city (from Station table).
SELECT S.City, T.TEMP_F FROM STATS T
INNER JOIN STATION S
ON T.ID = S.ID;
OUTPUT
CITY TEMP_F
Phoeni
57.4
x
Phoeni
91.7
x
Denver 27.3
Denver 74.8
Caribou 6.7
Caribou 65.8
8. Execute a query to look at the table STATS, ordered by month and
greatest rainfall, with columns rearranged. It should also show the
corresponding cities.
SELECT T.MONTH , S.city, T.ID, T.RAIN_I, T.TEMP_F
FROM STATS T
INNER JOIN STATION S
ON T.ID = S.ID
ORDER BY T.MONTH, T.RAIN_I DESC ;
OUTPUT
MONTH CITY ID RAIN_I TEMP_F
1 Caribou 66 2.1 6.7
Phoeni
1 13 .31 57.4
x
4
1 Denver .18 27.3
4
Phoeni
7 13 5.15 91.7
x
7 Caribou 66 4.52 65.8
4
7 Denver 2.11 74.8
4
9. Execute a query to look at temperatures for July from table STATS,
lowest temperatures first, picking up city name and latitude.
SELECT S.LAT_N, S.CITY, T.TEMP_F
FROM STATS T , STATION s
WHERE MONTH = 7
AND T.ID = S.ID
ORDER BY TEMP_F;
OUTPUT
LAT_N CITY TEMP_F
47 Caribou 65.8
40 Denver 74.8
Phoeni
33 91.7
x
10. Execute a query to show MAX and MIN temperatures as well as
average rainfall for each city.
SELECT ID, MAX(TEMP_F), MIN(TEMP_F), AVG(RAIN_I)
FROM STATS
GROUP BY ID;
OUTPUT
ID MAX(TEMP_F) MIN(TEMP_F) AVG(RAIN_I)
4
74.8 27.3 1.145
4
66 65.8 6.7 3.31
13 91.7 57.4 2.73
11. Execute a query to display each city’s monthly temperature in
Celcius and rainfall in Centimeter.
SELECT S.ID, S.City,
T.MONTH,
ROUND((TEMP_F - 32) * 5 /9) As TEMP_CELCIUS,
Round(RAIN_I * 0.3937) As RAINFALL_CENTIMETER
From STATS T
JOIN STATION S
ON S.ID = T.ID ;
OUTPUT
ID CITY MONTH TEMP_CELCIUS RAINFALL_CENTIMETER
Phoeni
13 1 14 0
x
Phoeni
13 7 33 2
x
4
Denver 1 -3 0
4
4
Denver 7 24 1
4
66 Caribou 1 -14 1
66 Caribou 7 19 2
12. Update all rows of table STATS to compensate for faulty rain gauges
known to read 0.01 inches low.
UPDATE STATS SET RAIN_I = RAIN_I + 0.01;
SELECT * FROM STATS
OUTPUT
ID MONTH TEMP_F RAIN_I
13 1 57.4 .32
13 7 91.7 5.16
4
1 27.3 .19
4
4
7 74.8 2.12
4
66 1 6.7 2.11
66 7 65.8 4.53
13. Update Denver's July temperature reading as 74.9
UPDATE STATS SET TEMP_F = 74.9
WHERE ID = 44 AND MONTH = 7;
SELECT * FROM STATS
OUTPUT
ID MONTH TEMP_F RAIN_I
13 1 57.4 .32
13 7 91.7 5.16
4
1 27.3 .19
4
4
7 74.9 2.12
4
66 1 6.7 2.11
66 7 65.8 4.53