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.