In this video we continue our exploration of writing queries involving a single table.
To follow along, you will need to download and install 2 sql files:
DISTINCT
- getting unique entriesLIMIT
- limit the number of rows returned. (for example., in combination with the above – the top ten cities in terms of population.AVG
, COUNT
, ETC – summary statistics like average, minimum, max values. (for example, the average tomato rating, or count things like how many movies Robert Downey was in.)GROUP BY
– group rows in order to perform some summary statistic (for example, what is the average tomato rating of each actor in our database – so we are grouping by actor.)AS
- Column aliases – ways of renaming the columns of a result.IN
, BETWEEN
, NOT
– syntactic sugar. these don’t add to the expressive power but help make our queries simpler.SELECT DISTINCT movie FROM actors ORDER BY movie;
SELECT DISTINCT firstname, lastname FROM actors ORDER BY lastname, firstname;
SELECT name, population FROM city WHERE countrycode = 'USA'
ORDER BY population DESC;
and for completeness (ascending order)
SELECT name, population FROM city WHERE countrycode = 'USA'
ORDER BY population ASC;
SELECT name, population FROM city WHERE countrycode = 'USA'
ORDER BY population DESC LIMIT 10;
SELECT DISTINCT movie, trating FROM actors
ORDER BY trating DESC LIMIT 5;
SELECT movie, runtime FROM actors WHERE runtime IS NOT NULL
ORDER BY runtime DESC LIMIT 1;
SELECT COUNT(*) FROM city';
SELECT COUNT(*) FROM city WHERE countrycode = 'USA';
SELECT AVG(trating) FROM actors WHERE firstname = 'Scarlet'
AND lastname = 'Johansson';
SELECT MAX(trating) FROM actors WHERE firstname = 'Robert'
AND lastname = 'Downey';
SELECT movie, trating FROM actors WHERE firstname = 'Robert'
AND lastname = 'Downey' ORDER BY trating DESC LIMIT 1;
SELECT district, COUNT(name) FROM city WHERE countrycode = 'USA'
GROUP BY district ORDER BY district;
SELECT firstname, lastname, COUNT(movie) FROM actors
GROUP BY firstname, lastname ORDER BY COUNT(movie) DESC;
The old way using OR
:
SELECT name, population FROM city WHERE district = 'Texas' OR
district = 'Arizona' OR district = 'Nevada' ORDER BY name; an alternative
Using IN
:
SELECT name, population FROM city WHERE district
IN ('Arizona', 'Texas', 'Nevada') ORDER BY name;
The old way using AND
:
SELECT name, population FROM city WHERE district = 'Texas' AND population < 200000
AND population > 100000 ORDER BY name;
Using between
:
SELECT name, population FROM city WHERE district = 'Texas' AND
population BETWEEN 100000 AND 200000 ORDER BY name;
SELECT COUNT(*) FROM city WHERE countrycode = 'USA'
AND NOT district IN ('Alaska', 'Hawaii');
We want the names of cities, the state name, and the population in columns named, city, state and population.
SELECT name AS city, district AS state,population FROM city WHERE countrycode = 'USA' order by population desc limit 10;
The AS
is optional
To have a single quote be interpreted as a character rather than a string delimiter use two single quotes.
SELECT movie FROM actors WHERE movie LIKE '%''%';
movie
---------------
King's Speech
(1 row)