Given A Table
Given A Table
Given a table "UserActivity" with columns "user_id," "action," and "timestamp," write an SQL
query to find the number of unique users who performed each action on a specific date.
Example:
UserActivity:
Result:
view 2023-07-25 3
add_cart 2023-07-25 1
purchase 2023-07-25 1
select
action,
date(tsmp) as date,
count(user_id) as total_users
from `Practice.q1`
group by action, date
2. You have a table "Sales" with columns "user_id," "product_id," "timestamp," and
"purchase_status." Write an SQL query to calculate the conversion rate of users from each
stage of the sales funnel (e.g., viewed the product, added to cart, completed the purchase).
Example:
viewed 2 0 0 2
Added_to_cart 0 1 0 1
completed 1 0 2 3
SELECT
purchase_status,
SUM(CASE WHEN purchase_status = 'viewed' THEN 1 ELSE 0 END) AS viewed_users,
SUM(CASE WHEN purchase_status = 'added_to_cart' THEN 1 ELSE 0 END) AS added_to_cart_users,
SUM(CASE WHEN purchase_status = 'completed' THEN 1 ELSE 0 END) AS completed_users,
COUNT(*) AS total_users
FROM Practice.q2
GROUP BY purchase_status
ORDER BY 4
3. You have two tables: "ControlGroup" with columns "user_id" and "conversion_status," and
"ExperimentalGroup" with the same columns. Write an SQL query to calculate the
conversion rate for both groups and determine if the experimental group has a statistically
significant difference in conversion compared to the control group.
Example:
Control group:
user_id conversion_status
1 converted
2 not_converted
3 converted
4 not_converted
5 not_converted
Experimental group:
user_id conversion_status
6 not_converted
7 converted
8 converted
9 not_converted
10 converted
Result:
ControlGroup 2 5 0.4
ExperimentalGroup 3 5 0.6
Given a table "Numbers" with a single column "number," write a query to find the missing numbers
in the sequence from 1 to 100 (excluding the ones present in the table).
Example:
Numbers:
number
...
98
100
Result:
number
...
99
1 25 Male 100.00
2 30 Female 50.00
3 22 Male 75.00
4 28 Female 120.00
5 35 Male 80.00
Result:
Write an SQL query to find the Nth highest salary from a "Salary" table.
Example:
Salary
50000
75000
60000
90000
80000
Result: The 3rd highest salary is 60000.
Write an SQL query to rank "Scores" in the "Students" table. If two students have the same score,
they should receive the same rank, and the next rank should be skipped.
Example:
StudentScores
Alice 85
Bob 92
Charlie 85
David 78
Eve 92
Result:
StudentScores rank
Bob 92 1
Eve 92 1
Alice 85 2
Charlie 85 2
David 78 3
Write an SQL query to calculate the cumulative sum of "Revenue" for each "Category," resetting the
sum when encountering a new category.
Example:
Sales:
Category Revenue
A 100
A 150
B 200
A 50
B 300
B 250
Result:
A 50 50
A 100 150
A 150 300
B 200 200
B 250 450
B 300 750
Given a table "Transaction" with columns "transaction_id," "product_id," and "timestamp," write an
SQL query to find the top 5 most frequently co-occurring product pairs in transactions.
Example
Transactions:
1 A 2023-07-25 08:15:00
2 B 2023-07-25 09:30:00
3 A 2023-07-25 10:00:00
4 C 2023-07-25 12:45:00
5 A 2023-07-25 14:20:00
Result:
A B 3
A C 2
B C 1
D E 1
A D 1
Using a table "Locations" with columns "location_id," "latitude," and "longitude," write an SQL query
to find the closest locations to a given set of latitude and longitude coordinates.
Example:
Locations:
1 40.7128 -74.0060
2 34.0522 -118.2437
3 41.8781 -87.6298
4 37.7749 -122.4194
5 29.7604 -95.3698
latitude_target longitude_target
38.9072 -77.0369
Result: