Code Examples Flashcards
SELECT COUNT(*)
FROM table_name;
This counts all the rows in the Table.
SELECT SUM(downloads)
FROM table_name;
This adds all values in the downloads column of the Table.
SELECT MAX(downloads)
FROM table_name;
This returns the largest value in the downloads column of the Table.
SELECT MIN(downloads)
FROM table_name;
This returns the smallest value in the downloads column of the Table.
SELECT AVG(downloads)
FROM table_name;
This returns the average number of downloads column of the Table.
SELECT ROUND(price, 0)
FROM table_name;
This rounds the values in the price column Table to 0 decimal places.
SELECT year,
AVG(imdb_rating)
FROM movies
GROUP BY year
ORDER BY year;
This returns the the year and average imdb_rating values that are grouped and ordered by the year.
SELECT ROUND(imdb_rating),
COUNT(name)
FROM movies
GROUP BY 1
GROUP BY 1;
This rounds the imdb_rating values and counts all the columns in name from the Table movies and the values are grouped by the first column.
SELECT year,
genre,
COUNT(name)
FROM movies
GROUP BY 1, 2
HAVING COUNT(name) > 10;
This selects the year, genre, and counts the name column from the movies Table, Grouping them by column 1 and column 2 but only counting the name values that are greater than 10.
SELECT orders.order_id,
customers.customer_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id
INNER JOIN
SELECT *
FROM table1
LEFT JOIN table2
ON table1.c2 = table2.c2;
LEFT JOIN
SELECT shirts.shirts_color,
pants.pants_color
FROM shirts
CROSS JOIN pants;
CROSS JOIN
SELECT *
FROM table1
UNION
SELECT *
FROM table2;
UNION
WITH previous_results AS (
SELECT …..
)
SELECT *
FROM previous_results
JOIN customers
ON _____ = ______;
WITH
What is a PRIMARY KEY?
Special columns
What are the requirements of a PRIMARY KEY?
None of the value can be NULL
Each value must be unique (can’t have duplicate values)
A Table can not have more than one PRIMARY KEY column
What is a FOREIGN KEY?
A PRIMARY KEY that appears in a different Table.
What is the difference between PRIMARY KEYs and FOREIGN KEYs?
PRIMARY KEYs are unique and FOREIGN KEYs have descriptive names.
The most common types of JOIN do what with PRIMARY and FOREIGN KEYs?
The most common types of JOINs, will be joining a FOREIGN KEY from one Table with the PRIMARY KEY of another Table.
SELECT column1, column2
FROM table_name;
SELECT *
FROM table_name;
SELECT query
SELECT name AS ‘Titles’
FROM movies;
AS - renames column or Table
SELECT DISTINCT tools
FROM inventory;
DISTINCT - removes duplicate values
SELECT *
FROM movies
WHERE imdb_rating>8;
WHERE filters the result set to include rows where the condition is true
SELECT *
FROM movies
WHERE name LIKE ‘Se_en’
LIKE is used with the WHERE clause to search for a specific pattern.
SELECT *
FROM movies
WHERE name LIKE ‘A%’;
SELECT *
FROM movies
WHERE name LIKE ‘%A’;
SELECT *
FROM movies
WHERE name LIKE ‘%A%’;
The wildcard character, %, matches zero or missing letters in a patter.
Example one matches movies that begin with A
Example two matches movies that end with A
Example three matches movies that contain A in it
SELECT name
FROM movies
WHERE imdb_rating IS NOT NULL;
Unknown values are indicated by NULL
IS NOT NULL
IS NULL
Will be used as comparison operators for WHERE clause and not = or !=
SELECT *
FROM movies
WHERE year BETWEEN 1990 AND 1999;
The result set will include movie with years from 1990 up to and including 1999
SELECT *
FROM movies
WHERE name BETWEEN ‘A’ AND ‘J’;
The result set will include movies with the names that begin with the letter ‘A’ up to, but not including the letter ‘J’.
SELECT *
FROM movies
WHERE year BETWEEN 1990 AND 1999
AND genre = ‘romance’;
With AND, both conditions must be true for the row to be included in the result
SELECT *
FROM movies
WHERE year > 2014
OR genre = ‘action’;
With OR, if any of the conditions are true then the row is added to the result.
SELECT *
FROM movies
WHERE imdb_rating > 8
ORDER BY year DESC;
ORDER BY sorts the result set by a particular column.
Always goes after WHERE clause if WHERE is present
SELECT *
FROM movies
LIMIT 10;
LIMIT specifies the maximum number of rows the result will have
Always goes at the very end of the query
SELECT name,
CASE
WHEN imdb_rating > 8 THEN ‘Fantastic’
WHEN imdb_rating > 6 THEN ‘Poorly Received’
ELSE ‘Avoid at All costs’
END AS ‘Review’
FROM movies;
CASE statement creates different outputs
-usually in the SELECT statement
- WHEN tests a condition
- THEN gives us the string
- CASE must end with END