SQL Cheat Sheet

We use Sqlite and the Sakila Sample database [kaggle.com].

The diagram of database is:

select

from

where

group by

having

oerder by

limit

=

!=

<

>

<=

>=

True

False

not

in

between

Aggregate Functions

min()

max()

avg()

count()

median()

mode()

stddev()

w/group by

w/ having

w/ distinct

String Manipulation

concat()

replace()

reverse()

trim()

upper()

lower()

len()

str()

wildcards (%)

like / ilike

Date Manipulation

day()

month()

year()

getdate()

date_add()

datediff()

date_trunc()

date_format()

Joins

inner join

left join

outer join

self join

anti join

joining on multiple keys

joining on a condition

Cleaning & Transformation

cast()

coalesce()

case when

ifnull()

iff()

listagg()

except

Union

Union All

Intersect

Minus

Window Functions

sum()

count()

avg()

max()

min()

row_number()

rank()

denserank()

lead()

lag()

over( ...

partition by<

order by

)

Advanced

CTEs

UDFs

Data Modeling

Subqueries