SQL
Codecademy SQL
Manipulation
Data Type Includes:
SELECT from a Table
Set LIMIT
CREATE TABLE
INSERT to a Table
Add Coln
UPDATE Value
Query
Comparison Operators
LIKE Operator
BETWEEN Operator
AND and OR Operator
ORDER
Aggregate Functions
COUNT
GROUP BY
Having
SUM
MAX and MIN and Average
DISTINCT
CASE
Multiple Tables
Primary Keys
Select from Multiple tables
Join (Combine)
Use AS to Rename
UNION
Manipulation
Clauses perform specific tasks in SQL. By convention, clauses are written in capital
letters. Clauses can also be referred to as commands.
Data Type Includes:
1. Integer: a positive or negative whole number
2. Text: a text string
3. Date: the date formatted as YYYY-MM-DD for the year, month, and day
4. Real: a decimal value
Set LIMIT
To save space
SELECT *
FROM tutorial.us_housing_units
LIMIT 100
CREATE TABLE
INSERT to a Table
INSERT INTO celebs (id, name, age) VALUES (3, 'Jeremy Lin', 26);
INSERT INTO celebs (id, name, age) VALUES (4, 'Taylor Swift', 26);
Add Coln
Add a coln that contains value type TEST
UPDATE Value
Query
Comparison Operators
The same as C++, except we use = in SQL instead of == .
<> functions the same as != and it is more widely used so <> is recommended.
LIKE Operator
The _ means you can substitute any (one) individual character here without
breaking the pattern.
% is a wildcard character that matches zero or more missing letters in the pattern.
SELECT * FROM movies WHERE name LIKE 'a%'; /* any string starts wit
h a or A*/
SELECT * FROM movies WHERE name LIKE '%man%'; /* any string contain
s man (not case sentisive)*/
BETWEEN Operator
This statement filters the result set to only include movies with names that begin
with letters A up to but not including J.
SELECT * FROM movies WHERE year BETWEEN 1990 AND 2000 AND genre =
'comedy';
SELECT * FROM movies WHERE genre = 'comedy' OR year < 1980;
ORDER
SELECT * FROM movies ORDER BY imdb_rating DESC;
SELECT * FROM movies ORDER BY imdb_rating ASC;
SELECT * FROM movies ORDER BY imdb_rating ASC LIMIT 3; /* only show
the first 3 rows */
Aggregate Functions
COUNT
COUNT() is a function that takes the name of a column as an argument and counts the
number of rows where the column is not NULL .
GROUP BY
Having
The WHERE clause wont sometimes since it doesnt allow you to filter on aggregate
columnsthats where the HAVING clause comes in
SELECT year,
month,
MAX(high) AS month_high
FROM tutorial.aapl_historical_stock_price
GROUP BY year, month
HAVING MAX(high) > 400
ORDER BY year, month
SUM
DISTINCT
CASE
SELECT player_name,
year,
CASE WHEN year = 'SR' THEN 'yes'
ELSE NULL END AS is_a_senior
FROM benn.college_football_players
Multiple Tables
Primary Keys
A primary key serves as a unique identifier for each row or record in a given table. It is
useful when combining 2 tables.
Join (Combine)
(INNER) JOIN : Returns records that have matching values in both tables
LEFT (OUTER) JOIN : Return all records from the left table, and the matched
records from the right table
RIGHT (OUTER) JOIN : Return all records from the right table, and the matched
records from the left table
FULL (OUTER) JOIN : Return all records when there is a match in either left or right
table
SELECT * FROM
albums JOIN artists /* starting combining */
ON albums.artist_id = artists.id; /* combining condition */
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
2. LEFT JOIN ( LEFT OUTER JOIN ) if there is a match, add together. If there is not,
still add but not match. It will return every row in the left table, and if the join
condition is not met, NULL values are used to fill in the columns from the right
table.
SELECT * FROM
albums LEFT JOIN artists
ON albums.artist_id = artists.id;
3. RIGHT JOIN
4. FULL OUTER JOIN
5. Self Join
Use AS to Rename
It can be confusing when two tables have a column with the same name.
SELECT
albums.name AS 'Album',
albums.year,
artists.name AS 'Artist'
FROM
albums
JOIN artists ON
albums.artist_id = artists.id
WHERE
albums.year > 1980;
UNION
The UNION operator is used to combine the result-set of two or more SELECT
statements.
Each SELECT statement within UNION must have the same number of columns.
The columns must also have similar data types.
The columns in each SELECT statement must also be in the same order.
or
positively correlated
not correlated
negatively correlated
positively correlated
(inconclusive)
no correlation
(inconclusive)
negatively correlated