Jed Rembold
Wednesday, August 27, 2025
The SQL commands to the right involve creating and then populating a
simple table. If they were run one after the other, which of the below
possibilities would mostly closely mimic how the
rev table would look?
CREATE TABLE rev (
name VARCHAR(15),
age int);
INSERT INTO rev (name)
VALUES ('Bob'), ('Sally');
| name | age |
|---|---|
| Bob | 15 |
| Sally | 15 |
| name | age |
|---|---|
| Bob | |
| Sally |
| name | age |
|---|---|
| Bob | Sally |
The prime use of SQL for individuals not in charge of managing a database is using it to query the database for information
Queries return only a subset of information from the database, and don’t alter the database in any way
The keyword that begins any query is
SELECT, followed by:
Basic syntax then could look something like:
SELECT |||column₁|||, |||column₂||| FROM |||table name|||;While you can list out as many columns as you want, for tables with tons of columns that can be annoying
An asterisk *, can stand in for the
column names to stand for “all the columns”
SELECT * FROM |||table name|||;There is no way in core SQL or Postgres to indicate that you want “all but a few columns”
DISTINCT keyword allows you to only
display the unique values that appear in a column
SELECT DISTINCT |||column₁||| FROM |||table name|||;
SELECT DISTINCT |||column₁|||, |||column₂||| FROM |||table name|||;
ORDER BY keyword to
indicate a column to use for orderingASC (the default) or
DESC to specify the direction of
orderingSELECT |||column₁|||, |||column₂|||
FROM |||table name|||
ORDER BY |||column₁||| DESC;
SHOW lc_collate;en_US.utf8ORDER BY statementSELECT |||column|||
FROM |||table name|||
ORDER BY |||column||| DESC COLLATE "C"WHERE keyword provides exactly this
functionalitySELECT |||column||| FROM |||table name|||
WHERE |||some condition|||;
| Operator | Function | Example |
|---|---|---|
= |
Equal to | WHERE colname = 5 |
<> or != |
Not equal to | WHERE colname != 'fish' |
> |
Greater than | WHERE colname > 0 |
< |
Less than | WHERE colname < 100 |
>= |
Greater than or equal to | WHERE colname >= 0 |
<= |
Less than or equal to | WHERE colname <= 1000 |
BETWEEN |
Within a range | WHERE colname BETWEEN 50 and 100 |
IN |
Matches one of a set | WHERE colname IN ('red', 'blue') |
LIKE or
ILIKE |
Match a pattern | WHERE colname LIKE '%ed' |
NOT |
Negates a condition | WHERE colname NOT LIKE '%ed' |
NULL values in your output
querycolname != NULL
NULL value comparison always
gives NULL, no matter the comparisonIS NOT NULL (or
IS NULL if you want to find the null
values)SELECT |||column|||
FROM |||tablename|||
WHERE |||column||| IS NOT NULL;
Suppose I’d like to select from my cereal
table an alphabetized list of all the cereal names that have the same
amount of grams of proteins as they do grams of fat. Which query would
best deliver on this?
SELECT name FROM cereal
WHERE fat = protein
ORDER BY cereal;
SELECT DISTINCT name
FROM cereal
WHERE fat = protein;
SELECT name FROM cereal
WHERE fat = protein
ORDER BY name;
SELECT name FROM cereal
WHERE protein = fat
ORDER BY protein;
LIKE and ILIKE
differ only in whether capitalization matters in the match
LIKE is capitalization sensitive,
ILIKE is not% is a wildcard matching zero or more of
any character_ is a wildcard matching just a single
character\_ or
\%)AND and
OR operators that you can use to stitch
together multiple conditionsSELECT *
FROM |||table name|||
WHERE |||column₁||| LIKE 'F%' AND
(|||column₂||| > 50 OR |||column₃||| <= 10);
LIMITSELECT * FROM |||table name|||
WHERE |||some condition|||
ORDER BY |||column|||
LIMIT 5;
Introduce yourself to a neighbor and then work together to construct a query to solve the following situation: