Jed Rembold
Monday, November 3, 2025
Given the starting table called rev, what
is the output of the query?
| name | num |
|---|---|
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
| E | 5 |
SELECT
CASE
WHEN num % 2 = 0 THEN name
WHEN name > 'B' THEN 'D'
ELSE 'A'
END
FROM rev
WHERE num < 4
ORDER BY num DESC
LIMIT 1
| Function | Description |
|---|---|
|||str||| || |||str2||| |
Concatenates string 1 and string 2 together |
upper(|||str|||) |
Converts a string to all uppercase characters |
lower(|||str|||) |
Converts a string to all lowercase characters |
char_length(|||str|||) |
Returns the number of characters in the string |
position(|||str||| IN |||substr|||) |
Find the number of the character where the substring begins |
trim(|||opt||| |||chr||| FROM |||str|||) |
Removes the given characters from the string, optionally taking from the leading or trailing edge |
substring(|||str||| FROM |||n||| FOR |||l|||) |
Returns the portion of the string starting at position n and continuing for l characters |
| Function | Description |
|---|---|
initcap(|||str|||) |
Converts the first character of each word to uppercase, and the rest lower |
left(|||str|||,|||n|||) |
Returns the first n characters of the string |
right(|||str|||,|||n|||) |
Returns the last n characters of the string |
ltrim(|||str|||,|||chr|||) |
Remove the characters (space by default) from the start of the string |
rtrim(|||str|||,|||chr|||) |
Remove the characters (space by default) from the end of the string |
replace(|||str|||,|||from|||,|||to|||) |
Replaces all occurance of from in the string to to |
length(|||str|||) |
Returns the number of characters in the string |
substr(|||str|||, |||n|||, |||l|||) |
Returns the portion of the string starting at position n and continuing l characters |
LIKE and LIKE
% and _| Expression | Description |
|---|---|
. |
Matches any character except a new line (this can vary some in other implementations) |
[abc] |
Matches any character in the square brackets (a or b or c) |
[a-z] |
Matches a range of characters (all lowercase letters here) |
[^a-z] |
Caret negates what follows (so no lowercase letters here) |
\w |
Any word character, digit or underscore |
\d |
Any digit |
\s |
A space |
\t |
A tab character |
\n |
A newline character |
| Expression | Description |
|---|---|
^ |
Match at the start of the string |
$ |
Match at the end of the string |
? |
Get the preceding match 0 or one time |
* |
Get the preceding match zero or more times |
+ |
Get the preceding match one or more times |
{m} |
Get the preceding match exactly m times |
{m,n} |
Get the preceding match between m and n times |
a | b |
Match on either a or b, where a and b are full matching expressions |
( ) |
Create a capture group or set precedence |
(?: ) |
Negate reporting a capture group |
\({ } [ ] / \ + * . $ ^ | ?/i means that matches will be case
insensitive/g means that all instances of the match
will be returned, not just the first/m allows the anchor characters
(^ and $) to
operate on each line, not just across the entire string.~ is a case sensitive match using the
following regex~* is a case insensitive match using the
following regex! in front to negate
the search (where things do not match the regex)SELECT colname
FROM tablename
WHERE colname ~ '[a-z]*\s\d{2}';
regexp_match(str, regex) returns the
first matching instance in the string
[1] at the end after
wrapping entire expression in ()SELECT (regexp_match('today is March 15, 2022', '\d{4}'))[1];
1 1/4 form)