DataLemur SQL Problems- Part 5
Hi! I am Ilona! This is Part5 of my DataLemur coding practice journey, I’m doing this to sharpen my SQL skills and share what I learn along the way!
If you’re new here, feel free to check out Parts 1–4 below.
Q13.
Given a table of Facebook posts, for each user who posted at least twice in 2021, write a query to find the number of days between each user’s first post of the year and last post of the year in the year 2021. Output the user and number of the days between each user’s first and last post.
Solution:
SELECT user_id, MAX(post_date::DATE) - MIN(post_date::DATE) AS days_between
FROM posts
WHERE EXTRACT(YEAR FROM post_date) = 2021
GROUP BY user_id
HAVING COUNT(post_id) >= 2;Solution Explanation:
SELECT user_id, MAX(post_date::DATE) - MIN(post_date::DATE) AS days_between
FROM postsMAX(post_date::DATE): This finds the most recent post date for each user.
MIN(post_date::DATE): This finds the earliest post date for each user.
We can calculate the time span by subtracting the earliest from the latest date.
WHERE EXTRACT(YEAR FROM post_date) = 2021Filters the data so we only look at posts made in the year 2021.
GROUP BY user_id
HAVING COUNT(post_id) >= 2;Group the results by user_id and select the ones who made at least 2 posts in 2021.
Q14.
Given a table containing information about bank deposits and withdrawals made using Paypal, write a query to retrieve the final account balance for each account, taking into account all the transactions recorded in the table with the assumption that there are no missing transactions.
Solution:
SELECT account_id,
SUM (CASE
WHEN transaction_type = 'Deposit' THEN amount
Else -amount END) AS final_balance
FROM transactions
GROUP BY account_idSolution Explanation:
SELECT account_id... FROM transactionsThis one is simple, we need to select account_id from the transactions table.
SUM (CASE
WHEN transaction_type = 'Deposit' THEN amount
Else -amount END) AS final_balanceWe also need to compute the finals account balance.
Get Yu-Chen (Ilona) Tung’s stories in your inbox
Join Medium for free to get updates from this writer.
Sum: Adds up all values returned by the CASE for each account.
CASE WHEN transaction_type = ‘Deposit’ THEN amount: If the transaction is a deposit, we add the amount to the balance.
Else -emount: For Withdrawl, we substract the amount by turning it into a negative value.
AS final_balance: Change the column name to match the output.
GROUP BY account_idLast but not least, we groups all transactions by account. This ensures the SUM() is calculated per account.
Q15.
You’re trying to find the mean number of items per order on Alibaba, rounded to 1 decimal place using tables which includes information on the count of items in each order (item_count table) and the corresponding number of orders for each item count (order_occurrences table).
Solution:
SELECT ROUND(
SUM(item_count * order_occurrences)::DECIMAL
/ SUM(order_occurrences),
1
) AS mean
FROM items_per_order;Solution Explanation:
SELECT ROUND(...,1) AS mean
FROM items_per_order;Round the final result to 1 decimal place and name the column mean.
SUM(item_count * order_occurrences)::DECIMALThis computes the weighted total (numerator).
::DECIMAL casts that integer sum to a decimal type so division will be done in decimal (not integer).
/ SUM(order_occurrences)Divide the decimal numerator by the total number of orders (denominator).
At first, my query was
SELECT ROUND (SUM(item_count * order_occurrences) / SUM(order_occurrences), 1)
AS mean
FROM items_per_order;However, this fails because if item_count and order_occurrences are stored as integer types, the division
SUM(item_count * order_occurrences) / SUM(order_occurrences) will perform integer division, soROUND() won’t fix anything because it gets a whole number.
If you’re learning SQL too, I’d love for you to join me on this journey — we can keep improving together!









