11 May 2022

SQL Clauses Cheatsheet

LIKE

% represents 0, 1 or more characters

_ single character

SELECT * FROM table1
WHERE column1 LIKE 'A%'

GROUP BY

Group rows that have identical data

name salary
James 20
James 10
Paul 5
SELECT name, sum(salary)
FROM company1
GROUP BY name

Result:

name sum(salary)
James 30
Paul 5

WITH

WITH some_name AS (SELECT id, name)
SELECT * FROM table1

some_name can be an existing table name (will override the table)

HAVING

Places conditions on groups created by the GROUP BY clause.

name salary
Paulina 50
Janet 10
Paulina 30
SELECT name FROM table1
GROUP BY name
HAVING count(name) > 1

Result:

name
Paulina

LIMIT

SELECT * FROM table1
LIMIT 20 OFFSET 3