SELECT id, occurred_at,total_amt_usd//select what rows to show FROM orders //from what table ORDER BY occurred_at DESC//A to Z order + DESC Z to A (descend) LIMIT 10//quantity of rows to show //////////////////////////////////////////////////////////////////////////////////////// ORDER BY account_id,total_amt_usd DESC//account_id gets organized A to Z, and then total_amt_usd DESC Z to A ////////////////////////////////////////////////////////////////////////////////////////////// SELECT *//select every column FROM orders WHERE total_amt_usd < 500//WHERE filters and shows data according to <,>,=,<=,>=,!=, LIMIT 10; ////////////////////////////////////////////////////////////////////////////////////////////////// SELECT name, website, primary_poc FROM accounts WHERE name = 'Exxon Mobil';//for non numeric data /////////////////////////////////////////////////////////////////////////////////////////////////// SELECT id, (standard_amt_usd/total_amt_usd)*100 AS std_percent, total_amt_usd//arithemtic operations, AS renames the new table created from the operation FROM orders LIMIT 10; ////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT * FROM accounts WHERE name LIKE '%s%'//LIKE looks for any similar string in the data '%something%' , % symbol means strings, so %s strings before s nothing after, s& , wild cards//nothing before and strings after s, %s% before and after s strings ////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT name, primary_poc, sales_rep_id FROM accounts WHERE name IN ('Walmart', 'Target', 'Nordstrom');//IN looks for more than one operator with an specific information, specific values ////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT name, primary_poc, sales_rep_id FROM accounts WHERE name NOT IN ('Walmart', 'Target', 'Nordstrom');//NOT gives you the opposite of what you looking for ,this case everything that is not //WALMART,TARGET,NORDSTROM SELECT name FROM accounts WHERE name NOT LIKE 'C%';//using LIKE, gives you all the names that do not start with C /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT * FROM orders WHERE standard_qty > 1000 AND poster_qty = 0 AND gloss_qty = 0;//AND gives you the option to add more than paramater from diferent columns /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT name FROM accounts WHERE name NOT LIKE 'C%' AND name LIKE '%s';//can be used with different other reserved words, this case, names that do NOT start with C and //names that end in S /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT occurred_at, gloss_qty FROM orders WHERE gloss_qty BETWEEN 24 AND 29;//BETWEEN is used instead of AND when we look for data in the same column, this case gloss_qty //between 24 and 29, includes 24 and 29 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT * FROM web_events WHERE channel IN ('organic', 'adwords') AND occurred_at BETWEEN '2016-01-01' AND '2017-01-01'//more complicated code, IN looks for organic OR adwords, //and BETWEEN gives you the date stamp , format is important ORDER BY occurred_at DESC; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT id FROM orders WHERE gloss_qty > 4000 OR poster_qty > 4000;//OR statement works like in normal programming, or this or that, exclusive /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT * FROM orders WHERE standard_qty = 0 AND (gloss_qty > 1000 OR poster_qty > 1000);//parenthesis are important for the logic too /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT * FROM accounts WHERE (name LIKE 'C%' OR name LIKE 'W%') //parenthesis again for the logic AND ((primary_poc LIKE '%ana%' OR primary_poc LIKE '%Ana%') AND primary_poc NOT LIKE '%eana%'); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT orders.*, accounts.*//JOIN works as FROM, this case select everything from both tables and join them on id and account id FROM accounts JOIN orders ON accounts.id = orders.account_id; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT orders.standard_qty, orders.gloss_qty, //here you specify SELECT standard_qty from orders table, with the period format//(orders.standard_qty),and so on, FROM orders, and JOIN with accounts, on those two //columns,account_id and id orders.poster_qty, accounts.website, accounts.primary_poc FROM orders JOIN accounts ON orders.account_id = accounts.id ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT a.primary_poc, w.occurred_at,w.channel,a.name//SELECT those columns with the alias made in FROM, following what column with the dot FROM web_events w//FROM web_events alias (w) JOIN accounts a//JOIN with accounts alias (a) ON w.account_id = a.id//ON using the alias and the PK(primary key) and FK(foreign key) WHERE a.name = 'Walmart'//WHERE name from account (a)table a.name = 'Walmart' SELECT r.name region, s.name rep, a.name account//SELECT columns with the alias already set up, and what column from those tables adding //names to the tables or will not show all the data FROM sales_reps s//FROM sales_reps alias (s) JOIN region r//JOIN region alias (r) ON s.region_id = r.id//ON PK = FK JOIN accounts a//JOIN a third table accounts alias (a) ON a.sales_rep_id = s.id//ON PK = FK ORDER BY a.name DESC//ORDER BY account alias (a) a.name SELECT r.name region, a.name account, o.total_amt_usd/(o.total + 0.01) unit_price//SELECT columns from tables with alias, dot notation to select what from those tables //adding names or the columns will not show up FROM orders o//FROM orders alias (o) JOIN accounts a//JOIN accounts alias (a) ON o.account_id = a.id//ON PK = FK using alias and the specific name of the KEYS (dot notation) JOIN sales_reps s//JOIN third table sales_reps alias (s) ON a.sales_rep_id = s.id// ON PK = FK JOIN region r//JOIN a fourth table region alias r ON s.region_id = r.id//ON PK = FK ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT r.name region, s.name rep, a.name account//after alias each columns new a new alias, or wont show up, because they share (name) FROM sales_reps s JOIN region r ON s.region_id = r.id JOIN accounts a ON a.sales_rep_id = s.id WHERE r.name = 'Midwest' ORDER BY a.name; SELECT r.name region, s.name rep, a.name account FROM sales_reps s JOIN region r ON s.region_id = r.id JOIN accounts a ON a.sales_rep_id = s.id WHERE r.name = 'Midwest' AND s.name LIKE 'S%' ORDER BY a.name; SELECT r.name region, s.name rep, a.name account FROM sales_reps s JOIN region r ON s.region_id = r.id JOIN accounts a ON a.sales_rep_id = s.id WHERE r.name = 'Midwest' AND s.name LIKE '% K%'//to pull LAST NAMES that beging with K, ORDER BY a.name; SELECT r.name region, a.name account, o.total_amt_usd/(o.total + 0.01) unit_price FROM region r JOIN sales_reps s ON s.region_id = r.id JOIN accounts a ON a.sales_rep_id = s.id JOIN orders o ON o.account_id = a.id WHERE o.standard_qty > 100; SELECT r.name region, a.name account, o.total_amt_usd/(o.total + 0.01) unit_price FROM region r JOIN sales_reps s ON s.region_id = r.id JOIN accounts a ON a.sales_rep_id = s.id JOIN orders o ON o.account_id = a.id WHERE o.standard_qty > 100 AND o.poster_qty > 50 ORDER BY unit_price; SELECT r.name region, a.name account, o.total_amt_usd/(o.total + 0.01) unit_price FROM region r JOIN sales_reps s ON s.region_id = r.id JOIN accounts a ON a.sales_rep_id = s.id JOIN orders o ON o.account_id = a.id WHERE o.standard_qty > 100 AND o.poster_qty > 50 ORDER BY unit_price DESC; SELECT DISTINCT a.name, w.channel//SELECT DISTINCT pulls the non repeating and unique data FROM accounts a JOIN web_events w ON a.id = w.account_id WHERE a.id = '1001'; SELECT o.occurred_at, a.name, o.total, o.total_amt_usd FROM accounts a JOIN orders o ON o.account_id = a.id WHERE o.occurred_at BETWEEN '01-01-2015' AND '01-01-2016'//BETWEEN for dates ORDER BY o.occurred_at DESC; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT COUNT (orders.poster_amt_usd) IDs//COUNT gives you how many rows a table has, doesnt count NULL values FROM orders; SELECT SUM(poster_qty) AS total_poster_sales//SUM gives you the sum of and specific column FROM orders; SELECT SUM(standard_amt_usd)/SUM(standard_qty) AS standard_price_per_unit //SUM can be used in the middle of a math expresion FROM orders; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT MIN(occurred_at)//MIN gives you the minimun value of that column FROM orders; SELECT MAX(occurred_at)//MAX gives you the maximum value of that column FROM web_events; SELECT AVG(standard_qty) mean_standard, AVG(gloss_qty) mean_gloss, //AVG gives you the average value of that column AVG(poster_qty) mean_poster, AVG(standard_amt_usd) mean_standard_usd, AVG(gloss_amt_usd) mean_gloss_usd, AVG(poster_amt_usd) mean_poster_usd FROM orders; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT a.name, o.occurred_at FROM accounts a JOIN orders o ON a.id = o.account_id ORDER BY occurred_at LIMIT 1; SELECT SUM(o.total_amt_usd) TotalUsd, a.name CompanyName FROM orders o JOIN accounts a ON o.account_id = a.id GROUP BY CompanyName//GROUP BY, groups the date and does the operation there, is like minitables inside the big table ORDER BY TotalUsd SELECT w.occurred_at, w.channel, a.name FROM web_events w JOIN accounts a ON w.account_id = a.id ORDER BY w.occurred_at DESC LIMIT 1; SELECT COUNT (w.channel) ChannelCount, w.channel NameOfChannel FROM web_events w GROUP BY NameOfChannel SELECT a.primary_poc FROM web_events w JOIN accounts a ON a.id = w.account_id ORDER BY w.occurred_at LIMIT 1; SELECT a.name, MIN(total_amt_usd) smallest_order FROM accounts a JOIN orders o ON a.id = o.account_id GROUP BY a.name ORDER BY smallest_order; SELECT r.name, COUNT(*) num_reps FROM region r JOIN sales_reps s ON r.id = s.region_id GROUP BY r.name ORDER BY num_reps; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// AND SUBSTRING(RD.RECEIVE_DATE,1,10) BETWEEN '2022-01-25' and '2022-04-30' //to use dates , in a better way /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT a.name, AVG(o.standard_qty)AVG_standard, AVG(o.gloss_qty) AVG_gloss, AVG(o.poster_qty)AVG_poster FROM accounts a JOIN orders o ON a.id = o.account_id GROUP BY a.name ORDER BY a.name DESC//DESC Z-A SELECT SR.name, WE.channel, COUNT(WE.channel) channel_count FROM sales_reps SR JOIN accounts ON SR.id = accounts.sales_rep_id JOIN web_events WE ON accounts.id = WE.account_id GROUP BY WE.channel , SR.name ORDER BY channel_count DESC //GROUP BY name and channel, channel will give the COUNT of those channels in the table, under the same name /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT s.name, s.id, COUNT(*) numb_counts FROM sales_reps s JOIN accounts a ON s.id = a.sales_rep_id GROUP BY s.name, s.id//GROUP BY will group all of the people under same name (Lola,Lola,Lola as only one Lola), the by ID, and the COUNT HAVING COUNT(*) > 5// will count those groups individually ORDER BY COUNT(*) SELECT a.name,a.id, COUNT(*) numb_orders FROM accounts a JOIN orders o ON a.id = o.account_id GROUP BY a.name, a.id HAVING COUNT(*) > 20 ORDER BY numb_orders SELECT a.name,a.id, COUNT(*) numb_orders FROM accounts a JOIN orders o ON a.id = o.account_id GROUP BY a.name, a.id ORDER BY numb_orders DESC//ORDER BY DESC to get Z-A and limiting the result as 1 to show the MAX LIMIT 1 SELECT a.name name_of ,a.id id , SUM(o.total_amt_usd) total_sum//SUM is used to add the total_amt_us of each account under the same order FROM accounts a JOIN orders o ON a.id = o.account_id GROUP BY a.name, a.id HAVING SUM(o.total_amt_usd) >30000 ORDER BY total_sum SELECT a.id, a.name, w.channel, COUNT(*) use_of_channel//SELECT a.id a.name and w.channel FROM tables (accounts a) and (web_events w) FROM accounts a//COUNT(*) will count everything on the table that we created using the above columns JOIN web_events w//so,wont count the whole DATA set, just the new DATA we pulled. ON a.id = w.account_id GROUP BY a.id, a.name, w.channel//GROUP BY , uses what we have on SELECT HAVING COUNT(*) > 6 AND w.channel = 'facebook'//HAVING is used like WHERE, but HAVING is for operations like SUM , COUNT, etc ORDER BY use_of_channel; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// field1 AS last_name//AS renames the column name , for an easier work, does not change the data base table AS costumers SELECT last_name FROM costumers WHERE last_name LIKE 'CH%'; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT country FROM apt-philosophy-358219.costumer_data.customer_address WHERE LENGTH(country) > 2//LENGTH gives the lenght of certain cell /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT DISTINCT customer_id FROM apt-philosophy-358219.costumer_data.customer_address WHERE SUBSTR (country, 1, 2) = 'US'//SUBSTR gives a portion of a string, goes like from what column, starting point and # of characters after the starting point /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT DISTINCT customer_id FROM apt-philosophy-358219.costumer_data.customer_address WHERE TRIM(state) = 'OH'//TRIM eliminates any extra space in words /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT MIN(length) AS min_length, MAX(length) AS max_length//MIN and MAX gives the respective for certain cell data FROM apt-philosophy-358219.cars.car_info SELECT * FROM apt-philosophy-358219.cars.car_info WHERE num_of_doors IS NULL//NULL checks for empty cells /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// UPDATE cars.car_info//UPDATES data in table SET num_of_doors = 'four' WHERE make = 'dodge' AND fuel_type ='gas' AND body_style ='sedan' DELETE cars.car_info//DELETES certain rows in a table WHERE compression_ratio = 70; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SELECT CAST(purchase_price AS FLOAT64)//CAST converts data types, here from string to float FROM apt-philosophy-358219.customer_data.customer_purchase ORDER BY CAST(purchase_price AS FLOAT64) DESC SELECT CAST(date As date) AS date_only , purchase_price//CAST converts date time format to only date FROM apt-philosophy-358219.customer_data.customer_purchase WHERE date BETWEEN '2020-12-01' AND '2020-12-31' SELECT CONCAT(product_code, product_color)//CONCAT concatenates 2 columns and show them as a new column FROM apt-philosophy-358219.customer_data.customer_purchase WHERE product = 'couch' SELECT COALESCE(product, product_code) AS code_info//COALESCE looks for NULL values and replace them with whatever column you select FROM apt-philosophy-358219.customer_data.customer_purchase