SQL Flashcards
SQL: Datatype for an id
INTEGER PRIMARY KEY
SQL: How to create a table called “mytable” with an id and columns a, b and c, which are text, integer, text respectively.
CREATE TABLE mytable (id INTEGER PRIMARY KEY, a TEXT, b INTEGER, c TEXT);
SQL: Datatype for an integer
INTEGER
SQL: Datatype for a string
TEXT
SQL: How to insert a row with ints and strings into table “mytable”
INSERT INTO mytable VALUES (“yes”, 1, “no”, 2);
SQL: What goes at the end of every line?
;
SQL: How to select all columns, or the entire table, from table “mytable”
SELECT * FROM mytable;
SQL: How to select column “a” from table “mytable”
SELECT a FROM mytable;
SQL: How to select columns “a” and “c” from table “mytable”
SELECT a, c FROM mytable;
SQL: How to select all columns from table “mytable” and order by column “b”
SELECT * FROM mytable ORDER BY b;
SQL: Does ordering by a column return with that column in ascending or descending order?
Ascending
SQL: How to select all columns from table “mytable”, but only rows where column “b” is greater than 5?
SELECT * FROM mytable WHERE b > 5;
SQL: How to get sum of column b from table “mytable”
SELECT SUM(b) FROM mytable;
SQL: How to get max of column b from table “mytable”
SELECT MAX(b) FROM mytable;
SQL: How to get table “mytable” grouped by column “c”, with summary statistics of the max and sum of column “b”, while also displaying the value from column “c” corresponding to each group?
SELECT c, MAX(b), SUM(b) FROM mytable GROUP BY c;
SQL: Summary statistic for total number of rows in a group
COUNT(*) (could also use a particular column)
SQL: Summary statistic for average value of column “b” in a group
AVG(b)
SQL: What pitfall can happen when you include a non-aggregated column in a group by, and how do you avoid this?
SQL displays the first value of the row for that group, misleading when there are multiple rows. To avoid this, only include the column you’re grouping by as a non-aggregated column, and no others.
SQL: How to combine multiple logicals?
AND or OR
SQL: How to select rows from table “mytable” where column “b” has a value of either “warriors,” “bucks,” or “raptors”?
Select * From mytable Where b in (“warriors”, “bucks”, “raptors”);
SQL: How to select rows from table “mytable” where column “b” is only one of the values present in column “b” for a different table called “other_table”?
Select * From mytable Where b in
(Select b From other_table);
(I feel like this could be optimized…)
SQL: How to revers the value of any logical?
NOT
SQL: Describe the difference between WHERE and HAVING
WHERE filters the rows from the table you’re querying to only include some of them in the results of your query.
HAVING filters out some of the rows in your aggregated data table. So when you are using a Group By, HAVING allows you to filter the results of the Group By based on some criterion.
SQL: How do you get the sum of column “b” for table “mytable” when grouped by column “c”, and only include groups where the sum of column “b” exceeds 50?
Select c, SUM(b) as sum_b From my_table
Group By c
Having sum_b > 50;