Download SQL for Beginners: Practical Exercises and Solutions and more Exams Advanced Data Analysis in PDF only on Docsity!
SQL for Beginners 2024-2025. Questions and Correct
Verified Answers. Graded A
A few month after diary app is being used, a person suggests a "how are you feeling" drop down menu for each entry
- how do you add an emotion column to the table?
- what's wrong with just adding it in the create statement? - ANS- you can't add it in the create statement bc it will just override all of the data that you've collected over the months
- use ALTER TABLE statement ALTER TABLE tableName ADD colNAME dataTYPE; ALTER TABLE diary_logs ADD emotion TEXT; a query language is used to .... - ANSinteract with the database filter results to only show activities where I burned more than 150 calories total across all the times that I've done that exercise - ANSuse HAVING clause SELECT type, SUM(calories) AS total_calories FROM exercise_logs GROUP BY type HAVING total_calories > 150);
find activities that have burned more than 50 calories, but taken less than 30 minutes - ANS- use AND operator to combine multiple conditions SELECT * FROM exercise_log WHERE calories > 50 AND minutes < 30; find where you've done activities that made you burn the most calories - ANSSELECT * FROM exercise_log WHERE calories > 50 ORDER BY calories; FULL OUTER JOINS? - ANSmatches rows on both the left and the the right tables if it can and fills in NULLS when it can't on either side
- also not supported in this environment group all of the exercises to show which heart rate zone each exercise is in
- add a new column for which zone the heart rate is in - ANS- group by CASE
- similar to if or switch statement SELECT type, heart_rate, CASE WHEN heart_rate > (220 - 30) THEN "above max" WHEN heart_rate > ROUND(0.90 * (220-30)) THEN "above target"
how do you change the order of evaluation of AND and OR operators? - ANSparenthesis how do you change the order of the data that is returned to you from a table? - ANSadd an ORDER BY whichColumn statement SELECT * FROM groceries ORDER BY aisle; how do you check if the database actually holds data? - ANSclick the table name on the right, which inserts a SELECT statement in your code how do you do an inverse query? - ANSSELECT * FROM exercise_logs WHERE type NOT IN ("biking", "hiking", "rowing", "tree climbing");
- just dancing would appear how do you filter the exercise logs to just show the biking? - ANSSELECT * FROM exercise_logs WHERE type = "biking"; how do you filter the exercise logs to show all of the ones that are done outside? the hard way - ANSSELECT * FROM exercise_logs WHERE type = "biking" OR type = "hiking" OR type = "tree climbing" OR type = "rowing";
how do you find out which items are just in aisles 6-12? (filtering results out? - ANSuse a WHERE columName >= 5 clause SELECT * FROM groceries WHERE aisle >= 5 ORDER BY aisle; how do you get the total number of items for each aisle, and also display the aisle number?? - ANSSELECT colNam, aggregateFunction(colName) FROM tableName GROUP BY colName SELECT aisle, SUM(quantity) FROM groceries GROUP BY aisle; how do you insert into a table? - ANSINSERT INTO groceries VALUES(1, "bananas", 4); INSERT INTO groceries VALUES (2, "peanut butter", 1); how do you make a table to store the list of groceries? (grocery names and amounts) - ANS- CREATE TABLE in all caps, and then name of table, then column names followed by data type inside parenthesis CREATE TABLE groceries (id INTEGER PRIMARY IDENTIFER, name TEXT, quantity INTEGER); how do you make sure you have the right number of items after each aisle? (total number of items per aisle) - ANSuse the GROUP BY whichCol clause
- add it to the end of a query
SELECT name FROM groceries;
- and then a list of grocery item names appears on the right under the results section how do you see how many calories you burn for each type of activity? - ANS- use aggregate query SELECT type, SUM(calories) FROM exercise_logs GROUP BY type; how do you show all of my exercises that the dr recommends for cardiovascular reasons? use LIKE operator - ANSSELECT * FROM exercise_logs WHERE type IN (SELECT type FROM drs_favorites WHERE reason LIKE %cardiovascular%");
- this should match any rows where the reason explanation has the word "cardiovascular" in it how do you show all of my exercises that the dr recommends for cardiovascular reasons? - ANSSELECT * FROM exercise_logs WHERE type IN (SELECT type FROM drs_favorites WHERE reason = "increases cardiovascular health"); how do you show how many exercises you have above target and how many exercises you have below target? - ANSSELECT type, heart_rate,
CASE
WHEN heart_rate > (220 - 30) THEN "above max" WHEN heart_rate > ROUND(0.90 * (220 - 30)) THEN "above target" WHEN heart_rate > ROUND(0.50 * (220 - 30)) THEN "within target" ELSE "below target" END as "hr_zone" FROM exercise_logs GROUP BY hr_zone' //shows that 4 exercises are below target and 4 exercises are within target how does cross join work?
- is it useful? why or why not - ANSSELECT * FROM student_grades, students;
query logs to see if heart rate ever goes above max
- max heart rate = 220 - age - ANSSELECT COUNT(*) FROM exercise_logs WHERE heart_rate > 220- 30; relational databases make it easy to .... give an example - ANSform relationships between tables
- for storing Kahn academy users and their badges ....
- have a users table
- and a badges tables
- and then a user/badges table to keep track of which users earned which badges by mapping user ids to badge ids
- this is better than storing everything about the users and everything about the badges in the user/badges table say a user returns and wants to modify their diary logs, how would you go about doing this? - ANS- you would need an UPDATE statement since the user has already entered the information UPDATE diary_logs SET content = "i had a horrible fight with no one" WHERE user_id AND date = "2015-04-01"; //ids are safer bc the app could be designed to let you create more than one entry per day
show all of the activities where we logged at least two logs for that type of exercise - ANS- use COUNT function SELECT type FROM exercise_logs GROUP BY type HAVING COUNT(*) >= 2; show average calories for all of the exercises where we burn more than 70 calories
- ANSSELECT type, AVG(calories) AS avg_calories FROM exercise_logs GROUP BY type HAVING avg_calories > 70); show how you would create a users table and a diary_logs table - ANSCREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT); CREATE TABLE diary_logs( id INTEGER PRIMARY KEY, user_id INTEGER, date TEXT, content TEXT); show how you would insert into the diary_log table - ANSINSERT INTO diary_logs(user_id, date, content) VALUES (1, "2015-04-01", "I had a horrible fight with tilly"); since you buy more than one of some item, how do you know the total number of items you'll purchase, and not just the total kinds of unique items? - ANSuse an aggregate function SELECT nameOfAggregateFunction(nameOfCol) FROM tableName SELECT SUM(quantity) FROM groceries;
- write a query using EXPLICIT INNER JOIN that outputs the student first name, last name, email, test, and grade - ANSSELECT first_name, last_name, email, test, grade FROM students JOIN student_grades ON students.id = student_grades.studnet_id; we have a students table with info on their id, first name, last name, email, phone, and birthday we have another table student_grades with id, student_id, test, and grade
- how are these two tables related ??? - ANSthe student_id in the student_grades table is the same as the id in the students table we have a students table with info on their id, first name, last name, email, phone, and birthday we have another table student_grades with id, student_id, test, and grade
- write a query using CROSS JOIN that outputs the student name and email next to each test grade - ANS- this means we need to JOIN the tables
- cross join
SELECT * FROM student_grades, students; we have a students table with info on their id, first name, last name, email, phone, and birthday we have another table student_grades with id, student_id, test, and grade
- write a query using EXPLICIT INNER JOIN that outputs the student name and email next to each test grade - ANS- explicit uses the JOIN keyword SELECT * FROM student_grades, students JOIN student_grades ON students.id = student_grades.student_id; we have a students table with info on their id, first name, last name, email, phone, and birthday we have another table student_grades with id, student_id, test, and grade
- write a query using EXPLICIT INNER JOIN that outputs the student name and email next to each test grade for grades > 90 - ANSSELECT first_name, last_name, email, test, grade FROM students JOIN student_grades ON students.id = student_grades.student_id WHERE grade > 90;
- Alice's project title will have a NULL since she hasn't started a project yet we want to create tables to store the data for a diary app
- what would a basic setup be? - ANS- a users table with id, and name
- a diary_logs table with id, user_id, date, and content what do we call an inner query? - ANSa sub query
- the query that we are doing inside another query what does a RIGHT OUTER JOIN do?
- do we support this? how do we implement it - ANS- it makes sure to keep everything from the RIGHT table and then JOINS with the left table
- we don't support it
- implement it by switching the order of the tables FROM student_grades LEFT OUTER JOIN students what does SQL stand for? - ANSstructured query language what does the IN operator check? - ANSthe IN operator checks to see if a particular value is in a list of values
what does the LIKE operator allow us to do? - ANS- if you don't use it, your query will try and find an exact match
- if you forget a period in a reason, then no results will show up what is a database? - ANSprogram that helps store information and provides functionality for adding, modifying, and querying that data what is a key thing that you need in databases? - ANSa unique identifier for each row bc we need a way to identify rows later when we are updating or deleting entries what is a relational database? - ANSa database that stores each kind of data in a table (similar to spreadsheet)
- a row represents an item
- and a column represents a property about that item what is an aggregate function used for getting? - ANSthe maximum, the minimum, sum, average of values in database what is the HAVING clause and what is it used for? - ANS- used when you want to display activities where calories > 150 for not just a single session of exercise, but all of the times that you've done that exercise
- use after GROUP BY GROUP BY type HAVING total_calories > 150);
when you use an aggregate like SUM(calories), when the results appear, what is the column name holding the results?
- how do you give that column a new name? - ANSSUM(calories) SELECT type, SUM(calories) AS total_calories FROM exercise_logs GROUP BY type; when you use HAVING, you are implying .... - ANSthe conditions to the grouped values, not the individual values in the individual rows which operator has precedence over the other? - ANSthe AND operator has precedence over the OR operator