SELECT *
FROM table
WHERE column_name = column_value
LIMIT value
COMPARISON OPERATORS
WHERE column_name = / (!= / < >) / > / < / >= / <= (value / ‘value’ (if non-numerical))
SELECT *
FROM tutorial.us_housing_units
WHERE month_name > 'J'
— Will yield results commencing with ‘J’ or later
ARITHMETIC IN SQL
Only across columns on values in a given row — i.e. C1 + C2 of R2 —otherwise use
aggregate functions
SELECT year,
month,
west,
south,
west + south - 4 * year AS nonsense_column
FROM tutorial.us_housing_units
— nonsense_column is a derived column.
Parentheses for order of operation management. (e.g. (column_name + column_name) / 2 =
average)
SELECT year,
month,
west,
south,
(west + south)/2 AS south_west_avg
FROM tutorial.us_housing_units
SQL LOGICAL OPERATORS
| LIKE case-sensitive (Case-absence = ILIKE) | % is wildcard. Denotes ‘rest’ (e.g. word-
commencer = snoop) | _ can denote single char (‘te_t’) ||
SELECT *
FROM table
WHERE column_name LIKE ‘text%’
SELECT *
FROM table
WHERE column_name IN (value, value, value) | ‘’ if alphabetic ||
SELECT *
FROM table
WHERE column_name BETWEEN value and value | returns range inclusive of stated values ||
SELECT *
FROM table
WHERE column_name IS NULL
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2012
AND year_rank <= 10
AND "group_name" ILIKE '%feat%'
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
AND ("group_name" ILIKE '%macklemore%' OR "group_name" ILIKE '%timberlake%')
| NOT usable before any conditional statement | In certain cases NOT makes little sense for use.
(e.g. use AND column_name <=3 instead of AND column_name NOT > 3) | freq. used with
LIKE (e.g. AND column_name NOT ILIKE ‘%characters%’) | oft used to identify non-null
rows (e.g. AND column_name IS NOT NULL) ||
SELECT *
FROM table
WHERE column_name = column_value
AND column_name NOT BETWEEN value AND value
SORTING DATA WITH SQL ORDER BY
Default = alphabetic / smallest number to largest (ascending) — for descending = ORDER BY
column_name DESC
SELECT *
FROM table
WHERE column_name = column_value
ORDER BY column_name
ORDERING DATA BY MULTIPLE COLUMNS
| Columns in ORDER by must be comma-separated, DESC applies to anterior column only | for
example first sorted by year then by year_rank (i.e. 2013, 1, etc, 2013, 2, etc…) | for some SQL
versions: substitute numbers for names (e.g. ORDER BY 2, 1 DESC) | ORDER BY is done
before LIMIT ||
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank <= 3
ORDER BY year DESC, year_rank
USING COMMENTS
-- everything to right of two dashes is a comment OR /* comment between these*/
INTERMEDIATE SQL
COUNTING ALL ROWS
Counts number of rows in a column | COUNT (*) / COUNT (1) are the same |
SELECT COUNT (*)
FROM table
COUNTING INDIVIDUAL COLUMNS
Returns number of rows in column which are not null |
SELECT COUNT (column_name)
COUNTING NON-NUMERICAL COLUMNS
Makes no distinction of row content — only empty or not | use alias to make easier to read
(AS). Use “” for alias with spaces ||
SELECT COUNT(column) AS alias
FROM table
THE SQL SUM FUNCTION
Aggregators only aggregate vertically | SUM totals values of given rows — only usable on
number-containing columns | SUM treats null as 0 ||
SELECT SUM (column)
FROM table
SQL MIN / MAX
MIN or MAX of selected column | also for non-numerical (closest to ‘A’ or ‘Z’ respectively) ||
SELECT MIN (column) AS column_name,
MAX (column) AS column_name
FROM table
SQL AVERAGE
Calculates average of selected value group | Limitations: numerical-columns only, ignores null
completely (i.e. in some cases want to treat null as 0 — therefore write a statement) ||
SELECT AVG (column)
SQL GROUP BY
SQL JOINS
| ON indicates the relation (i.e. which of one table corresponds to which in the other). Oft called
‘mappings’. To joined columns called ‘foreign keys’ / ‘join keys’ | SELECT * returns all
columns from both tables not just from table after FROM | if only want return columns from
one table (e.g. SELECT players.*) ||
SELECT [Link] AS conference,
AVG([Link]) AS average_weight
FROM benn.college_football_players players
JOIN benn.college_football_teams teams
ON teams.school_name = players.school_name
GROUP BY [Link]
ORDER BY AVG([Link]) DESC
SQL INNER JOIN
Intersection of two tables | eliminates non-join condition matches | JOIN [Link] or
INNER JOIN [Link] | e.g. player going to school not in team table won’t be included
in inner join result. Likewise schools in team table not matching schools in player table won’t
occur ||
JOINING TABLES WITH IDENTICAL COLUMN NAMES
Results support one column with given name — 2 with same name will show same results even
if data is different. Can avoid by naming columns individually (see second example) ||
SELECT players.*,
teams.*
FROM benn.college_football_players players
JOIN benn.college_football_teams teams
ON teams.school_name = players.school_name
||
SELECT players.school_name AS players_school_name,
teams.school_name AS teams_school_name
FROM benn.college_football_players players
JOIN benn.college_football_teams teams
ON teams.school_name = players.school_name
SQL OUTER JOIN
(LEFT JOIN / OUTER LEFT JOIN), (RIGHT JOIN / RIGHT OUTER JOIN), (FULL OUTER
JOIN / OUTER JOIN) | Unmatched rows in one or both tables can be returned ||
SQL LEFT JOIN