"Here is my implementation: select marketing_channel, AVG(purchasevalue) as avgpurchase_value from attribution group by marketing_channel order by avgpurchasevalue DESC ; There is no need to copy and past the line of code for calculating the average into order by, just Alias is enough because going by the order of execution in sql, Always, order by is executed after executing select clause."
Maliki U. - "Here is my implementation: select marketing_channel, AVG(purchasevalue) as avgpurchase_value from attribution group by marketing_channel order by avgpurchasevalue DESC ; There is no need to copy and past the line of code for calculating the average into order by, just Alias is enough because going by the order of execution in sql, Always, order by is executed after executing select clause."See full answer
"-- The text of the task is a bit confusing. If the status is repeated several -- times, then in the end you should show as start_date the date of the first -- occurrence, and in end_date the date of the last occurrence of this status, -- and not the date of the beginning of the next status with t1 as (select order_id, status, orderdate as startdate, lead(orderdate) over (partition by orderid order by orderdate) as enddate, ifnull(lag(status) over (partition by order_id order by or"
Alexey T. - "-- The text of the task is a bit confusing. If the status is repeated several -- times, then in the end you should show as start_date the date of the first -- occurrence, and in end_date the date of the last occurrence of this status, -- and not the date of the beginning of the next status with t1 as (select order_id, status, orderdate as startdate, lead(orderdate) over (partition by orderid order by orderdate) as enddate, ifnull(lag(status) over (partition by order_id order by or"See full answer
"select customer_id, order_date, orderid as earliestorder_id from ( select customer_id, order_date, order_id, rownumber() over (partition by customerid, orderdate order by orderdate) as orderrankper_customer from orders ) sub_table where orderrankper_customer=1 order by orderdate, customerid; Standard solution assumed that the orderid indicates which order comes in first. However this is not always the case, and sometime orderid can be random number withou"
Jessica C. - "select customer_id, order_date, orderid as earliestorder_id from ( select customer_id, order_date, order_id, rownumber() over (partition by customerid, orderdate order by orderdate) as orderrankper_customer from orders ) sub_table where orderrankper_customer=1 order by orderdate, customerid; Standard solution assumed that the orderid indicates which order comes in first. However this is not always the case, and sometime orderid can be random number withou"See full answer
"-- LTV = Sum of all purchases made by that user -- order the results by desc on LTV select u.user_id, sum(a.purchase_value) as LTV from user_sessions u join attribution a on u.sessionid = a.sessionid group by u.user_id order by sum(a.purchase_value) desc"
Mohit C. - "-- LTV = Sum of all purchases made by that user -- order the results by desc on LTV select u.user_id, sum(a.purchase_value) as LTV from user_sessions u join attribution a on u.sessionid = a.sessionid group by u.user_id order by sum(a.purchase_value) desc"See full answer
"SELECT order_amount FROM (SELECT o.order_amount, DENSERANK() OVER (ORDER BY o.orderamount DESC) as rnk FROM orders o JOIN departments d ON d.departmentid = o.departmentid WHERE d.department_name = 'Fashion' ) WHERE rnk = 2"
Ankit P. - "SELECT order_amount FROM (SELECT o.order_amount, DENSERANK() OVER (ORDER BY o.orderamount DESC) as rnk FROM orders o JOIN departments d ON d.departmentid = o.departmentid WHERE d.department_name = 'Fashion' ) WHERE rnk = 2"See full answer
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"SQL databases are relational, NoSQL databases are non-relational. SQL databases use structured query language and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data. SQL databases are vertically scalable, while NoSQL databases are horizontally scalable."
Ali H. - "SQL databases are relational, NoSQL databases are non-relational. SQL databases use structured query language and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data. SQL databases are vertically scalable, while NoSQL databases are horizontally scalable."See full answer
"with cte as ( select a.departmentid departmentid , b.departmentname departmentname , extract(month from a.order_date) mm , sum(a.orderamount) currentval , lag(sum(a.order_amount)) over(partition by a.departmentid order by extract(month from a.orderdate)) previous_val from orders a inner join departments b on a.departmentid=b.departmentid where extract(year from a.order_date) = 2022 and extract(month from a.order_date) in (11,12) group by 1,2,3 ) select departmentid, departmentname, c"
Anonymous Wombat - "with cte as ( select a.departmentid departmentid , b.departmentname departmentname , extract(month from a.order_date) mm , sum(a.orderamount) currentval , lag(sum(a.order_amount)) over(partition by a.departmentid order by extract(month from a.orderdate)) previous_val from orders a inner join departments b on a.departmentid=b.departmentid where extract(year from a.order_date) = 2022 and extract(month from a.order_date) in (11,12) group by 1,2,3 ) select departmentid, departmentname, c"See full answer
"-- Write your query here select user_id, name, COUNT(t.id) AS orders FROM transactions t INNER JOIN users u ON t.user_id = u.id GROUP BY user_id ORDER BY orders desc LIMIT 1 ;"
Aneesha K. - "-- Write your query here select user_id, name, COUNT(t.id) AS orders FROM transactions t INNER JOIN users u ON t.user_id = u.id GROUP BY user_id ORDER BY orders desc LIMIT 1 ;"See full answer
"with t1 as (select employee_name, department_id, salary, avg(salary) over (partition by departmentid) as avgsalary, abs(salary - avg(salary) over (partition by department_id)) as diff from employees ) select employee_name, department_id, salary, avg_salary, denserank() over (partition by departmentid order by diff desc) as deviation_rank from t1 order by departmentid asc, deviationrank asc, employee_name `"
Alexey T. - "with t1 as (select employee_name, department_id, salary, avg(salary) over (partition by departmentid) as avgsalary, abs(salary - avg(salary) over (partition by department_id)) as diff from employees ) select employee_name, department_id, salary, avg_salary, denserank() over (partition by departmentid order by diff desc) as deviation_rank from t1 order by departmentid asc, deviationrank asc, employee_name `"See full answer
"-- Write your query here select avg(julianday(dateend) - julianday(datestart)) as average_duration from campaign; `"
Anonymous Roadrunner - "-- Write your query here select avg(julianday(dateend) - julianday(datestart)) as average_duration from campaign; `"See full answer
"SELECT a.marketing_channel, AVG(a.purchasevalue) AS avgpurchase_value, SUM(CASE WHEN a.purchasevalue > 0 THEN 1 ELSE 0 END) * 100 / COUNT(a.sessionid) AS conversion_rate FROM attribution a LEFT JOIN user_sessions u ON a.sessionid = u.sessionid GROUP BY a.marketing_channel ORDER BY conversion_rate DESC; "
Soma R. - "SELECT a.marketing_channel, AVG(a.purchasevalue) AS avgpurchase_value, SUM(CASE WHEN a.purchasevalue > 0 THEN 1 ELSE 0 END) * 100 / COUNT(a.sessionid) AS conversion_rate FROM attribution a LEFT JOIN user_sessions u ON a.sessionid = u.sessionid GROUP BY a.marketing_channel ORDER BY conversion_rate DESC; "See full answer
"with cte as ( select user_id, timestamp as current_login, lag(timestamp,1) over(partition by userid order by timestamp) as previouslogin , round(abs(julianday(timestamp)-julianday(lag(timestamp,1) over(partition by userid order by timestamp)))2460)as minuteselapsed from useractivitylog where activity_type ='LOGIN' ) select userid, currentlogin, previouslogin, minuteselapsed from cte where currentlogin previouslogin `"
Gowtami K. - "with cte as ( select user_id, timestamp as current_login, lag(timestamp,1) over(partition by userid order by timestamp) as previouslogin , round(abs(julianday(timestamp)-julianday(lag(timestamp,1) over(partition by userid order by timestamp)))2460)as minuteselapsed from useractivitylog where activity_type ='LOGIN' ) select userid, currentlogin, previouslogin, minuteselapsed from cte where currentlogin previouslogin `"See full answer
"SELECT i.item_category, o.order_date, SUM(o.orderquantity) AS totalunits_ordered FROM orders o JOIN items i ON o.itemid = i.itemid WHERE o.order_date >= DATE('now', '-6 days') GROUP BY i.item_category, o.order_date ORDER BY i.item_category ASC, o.order_date ASC;"
Anonymous Tortoise - "SELECT i.item_category, o.order_date, SUM(o.orderquantity) AS totalunits_ordered FROM orders o JOIN items i ON o.itemid = i.itemid WHERE o.order_date >= DATE('now', '-6 days') GROUP BY i.item_category, o.order_date ORDER BY i.item_category ASC, o.order_date ASC;"See full answer
"WITH previous AS(SELECT viewer_id, watch_hours, LAG(watchhours) OVER(PARTITION BY viewerid ORDER BY year, month) AS previous_hours, year, month FROM watch_time GROUP BY viewer_id, year, month ), streaks AS(SELECT viewer_id, SUM(CASE WHEN previoushours IS NOT NULL AND previoushours = 3 `"
Alvin P. - "WITH previous AS(SELECT viewer_id, watch_hours, LAG(watchhours) OVER(PARTITION BY viewerid ORDER BY year, month) AS previous_hours, year, month FROM watch_time GROUP BY viewer_id, year, month ), streaks AS(SELECT viewer_id, SUM(CASE WHEN previoushours IS NOT NULL AND previoushours = 3 `"See full answer
"Test case is wrong. It expects to sort in asc order of month_year. -- Write your query here SELECT strftime('%Y-%m', createdat) AS monthyear, COUNT(DISTINCT userid) AS numcustomers, COUNT(t.id) AS num_orders, SUM(price * quantity) AS order_amt FROM transactions t INNER JOIN products p ON t.product_id = p.id GROUP BY month_year ORDER BY month_year ; "
Aneesha K. - "Test case is wrong. It expects to sort in asc order of month_year. -- Write your query here SELECT strftime('%Y-%m', createdat) AS monthyear, COUNT(DISTINCT userid) AS numcustomers, COUNT(t.id) AS num_orders, SUM(price * quantity) AS order_amt FROM transactions t INNER JOIN products p ON t.product_id = p.id GROUP BY month_year ORDER BY month_year ; "See full answer
"Clarification questions What is the purpose of connecting the DB? Do we expect high-volumes of traffic to hit the DB Do we have scalability or reliability concerns? Format Code -> DB Code -> Cache -> DB API -> Cache -> DB - APIs are built for a purpose and have a specified protocol (GET, POST, DELETE) to speak to the DB. APIs can also use a contract to retrieve information from a DB much faster than code. Load balanced APIs -> Cache -> DB **Aut"
Aaron W. - "Clarification questions What is the purpose of connecting the DB? Do we expect high-volumes of traffic to hit the DB Do we have scalability or reliability concerns? Format Code -> DB Code -> Cache -> DB API -> Cache -> DB - APIs are built for a purpose and have a specified protocol (GET, POST, DELETE) to speak to the DB. APIs can also use a contract to retrieve information from a DB much faster than code. Load balanced APIs -> Cache -> DB **Aut"See full answer
"SELECT d.department_name, SUM(o.orderamount) AS totalrevenue FROM orders o JOIN departments d ON o.departmentid = d.departmentid WHERE o.order_date >= DATE('now', '-12 months') GROUP BY d.department_name ORDER BY total_revenue DESC; "
Jayveer S. - "SELECT d.department_name, SUM(o.orderamount) AS totalrevenue FROM orders o JOIN departments d ON o.departmentid = d.departmentid WHERE o.order_date >= DATE('now', '-12 months') GROUP BY d.department_name ORDER BY total_revenue DESC; "See full answer
"SELECT upsellcampaignid, COUNT(DISTINCT trans.userid) AS eligibleusers FROM campaign JOIN "transaction" AS trans ON transactiondate BETWEEN datestart AND date_end JOIN user ON trans.userid = user.userid WHERE iseligibleforupsellcampaign = 1 GROUP BY upsellcampaignid `"
Alina G. - "SELECT upsellcampaignid, COUNT(DISTINCT trans.userid) AS eligibleusers FROM campaign JOIN "transaction" AS trans ON transactiondate BETWEEN datestart AND date_end JOIN user ON trans.userid = user.userid WHERE iseligibleforupsellcampaign = 1 GROUP BY upsellcampaignid `"See full answer
"WITH suspicious_transactions AS ( SELECT c.first_name, c.last_name, t.receipt_number, COUNT(t.receiptnumber) OVER (PARTITION BY c.customerid) AS noofoffences FROM customers c JOIN transactions t ON c.customerid = t.customerid WHERE t.receipt_number LIKE '%999%' OR t.receipt_number LIKE '%1234%' OR t.receipt_number LIKE '%XYZ%' ) SELECT first_name, last_name, receipt_number, noofoffences FROM suspicious_transactions WHERE noofoffences >= 2;"
Jayveer S. - "WITH suspicious_transactions AS ( SELECT c.first_name, c.last_name, t.receipt_number, COUNT(t.receiptnumber) OVER (PARTITION BY c.customerid) AS noofoffences FROM customers c JOIN transactions t ON c.customerid = t.customerid WHERE t.receipt_number LIKE '%999%' OR t.receipt_number LIKE '%1234%' OR t.receipt_number LIKE '%XYZ%' ) SELECT first_name, last_name, receipt_number, noofoffences FROM suspicious_transactions WHERE noofoffences >= 2;"See full answer
"too many questions for clarification on this to start"
Steven S. - "too many questions for clarification on this to start"See full answer
Interviewed recently?
Help improve our question database (and earn karma) by telling us about your experience
+ Add your interview