OPTIONAL Currently, there’s been quite a bit of discussion a…
OPTIONAL Currently, there’s been quite a bit of discussion about the causes of autism. There is some evidence of an association between taking Tylenol during pregnancy, and the later development of autism in a child. First, tell me what type of correlation this is and why. Next, tell me why arriving at the conclusion that Tylenol causes autism is questionable at best.
Read DetailsA movie rental company has a rentals table with columns movi…
A movie rental company has a rentals table with columns movie_id, genre, and rental_fee. The finance team wants to find genres that generated more than $50,000 in total rental fees. Which query should they use? SELECT genre, SUM(rental_fee) FROM rentals GROUP BY genre HAVING SUM(rental_fee) > 50000 SELECT genre, SUM(rental_fee) FROM rentals WHERE SUM(rental_fee) > 50000 GROUP BY genre SELECT genre, rental_fee FROM rentals GROUP BY genre WHERE rental_fee > 50000 SELECT genre, SUM(rental_fee) FROM rentals ORDER BY SUM(rental_fee) > 50000 Answer: SELECT genre, SUM(rental_fee) FROM rentals GROUP BY genre HAVING SUM(rental_fee) > 50000 Explanation: The HAVING clause is required to filter aggregated totals. WHERE can’t filter on SUM, and ordering doesn’t filter results.
Read DetailsA hospital has a patients table with columns city and patien…
A hospital has a patients table with columns city and patient_id. Administrators want to list cities with more than 100 patients. Which SQL should they use? SELECT city, COUNT(patient_id) FROM patients GROUP BY city HAVING COUNT(patient_id) > 100 SELECT city, COUNT(patient_id) FROM patients WHERE COUNT(patient_id) > 100 GROUP BY city SELECT city, patient_id FROM patients GROUP BY city HAVING patient_id > 100 SELECT city, COUNT(patient_id) FROM patients GROUP BY city WHERE COUNT(patient_id) > 100 Answer: SELECT city, COUNT(patient_id) FROM patients GROUP BY city HAVING COUNT(patient_id) > 100 Explanation: Aggregates like COUNT can only be filtered using HAVING. Using WHERE COUNT(…) is invalid. Filtering on patient_id > 100 incorrectly checks row-level values instead of group totals.
Read DetailsA telecom company has a calls table with columns region and…
A telecom company has a calls table with columns region and call_duration. Analysts want to find regions where the average call duration is less than 3 minutes. Which query is correct? SELECT region, AVG(call_duration) FROM calls GROUP BY region HAVING AVG(call_duration) < 3 SELECT region, AVG(call_duration) FROM calls WHERE AVG(call_duration) < 3 GROUP BY region SELECT region, call_duration FROM calls GROUP BY region HAVING call_duration < 3 SELECT region, SUM(call_duration) FROM calls GROUP BY region HAVING SUM(call_duration) < 3 Answer: SELECT region, AVG(call_duration) FROM calls GROUP BY region HAVING AVG(call_duration) < 3 Explanation: HAVING is the correct clause for filtering by an aggregate. AVG(call_duration) gives the mean per region. Using WHERE with an aggregate is invalid. Checking SUM(call_duration) < 3 is meaningless for durations, and filtering on row-level call durations is not correct.
Read DetailsA bank keeps transaction data in transactions and account da…
A bank keeps transaction data in transactions and account data in accounts. They run: SELECT accounts.account_id, transactions.amount FROM accounts FULL OUTER JOIN transactions ON accounts.account_id = transactions.account_id; Which statement best describes the result? Only accounts that have transactions Only transactions linked to valid accounts All accounts and all transactions, matching where possible Accounts without balances are excluded Answer: All accounts and all transactions, matching where possible Explanation: A FULL OUTER JOIN includes everything from both sides, inserting NULLs where no match exists.
Read DetailsA retail company’s database has a products table with a colu…
A retail company’s database has a products table with a column called price. The CEO asks: What’s the highest price of any product we sell? Which SQL query answers that? SELECT SUM(price) FROM products SELECT MAX(price) FROM products SELECT AVG(price) FROM products SELECT COUNT(price) FROM products Answer: SELECT MAX(price) FROM products Explanation: MAX retrieves the largest single value from the column. SUM adds all prices together, which doesn’t make sense here. AVG provides an average, and COUNT only tells how many product prices are listed.
Read Details