SQL Flashcards
1
Q
SELECT aisle, SUM(quantity) FROM groceries GROUP BY aisle;
A
SELECT aisle, SUM(quantity) FROM groceries GROUP BY aisle;
- remember if you ‘SELECT name’ it won’t give you meaningful results. ‘SELECT x’ should be the same as the ‘GROUP BY x’
2
Q
Give an example of sub query
A
SELECT * FROM exercise_logs WHERE type IN (
SELECT type FROM drs_favorites);
the query in the () is the inner query. What this does is it selects the results that appear in the drs_favorites table that also appears in the exercise logs table (which is a larger table)
// eg using LIKE
SELECT * FROM exercise_logs WHERE type IN (
SELECT type FROM drs_favorites WHERE reason LIKE “%cardiovascular%”);
(as seen in More advanced SQL queries video 3). NB the % above act as a wildcard