You are on page 1of 3

Cheatsheets / Data Acquisition

SQL
Column Constraints
Column constraints are the rules applied to the values of
individual columns: CREATE TABLE student (
  id INTEGER PRIMARY KEY,
● PRIMARY KEY constraint can be used to uniquely identify
  name TEXT UNIQUE,
the row.
  grade INTEGER NOT NULL,
● UNIQUE columns have a di erent value for every row.   age INTEGER DEFAULT 10
● NOT NULL columns must have a value. );

● DEFAULT assigns a default value for the column when no


value is speci ed.

There can be only one PRIMARY KEY column per table and
multiple UNIQUE columns.

CREATE TABLE Statement


The CREATE TABLE statement creates a new table in a database. It
allows one to specify the name of the table and the name of CREATE TABLE table_name (
each column in the table.   column1 datatype,
  column2 datatype,
  column3 datatype
);

INSERT Statement
The INSERT INTO statement is used to add a new record (row) to
a table. -- Insert into columns in order:
It has two forms as shown: INSERT INTO table_name
VALUES (value1, value2);
● Insert into columns in order.

● Insert into columns by name. -- Insert into columns by name:


INSERT INTO table_name (column1, column2)
VALUES (value1, value2);

ALTER TABLE Statement


The ALTER TABLE statement is used to modify the columns of an
existing table. When combined with the ADD COLUMN clause, it is ALTER TABLE table_name
used to add a new column. ADD column_name datatype;

DELETE Statement
The DELETE statement is used to delete records (rows) in a
table. The WHERE clause speci es which record or records that DELETE FROM table_name
should be deleted. If the WHERE clause is omitted, all records will WHERE some_column = some_value;
be deleted.

UPDATE Statement
The UPDATE statement is used to edit records (rows) in a table. It
includes a SET clause that indicates the column to edit and a UPDATE table_name
WHERE clause for specifying the record(s). SET column1 = value1, column2 = value2
WHERE some_column = some_value;
AND Operator
The AND operator allows multiple conditions to be combined.
Records must match both conditions that are joined by AND to SELECT model
be included in the result set. The given query will match any car FROM cars
that is blue and made after 2014. WHERE color = 'blue'
  AND year > 2014;

% Wildcard
The % wildcard can be used in a LIKE operator pattern to
match zero or more unspeci ed character(s). The given query SELECT name
will match any movie that begins with The , followed by zero or FROM movies
more of any characters. WHERE name LIKE 'The%';

AS Clause
Columns or tables can be aliased using the AS clause. This
allows columns or tables to be speci cally renamed in the SELECT name AS 'movie_title'
returned result set. The given query will return a result set with FROM movies;
the column for name renamed to movie_title .

OR Operator
The OR operator allows multiple conditions to be combined.
Records matching either condition joined by the OR are SELECT name
included in the result set. The given query will match customers FROM customers
whose state is either 'CA' or 'NY' . WHERE state = 'CA'
   OR state = 'NY';

SELECT Statement
The SELECT * statement returns all columns from the provided
table in the result set. The given query will fetch all columns and SELECT *
records (rows) from the movies table. FROM movies;

_ Wildcard
The _ wildcard can be used in a LIKE operator pattern to
match any single unspeci ed character. The given query will SELECT name
match any movie which begins with a single character, followed FROM movies
by ove . WHERE name LIKE '_ove';

ORDER BY Clause
The ORDER BY clause can be used to sort the result set by a
particular column either alphabetically or numerically. It can be SELECT *
ordered in two ways: FROM contacts
ORDER BY birth_date DESC;
● DESC is a keyword used to sort the results in descending
order.

● ASC is a keyword used to sort the results in ascending


order (default).
LIKE Operator
The LIKE operator can be used inside of a WHERE clause to
match a speci ed pattern. The given query will match any movie SELECT name
that begins with Star in its title. FROM movies
WHERE name LIKE 'Star%';

DISTINCT Clause
Unique values of a column can be selected using a DISTINCT
query. For a table contact_details having ve rows in which the SELECT DISTINCT city
city column contains Chicago, Madison, Boston, Madison, and FROM contact_details;
Denver, the given query would return:

● Chicago

● Madison

● Boston

● Denver

BETWEEN Operator
The BETWEEN operator can be used to lter by a range of values.
The range of values can be text, numbers, or date data. The SELECT *
given query will match any movie made between the years 1980 FROM movies
and 1990, inclusive. WHERE year BETWEEN 1980 AND 1990;

LIMIT Clause
The LIMIT clause is used to narrow, or limit, a result set to the
speci ed number of rows. The given query will limit the result SELECT *
set to 5 rows. FROM movies
LIMIT 5;

NULL Values
Column values can be NULL , or have no value. These records
can be matched (or not matched) using the IS NULL and IS NOT SELECT address
NULL operators in combination with the WHERE clause. The given FROM records
query will match all addresses where the address has a value or WHERE address IS NOT NULL;
is not NULL .

WHERE Clause
The WHERE clause is used to lter records (rows) that match a
certain condition. The given query will select all records where SELECT title
the pub_year equals 2017 . FROM library
WHERE pub_year = 2017;

You might also like