SQL Expressions

COMP3311 20T3 ♢ SQL Expressions ♢ [0/10]
❖ Expressions in SQL

Expressions in SQL involve: objects, constants, operators


SQL constants are similar to typical programming language constants
But strings are substantially different
COMP3311 20T3 ♢ SQL Expressions ♢ [1/10]
❖ SQL Operators

Comparison operators are defined on all types:

<   >   <=   >=   =   <>

In PostgreSQL,   !=  is a synonym for  <>   (but there's no ==)

Boolean operators AND, OR, NOT are also available

Note AND,OR are not "short-circuit" in the same way as C's &&,||

Most data types also have type-specific operations available

String comparison (e.g. str1 < str2) uses dictionary order

See PostgreSQL Documentation Chapter 8/9 for data types and operators

COMP3311 20T3 ♢ SQL Expressions ♢ [2/10]
❖ SQL Operators (cont)

SQL provides pattern matching for strings via  LIKE  and  NOT LIKE

Examples:

name LIKE 'Ja%' name begins with 'Ja'
name LIKE '_i%' name has 'i' as 2nd letter
name LIKE '%o%o%' name contains two 'o's
name LIKE '%ith' name ends with 'ith'
name LIKE 'John' name equals 'John'

PostgreSQL also supports case-insensitive matching:   ILIKE

COMP3311 20T3 ♢ SQL Expressions ♢ [3/10]
❖ SQL Operators (cont)

PostgreSQL provides regexp-based pattern matching via  ~  and  !~

Examples (using POSIX regular expressions):

name ~ '^Ja' name begins with 'Ja'
name ~ '^.i' name has 'i' as 2nd letter
name ~ '.*o.*o.*' name contains two 'o's
name ~ 'ith$' name ends with 'ith'
name ~ 'John' name contains 'John'

Also provides case-insensitive matching via  ~*  and  !~*

COMP3311 20T3 ♢ SQL Expressions ♢ [4/10]
❖ SQL Operators (cont)

Other operators/functions for string manipulation:

Etc. etc. ... consult your local SQL Manual (e.g. PostgreSQL Sec 9.4)

Note that above operations are null-preserving (strict):

COMP3311 20T3 ♢ SQL Expressions ♢ [5/10]
❖ SQL Operators (cont)

Arithmetic operations:


+  -  *  /  abs  ceil  floor  power  sqrt  sin  etc.

Aggregations "summarize" a column of numbers in a relation:

Note: count applies to columns of non-numbers as well.
COMP3311 20T3 ♢ SQL Expressions ♢ [6/10]
❖ The NULL Value

Expressions containing NULL generally yield NULL.

However, boolean expressions use three-valued logic:

a b a AND b a OR b
TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE
TRUE NULL NULL TRUE
FALSE FALSE FALSE FALSE
FALSE NULL FALSE NULL
NULL NULL NULL NULL

COMP3311 20T3 ♢ SQL Expressions ♢ [7/10]
❖ The NULL Value (cont)

Important consequence of NULL behaviour ...

These expressions do not work as (might be) expected:

x = NULL    x <> NULL

Both return NULL regardless of the value of x

Can only test for NULL using:

x IS NULL     x IS NOT NULL

COMP3311 20T3 ♢ SQL Expressions ♢ [8/10]
❖ Conditional Expressions

Other ways that SQL provides for dealing with NULL:

coalesce(val1,val2,...valn)

E.g. select coalesce(mark,'??') from Marks ...

nullif(val1,val2)

E.g. nullif(mark,'??')
COMP3311 20T3 ♢ SQL Expressions ♢ [9/10]
❖ Conditional Expressions (cont)

SQL also provides a generalised conditional expression:

CASE
   WHEN test1 THEN result1
   WHEN test2 THEN result2
   ...
   ELSE resultn
END

E.g. case when mark>=85 then 'HD' ... else '??' end

Tests that yield NULL are treated as FALSE

If no ELSE, and all tests fail, CASE yields NULL

COMP3311 20T3 ♢ SQL Expressions ♢ [10/10]


Produced: 22 Sep 2020