SQL For Interview Prep - SQL Interview Prep Cheatsheet - Codecademy
SQL For Interview Prep - SQL Interview Prep Cheatsheet - Codecademy
SELECT Statement
ORDER BY Clause
The ORDER BY clause can be used to sort the result set SELECT *
by a particular column either alphabetically or
FROM contacts
numerically. It can be ordered in two ways:
ORDER BY birth_date DESC;
DESC is a keyword used to sort the results in
descending order.
DISTINCT Clause
Chicago
Madison
Boston
Denver
LIMIT Clause
The GROUP BY clause will group records in a result set SELECT rating,
by identical values in one or more columns. It is often
COUNT(*)
used in combination with aggregate functions to query
information of similar records. The GROUP BY clause FROM movies
can come after FROM or WHERE but must come before GROUP BY rating;
any ORDER BY or LIMIT clause.
The given query will count the number of movies per
rating.
The SQL CASE statement enables control flow in SQL. SELECT name,
It allows for one or more conditions ( WHEN condition
CASE
THEN result) and an optional default case ( ELSE ). The
query above will provide each rating a value for the WHEN rating > 8 THEN "Excellent"
specified ranges within the result set. WHEN rating > 5 THEN "Good"
WHEN rating > 3 THEN "Okay"
ELSE "Bad"
END
FROM movies;
HAVING Clause
The HAVING clause is used to further filter the result SELECT year,
set groups provided by the GROUP BY clause. HAVING is
COUNT(*)
often used with aggregate functions to filter the result
set groups based on an aggregate property. The given
FROM movies
query will select only the records (rows) from only years GROUP BY year
where more than 5 movies were released per year. HAVING COUNT(*) > 5;
The HAVING clause must always come after a GROUP BY
clause but must come before any ORDER BY or LIMIT
clause.
WHERE Clause
The WHERE clause is used to filter records (rows) that SELECT title
match a certain condition. The given query will select
FROM library
all records where the pub_year equals 2017 .
WHERE pub_year = 2017;
ROUND() Function
Outer Join
An outer join will combine rows from different tables SELECT column_name(s)
even if the join condition is not met. In a LEFT JOIN ,
FROM table1
every row in the left table is returned in the result set,
and if the join condition is not met, then NULL values
LEFT JOIN table2
are used to fill in the columns from the right table. ON table1.column_name =
table2.column_name;
Inner Join
The JOIN clause allows for the return of results from SELECT *
more than one table by joining them together with
FROM books
other results based on common column values
specified using an ON clause. INNER JOIN is the default JOIN authors
JOIN and it will only return results matching the ON books.author_id = authors.id;
condition specified by ON .
Column Constraints
Column constraints are the rules applied to the values CREATE TABLE student (
of individual columns:
id INTEGER PRIMARY KEY,
PRIMARY KEY constraint can be used to uniquely name TEXT UNIQUE,
identify the row. grade INTEGER NOT NULL,
UNIQUE columns have a different value for every age INTEGER DEFAULT 10
row. );
NOT NULL columns must have a value.
The ALTER TABLE statement is used to modify the ALTER TABLE table_name
columns of an existing table. When combined with the
ADD column_name datatype;
ADD COLUMN clause, it is used to add a new column.
Primary Key
UPDATE Statement
NOT NULL
UNIQUE
Primary Keys
Print Share