SQL Major Assignment
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
);
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.
Q7) Execute a query to display temperature stats (from the STATS table) for each city
(from the STATION table).
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.
Q9) Execute a query to look at temperatures for July from table STATS, lowest
temperatures first, picking up city name and latitude.
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.
UPDATE Stats
SET Rain_I = Rain_I + 0.01;
UPDATE Stats
SET Temp_F = 74.9
WHERE Month = 7;