Hui Hu Ph.D.
Department of Epidemiology
College of Public Health and Health Professions & College of Medicine
January 15, 2020
R Markdown provides an authoring framework for data science.
You can use a single R Markdown file to both
save and execute code, and generate high quality reports that can be shared with an audience.
Where is something?
Finding directions
Column
Row
Table
Subject |
---|
id |
name |
gender |
age |
race/ethnicity |
county_id |
state_id |
County |
---|
id |
name |
median household income |
state_id |
State |
---|
id |
name |
policy |
policy start date |
Primary Key
Logical Key
Foreign Key
SELECT subject.name, state.name as state_name, state.policy
FROM subject
JOIN state ON subject.state_id=state.id
;
SELECT subject.name, state.name as state_name, county.income
FROM subject
JOIN state ON subject.state_id=state.id
JOIN county ON subject.state_id=county.state_id
AND subject.county_id=county.id
;
SELECT COUNT(id) AS n
FROM subject;
SELECT COUNT(id) AS n, state_id
FROM subject
GROUP BY state_id;
SELECT COUNT(DISTINCT state_id) AS nstate
FROM subject
;
SELECT MAX(income) AS maxIncome, state_id
FROM county
GROUP BY state_id
HAVING MAX(income) > 90000;
What's the difference between WHERE and HAVING?
SELECT name,
gender,
CASE WHEN age BETWEEN 1 AND 4 THEN 1
WHEN age BETWEEN 5 AND 8 THEN 2
WHEN age > 8 THEN 3
ELSE NULL
END AS recodeage
FROM subject
;