Remember to

Why Databases

Database Queries

Using DB Browser

Selecting columns

SELECT year, month, day
FROM surveys;
SELECT *
FROM surveys;
SELECT DISTINCT year, month, day
FROM surveys;
SELECT species_id, ROUND(hindfoot_length/10, 1)
FROM surveys;

Filtering

SELECT hindfoot_length
FROM surveys
WHERE species_id = 'DS';
SELECT year, month, day, species_id, hindfoot_length
FROM surveys
WHERE species_id = 'DS' AND year > 1990;
SELECT year, month, day, species_id, hindfoot_length
FROM surveys
WHERE species_id = 'DS' AND year > 1990 
  AND hindfoot_length IS NOT NULL;

Style

Saving queries for future use

Do the Simple WHERE exercise.

Sorting

SELECT genus, species
FROM species
ORDER BY genus;
SELECT genus, species
FROM species
ORDER BY genus DESC;
SELECT genus, species
FROM species
ORDER BY taxa, genus, species;

Aggregation

SELECT species_id, AVG(weight), COUNT(species_id)
FROM surveys
GROUP BY species_id;
SELECT species_id, plot_id, AVG(weight), COUNT(species_id)
FROM surveys
GROUP BY species_id, plot_id;
SELECT species_id, plot_id, AVG(weight), COUNT(weight)
FROM surveys
GROUP BY species_id, plot_id;
SELECT species_id, plot_id, AVG(weight) as avg_weight, COUNT(weight) as num_indiv
FROM surveys
GROUP BY species_id, plot_id;

Do the COUNT exercise.

Basic join

SELECT DISTINCT year, month, day, plot_type 
FROM surveys
JOIN plots USING (plot_id);
SELECT DISTINCT year, month, day, plot_type 
FROM surveys
JOIN species on surveys.species_id = species.species_id;

Multi-table join

SELECT year, month, day, taxa, plot_type
FROM surveys
JOIN species USING (species_id)
JOIN plots USING (plot_id)

Do Basic Join.

Do Multi-table Join.