SQL Major Assignment
Q1) Create a table “STATION ” to store information about weather observation station.
CREATE TABLE
Station( Id INT
PRIMARY KEY, City
CHAR(20),
State CHAR(2),
Lat_N INT,
Long_W INT
);
Q2) Insert the following records into the table.
INSERT INTO Station (Id, City,State, Lat_N, Long_W)
VALUE (13,'PHOENIX','AZ',33,112),
(44,'DENVER','CO',40,105),
(66,'CARIBOU','ME',47,68);
Q3) Execute a query to look at table STATION in undefined order.
SELECT * FROM Station;
Q4) Execute a query to select Northern stations (Northern latitude > 39.7).
SELECT Lat_N
FROM Station
WHERE Lat_N > 39.7;
Q5) Create another table, ‘STATS’, to store normalized temperature and precipitation data.
CREATE TABLE
Stats( Id INT NOT
NULL,
Month INT CHECK (Month BETWEEN 1 AND 12),
Temp_F FLOAT CHECK (Temp_F BETWEEN -80 AND 150),
Rain_I FLOAT CHECK (Rain_I BETWEEN 0 AND 100),
PRIMARY KEY (ID, MONTH),
FOREIGN KEY (ID) REFERENCES STATION(ID)
);
Q6) Populate the table STATS with some statistics for January and July.
INSERT INTO Stats (Id, MONTH, TEMP_F, RAIN_I)
VALUES (13, 1, 57.4, 0.31),
(13, 7, 91.7, 5.15),
(44, 1, 27.3, 0.18),
(44, 7, 74.8, 2.11),
(66, 1, 6.7, 2.1),
(66, 7, 65.8, 4.52);
Q7) Execute a query to display temperature stats (from the STATS table) for each city
(from the STATION table).
SELECT S.City, S.State, ST.month, ST.Temp_F, ST.Rain_I
FROM Stats ST
JOIN Station S ON ST.ID = S.ID
ORDER BY S.City, ST.Month;
Q8) 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 S.City, ST.Month, ST.Rain_I, ST.Temp_F
FROM Stats ST
JOIN Station S ON ST.ID = S.ID
ORDER BY ST.Month, ST.Rain_I DESC;
Q9) Execute a query to look at temperatures for July from table STATS, lowest
temperatures first, picking up city name and latitude.
SELECT S.City, S.Lat_N, ST.Temp_F
FROM Stats ST
JOIN Station S ON ST.ID = S.ID
WHERE ST.Month = 7
ORDER BY ST.Temp_F;
Q10) Execute a query to show MAX and MIN temperatures as well as average rainfall for
each city.
SELECT S.City,
MAX(ST.Temp_F) AS MAX_TEMP_F,
MIN(ST.Temp_F) AS MIN_TEMP_F,
AVG(ST.Rain_I) AS AVG_RAIN_I
FROM Stats ST
JOIN Station S ON ST.ID = S.ID
GROUP BY S.City;
Q11) Execute a query to display each city’s monthly temperature in Celcius and rainfall in
Centimeter.
SELECT S.City, ST.month,
Round((ST.Temp_F - 32) * 5/9, 1) AS Temp_Degree,
Round(ST.Rain_I * 2.54), 1 AS Rain_Centi_Miter
From Stats ST
JOIN Station S ON ST.Id = S.Id
Order by S. City, ST.Month;
Q12) 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;
Q13) Update Denver's July temperature reading as 74.9.
UPDATE Stats
SET Temp_F = 74.9
WHERE Month = 7;