181. Week-16 Quiz Solution
181. Week-16 Quiz Solution
Answer: a
Explanation:
In the context of MySQL Window Functions, a "Window Frame" refers to
a range of rows that define the subset of data to be used in a calculation. This
is the correct definition of a Window Frame. It determines which rows are
included in the calculation.
2. Consider a table called "sales" with columns "id", "date", "amount", and "region".
Which of the following SQL queries will calculate the average amount of sales for
each region, including the previous and next rows in the result set?
a. SELECT region, AVG(amount) OVER (ORDER BY region ROWS
BETWEEN 1 PRECEDING AND 1 FOLLOWING) as avg_sales FROM
sales;
b. SELECT region, AVG(amount) OVER (PARTITION BY region ORDER BY
date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as avg_sales
FROM sales;
c. SELECT region, AVG(amount) OVER (ORDER BY date ROWS BETWEEN
1 PRECEDING AND 1 FOLLOWING) as avg_sales FROM sales;
d. SELECT region, AVG(amount) OVER (PARTITION BY region ORDER BY
region ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as
avg_sales FROM sales;
Answer : b
Explanation: The correct query calculates the average amount of sales for
each region(PARTITION on region), including the previous and next rows. It uses the
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING clause. This clause considers
the rows immediately before and after the current row when calculating the average.
This allows you to perform calculations on a window of data around each row.
3. Consider a table called "employees" with columns "id", "name", "department", and
"salary". Which of the following SQL queries will calculate the running total of salary
for each employee, ordered by department?
a. SELECT name, salary, SUM(salary) OVER (PARTITION BY department
ORDER BY salary) as running_total FROM employees;
b. SELECT name, salary, SUM(salary) OVER (PARTITION BY department
ORDER BY name) as running_total FROM employees;
c. SELECT name, salary, SUM(salary) OVER (ORDER BY department,
name) as running_total FROM employees;
d. SELECT name, salary, SUM(salary) OVER (PARTITION BY name ORDER
BY department) as running_total FROM employees;
Answer: c
Explanation: This query calculates the running total of salary for each employee,
ordered by department. PARTITION is not needed as the question is not asked for
running salary sum department-wise.
4. Consider a table called "products" with columns "id", "name", "category", and
"price". Which of the following SQL queries will calculate the percentage of the total
revenue generated by each category of products?
a. SELECT category, price, SUM(price) OVER (PARTITION BY category
ORDER BY price) / SUM(SUM(price)) OVER () * 100 as
percent_total_revenue FROM products;
b. SELECT category, SUM(price) OVER (PARTITION BY category) as
revenue, SUM(price) / SUM(SUM(price)) OVER () * 100 as
percent_total_revenue FROM products;
c. SELECT category, SUM(price) as revenue, SUM(price) /
SUM(SUM(price)) OVER () * 100 as percent_total_revenue FROM
products GROUP BY category;
d. SELECT category, SUM(price) OVER (PARTITION BY category) as
revenue, SUM(SUM(price)) OVER () / SUM(price) * 100 as
percent_total_revenue FROM products;
Answer: c
Answer: b
12. Consider a table called "orders" with columns "order_id", "order_date", and
"order_total". Which of the following SQL queries will rank orders by their order total
within each month in descending order?
a. SELECT order_id, order_date, order_total, RANK() OVER (PARTITION BY
MONTH(order_date) ORDER BY order_total DESC) as order_rank FROM
orders;
b. SELECT order_id, order_date, order_total, RANK() OVER (ORDER BY
order_total DESC) as order_rank FROM orders;
c. SELECT order_id, order_date, order_total, RANK() OVER (PARTITION BY
MONTH(order_date) ORDER BY order_total ASC) as order_rank FROM
orders;
d. SELECT order_id, order_date, order_total, RANK() OVER (PARTITION BY
MONTH(order_date) ORDER BY order_total) as order_rank FROM
orders;
Answer: a
13. Consider a table called "customer_orders" with columns "customer_id",
"order_date", and "order_total". Which of the following SQL queries will calculate the
percent of each customer's total orders that are over $1000?
Select all queries which will give the correct Result.
14. Consider a table called "website_traffic" with columns "page_url", "visit_date", and
"visitors". Which of the following SQL queries will calculate the percent change in
visitors to each page from the previous day?
a. SELECT page_url, visit_date, visitors, visitors - LAG(visitors) OVER
(PARTITION BY page_url ORDER BY visit_date) / LAG(visitors) OVER
(PARTITION BY page_url ORDER BY visit_date) * 100 as
visitors_percent_change FROM website_traffic;
b. SELECT page_url, visit_date, visitors, (visitors - LAG(visitors) OVER
(PARTITION BY page_url ORDER BY visit_date)) / visitors * 100 as
visitors_percent_change FROM website_traffic;
c. SELECT page_url, visit_date, visitors, (visitors - LAG(visitors) OVER
(ORDER BY visit_date)) / visitors * 100 as visitors_percent_change
FROM website_traffic;
d. SELECT page_url, visit_date, visitors, (visitors - LAG(visitors) OVER
(ORDER BY visit_date)) / LAG(visitors) OVER (ORDER BY visit_date) *
100 as visitors_percent_change FROM website_traffic;
Answer : d