SQL Commands Flashcards
(110 cards)
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);