SQL Commands Flashcards
SELECT ALL
SELECT * FROM abc_table
CREATE A TABLE FROM SCRATCH
CREATE TABLE favoritebooks(id INTEGER PRIMARY KEY, name TEXT, rating INTEGER); INSERT INTO favoritebooks VALUES (1, "Brown Bear", 10), (2, "If You Give A Mouse A Cookie" , 7), (3, "The Hungry Caterpillar" , 9) ;
INSERT INTO favoritebooks _____? WHAT IS MISSING
INSERT INTO favoritebooks VALUES
Find Movie of 2000 or higher and sort by ASC release_year CREATE TABLE movies (id INTEGER PRIMARY KEY, name TEXT, release_year INTEGER);
INSERT INTO movies VALUES (1, “Avatar”, 2009);
INSERT INTO movies VALUES (2, “Titanic”, 1997);
INSERT INTO movies VALUES (3, “Star Wars: Episode IV - A New Hope”, 1977);
INSERT INTO movies VALUES (4, “Shrek 2”, 2004);
INSERT INTO movies VALUES (5, “The Lion King”, 1994);
INSERT INTO movies VALUES (6, “Disney’s Up”, 2009);
SELECT * FROM movies;
SELECT * FROM movies
WHERE release_year >= 2000
ORDER BY release_year ASC;
Insert data into table lazy way
INSERT INTO active_wear VALUES
(1, “leggings”, “black”, “medium”, “Adidas”, 15, 59.99),
(2, “jacket”, “pink”, “small”, “Adidas”, 25, 59.99),
(3, “SOCKS”, “BLACK AND WHITE”, “unisex”, “Nike”, 50, 12.99),
(4, “Women T-shirt”, “White”, “large”, “Adidas”, 250, 35.99),
HOW MANY FROM A BRAND?
SELECt COUNT(brand) FROM active_store WHERE brand = "Nike";
Max product quantity
SELECT MAX(quantity), item FROM active_store;
Min product
SELECT MIN(price), item FROM active_store;
There is not BY after where
There is not BY after where
Order by price DESC - from high to lowest
SELECT * FROM store
ORDER BY price desc;
Order price by ASC price
SELECT * FROM store
ORDER BY price desc;
Select all columns and rows from the customers table where the value in the age column is greater than 21 and thevalue in the state column is ‘PA’
hint there is not BY after Where and , after AND (served on next line)
SELECT *FROM customers
WHERE age > 21
AND state = ‘PA’
Select all columns and rows from the customers table where the value in the plan column is “free” or “basic”
SELECT *FROM customers
WHERE plan IN (“free”, “basic”)
How to use WHERE and INCLUDING in conditional statement
SELECT *FROM customers
WHERE plan IN (“free”, “basic”)
Select all columns and rows from the customers table where the value in the age column is greater than 21, and order the results by age starting with the highest value and DESC down
SELECT *FROM customers
WHERE age > 21
ORDER BY age DESC
ORDER IS FOLLOWED BY BY
ORDER BY
Select the gender column and the number of rows in the students table, and group by the value of the gender column
SELECT gender, COUNT(*)FROM students
GROUP BY gender
Create a column called “type” which assigns whether someone is an “adult” or “minor” based on their age
SELECT name, CASE WHEN age > 18 THEN “adult”
ELSE “minor” END “type”
FROM customers
Create a column called “flag” which assigns a 1 if someone’s tenure is greater than 5 years
SELECT name, CASE WHEN sum(tenure) > 5
THEN 1ELSE 0 END “flag”
FROM customers
Select only the max age from the customers table
SELECT MAX(age)FROM customers
Join the customers table and orders table based on customer ID to select all instances of “name” from the customers table and show then associated “item” from the orders table.
SELECT customers.name, orders.item
FROM customers
LEFT JOIN ordersON customers.id = orders.customer_id
This will show you the names of all the columns in a table
HELP TABLE database.table
This will give you a sample of 20 rows from every column in the table
SELECT * FROM database.table LIMIT 20or
or
SELECT TOP 20 * FROM database.table
SET ID TO autoincrement
CREATE TABLE exercise_logs (id INTEGER PRIMARY KEY AUTOINCREMENT, type TEXT, minutes INTEGER, calories INTEGER, heart_rate INTEGER);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES (“biking”, 30, 100, 110);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES (“biking”, 10, 30, 105);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES (“dancing”, 15, 200, 120);
Select all in tables which activities you have done that burns more that 50 calories and order by calories burned
SELECT * FROM exercise_logs WHERE calories > 50 ORDER BY calories;
AND operator
FIND ACTIVITIES WHERE CALORIE BURN IS MORE THAT 50 AND MINUTES ARE LESS THAN 30 MINS
SELECT * FROM exercise_logs WHERE calories > 50 AND minutes < 30;
/ OR / OPERATOR
SELECT * FROM exercise_logs WHERE calories > 50 OR heart_rate > 100;
USING WHERE AND OR OPERATOR TOGETHER
SELECT title FROM songs ;
SELECT title FROM songs WHERE mood = “epic”
OR releaseD > 1990 ;
Several Where and AND
SELECT title FROM songs ;
SELECT title FROM songs WHERE mood = “epic”
OR released > 1990 ;
SELECT title FROM songs WHERE mood = “epic”
AND released > 1990 AND duration <= 240;
SHOW JUST TYPING LOGS
SELECT * FROM exercise_logs WHERE type = “biking”;
FIND TYPE OF ACTIVITIES THAT ARE OUTSIDE ACTIVITIES USING WHERE AND OR OPERATORS
Second example show IN operator
SELECT * FROM exercise_logs WHERE type = “biking” OR type = “hiking” OR type = “tree climbing” OR type = “rowing”;
second example
SELECT * FROM exercise_logs WHERE type IN (“biking”, “hiking”, “tree climbing”, “rowing”);
or not
SELECT * FROM exercise_logs WHERE type IN (“biking”, “hiking”, “tree climbing”, “rowing”);
creation of new table
INSERT INTO drs_favorites(type, reason) VALUES (“biking”, “Improves endurance and flexibility.”);
INSERT INTO drs_favorites(type, reason) VALUES (“hiking”, “Increases cardiovascular health.”);
FIND DOCTOR RECOMMENDED ACTIVITY
SELECT type FROM drs_favorites;
SELECT * FROM exercise_logs WHERE type IN (
SELECT type FROM drs_favorites);
SELECT type FROM drs_favorites WHERE reason = “Increases cardiovascular health.”);
SELECT type FROM drs_favorites WHERE reason = “Increases cardiovascular health.”);
like use
SELECT type FROM drs_favorites WHERE reason = “Increases cardiovascular health.”);
find sum of calories by type of activities and create new column in table
SELECT type, SUM(calories) AS total_calories FROM exercise_logs GROUP BY type;
HAVING IS USED FOR TOTAL GROUP AFTER GROUP BY AND AND INDIVIDUAL CALCULATIONS
SELECT * FROM exercise_logs;
SELECT type, SUM(calories) AS total_calories FROM exercise_logs GROUP BY type;
SELECT type, SUM(calories) AS total_calories FROM exercise_logs
GROUP BY type
HAVING total_calories > 150
Another having problem
SELECT * FROM exercise_logs;
SELECT type, AVG(calories) AS avg_calories FROM exercise_logs
GROUP BY type
HAVING total_calories > 50
SELECT type FROM exercise_logs GROUP BY type HAVING COUNT(*)
SELECT type FROM exercise_logs GROUP BY type HAVING COUNT(*)
PERFECT HAVING EXAMPLE remember no comma
SELECT author, sum(words) as total_words from books
GROUP BY author
HAVING total_words>1000000;
We’ve created a database of a few popular authors and their books, with word counts for each book. In this first step, select all the authors who have written more than 1 million words, using GROUP BY and HAVING. Your results table should include the ‘author’ and their total word count as a ‘total_words’ column.
SELECT author, sum(words) as total_words from books
GROUP BY author
HAVING total_words>1000000;
Now select all the authors that write more than an average of 150,000 words per book. Your results table should include the ‘author’ and average words as an ‘avg_words’ column.
SELECT author, avg(words) as avg_words
FROM books
GROUP BY author
HAVING avg_words>150000;
What does an organization ask you to do?
Purchase habits, how many customer use A service, How many use A and B, what is the age age range customer selects what service, how often users login, what is the average lengh of time user login any time
Heart rate count example
SELECT COUNT(*) FROM exercise_logs WHERE heart_rate > 220 - 30;