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

0% found this document useful (0 votes)
9K views3 pages

Interactive SQL Examples: Reference

The document describes how to create and populate SQL tables to store weather station and weather data. It shows how to create tables with columns, primary keys, foreign keys and data types. Sample data is inserted into the tables. Various SELECT queries are performed to retrieve, filter and join data from the tables. Columns, rows and tables are projected and restricted in the queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9K views3 pages

Interactive SQL Examples: Reference

The document describes how to create and populate SQL tables to store weather station and weather data. It shows how to create tables with columns, primary keys, foreign keys and data types. Sample data is inserted into the tables. Various SELECT queries are performed to retrieve, filter and join data from the tables. Columns, rows and tables are projected and restricted in the queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

TP1

REFERENCE : https://www.itl.nist.gov/div897/ctg/dm/sql_examples.htm

INTERACTIVE SQL EXAMPLES


create a table to store information about weather observation stations: 
-- No duplicate ID fields allowed

CREATE TABLE STATION 


(ID INTEGER PRIMARY KEY, 
CITY CHAR(20), 
STATE CHAR(2), 
LAT_N REAL, 
LONG_W REAL);

populate the table STATION with a few rows:

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);

query to look at table STATION in undefined order:

SELECT * FROM STATION;

ID CITY STATE LAT_N LONG_W


13 Phoenix AZ 33 112
44 Denver  CO 40 105
66 Caribou ME 47 68

query to select Northern stations (Northern latitude > 39.7): 


-- selecting only certain rows is called a "restriction".

SELECT * FROM STATION 


WHERE LAT_N > 39.7;

ID CITY STATE LAT_N LONG_W


44 Denver  CO 40 105
66 Caribou ME 47 68

query to select only ID, CITY, and STATE columns: 


-- selecting only certain columns is called a "projection".

SELECT ID, CITY, STATE FROM STATION;

ID CITY STATE
13 Phoenix AZ

1
TP1

44 Denver  CO
66 Caribou ME

query to both "restrict" and "project":

SELECT ID, CITY, STATE FROM STATION 


WHERE LAT_N > 39.7;

ID CITY STATE
44 Denver  CO
66 Caribou ME

create another table to store normalized temperature and precipitation data: 


-- ID field must match some STATION table ID 
(so name and location will be known). 
-- allowable ranges will be enforced for other values. 
-- no duplicate ID and MONTH combinations. 
-- temperature is in degrees Fahrenheit. 
-- rainfall is in inches.

CREATE TABLE STATS 


(ID INTEGER REFERENCES STATION(ID), 
MONTH INTEGER CHECK (MONTH BETWEEN 1 AND 12), 
TEMP_F REAL CHECK (TEMP_F BETWEEN -80 AND 150), 
RAIN_I REAL CHECK (RAIN_I BETWEEN 0 AND 100), 
PRIMARY KEY (ID, MONTH));

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);

query to look at table STATS in undefined order:

SELECT * FROM STATS;

ID MONTH TEMP_F RAIN_I


13 1 57.4 .31
13 7 91.7 5.15
44 1 27.3 .18
44 7 74.8 2.11
66 1 6.7 2.1
2
TP1

66 7 65.8 4.52

query to look at table STATS, picking up location information by joining with


table STATION on the ID column: 
-- matching two tables on a common column is called a "join". 
-- the column names often match, but this is not required. 
-- only the column values are required to match.

SELECT * FROM STATION, STATS 


WHERE STATION.ID = STATS.ID;

ID CITY ST LAT_N  LONG_W ID MONTH  TEMP_F RAIN_I


13 Phoenix  AZ 33  112 13 1  57.4 .31 
13 Phoenix  AZ 33  112 13 7  91.7 5.15 
44 Denver CO 40  105 44 1  27.3 .18 
44 Denver CO 40  105 44 7  74.8 2.11 
66 Caribou  ME 47  68 66 1  6.7 2.1 
66 Caribou  ME 47  68 66 7  65.8 4.52 

You might also like