Maam Celine's SQL 101 Crash Course
Maam Celine's SQL 101 Crash Course
SELECT
– table = primary_key
– describe acccounts;
– tinyitnt(binary) 1
– primary key => id
– select * from accounts where id = <id>
– What if i dont know the id: select * from accounts where user_name
=“Ariana Grande”
– Only need specific columns,
– select id, name, email, company_name from accounts where id = 5;
JOIN (A ^ B)
– describe price_histories (belong to product);
– FOREIGN KEY -> user_id, account_id, product_id variat_id
– SIMPLIFIED QUERIES
– select products.name, price_histories.old_price, price_histories.new_price
– from prcie_historyes
– inner join products
– on price_histories.product_id = products.id
– where product.id = 2014234234;
– What if product has no price history?
– empty set
– LEFT JOINS
– select products.name, price_histories.old_price,
price_histories.new_price
– from prcie_historyes
– left join products
– on price_histories.product_id = products.id
– where product.id = 2014234234;
– RIGHT JOINS
– select products.name, price_histories.old_price,
price_histories.new_price
– from prcie_historyes
– right join products
– on price_histories.product_id = products.id
– where product.id = 2014234234;
– Find productsw
EDITING A ROW
– ex: select * from products where account_id =6154;
– how to add info to the
– update products set description = “Bakery Hot Item” where id = 2011465;
– i
INSERTING
DESTROY
– delete from products where id = 2064321 (letʼs assume this is the pb);
AGGREGATION
What if products.count > 1000?
– select a.name, count(p.id) from accounts a inner join products p on
a.id=p.account_id
– group by a.name
– where count(p.id) > 1000; -> IS WRONG!
– HAVING (only to functions / methods) -> group by
– having count(p.id) > 1000
–
CREATING A DATABASE
– create database xtiantest default charset utf8;
– use xtiantest;
– show tables;
– create products (stock_no varchar(100), name varchar(100), cost
decimal(15,4) )
dESTROYING
– drop table Persons;
– drop database xtiantest;
OPERATORS
IN OPERATOR
– select name from products where account id= 5 liimt 10;
AND CONDITIONS
– select id ,name from products where account_id = 5 and retail_price < 2000;
NOT
CHALLENGE: HOMEWORK
– Coalesce and exict and having.