SQL Basics Cheatsheet
Find All Columns and Rows in a Table
SELECT * FROM <table name>;The asterisk or star symbol (*) means all columns.
The semi-colon (;) terminates the statement like a period in sentence or question mark in a question.
Examples:
SELECT * FROM books;
SELECT * FROM products;
SELECT * FROM users;
SELECT * FROM countries;Retrieving Specific Columns of Information
Retrieving a single column:
SELECT <column name> FROM <table name>;Examples:
SELECT email FROM users;
SELECT first_name FROM users;
SELECT name FROM products;
SELECT zip_code FROM addresses;Retrieving multiple columns:
Examples:
Aliasing Column Names
Examples:
Finding the Data You Want
Equality Operator
Find all rows that a given value matches a column's value.
Examples:
Inequality Operator
Find all rows that a given value doesn't match a column's value.
The not equal to or inequality operator can be written in two ways != and <>. The latter is less common.
Examples:
Relational Operators
There are several relational operators you can use:
<less than<=less than or equal to>greater than>=greater than or equal to
These are primarily used to compare numeric and date/time types.
Examples:
More Than One Condition
You can compare multiple values in a WHERE condition. If you want to test that both conditions are true use the AND keyword, or either conditions are true use the OR keyword.
Examples:
Searching in a Set of Values
Examples:
To find all rows that are not in the set of values you can use NOT IN.
Examples:
Searching within a Range of Values
Examples:
Pattern Matching
Placing the percent symbol (%) any where in a string in conjunction with the LIKE keyword will operate as a wildcard. Meaning it can be substituted by any number of characters, including zero!
Examples:
PostgreSQL Specific Keywords
LIKE in PostgreSQL is case-sensitive. To do case-insensitive searches use ILIKE.
Missing Values
Examples:
To filter out missing values use can use IS NOT NULL.
Examples
Last updated
Was this helpful?