595. Big Countries
Write a solution to find the name, population, and area of the big countries.
A country is big if:
it has an area of at least three million (i.e., 3000000 km2), or
it has a population of at least twenty-five million (i.e., 25000000).
Input: World table
name
continent
area
population
gdp
Afghanistan
Asia
652230
25500100
20343000000
Albania
Europe
28748
2831741
12960000000
Algeria
Africa
2381741
37100000
188681000000
Andorra
Europe
468
78115
3712000000
Angola
Africa
1246700
20609294
100990000000
Output
name
population
area
Afghanistan
25500100
652230
Algeria
37100000
2381741
SELECT name, population, area FROM worldWHERE area >= 3000000 OR population >= 25000000;
record WorldInfo(String name, long population, long area) {}world.stream() .filter(w -> w.area >= 3000000 || w.population >= 25000000) .map(w -> new WorldInfo(w.name, w.population, w.area)) .collect(Collectors.toList());
1148. Article Views I
Write a solution to find all the authors that viewed at least one of their own articles. Return the result table sorted by id in ascending order.
Input: Views table
article_id
author_id
viewer_id
view_date
1
3
5
2019-08-01
1
3
6
2019-08-02
2
7
7
2019-08-01
2
7
6
2019-08-02
4
7
1
2019-07-22
3
4
4
2019-07-21
3
4
4
2019-07-21
Output
id
4
7
SELECT author_id as id FROM viewsWHERE author_id = viewer_idGROUP BY author_idORDER BY author_idSELECT distinct author_id as id FROM viewsWHERE aauthor_id = viewer_idORDER BY author_id
1683. Invalid Tweets
Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.
Input: Tweets table
tweet_id
content
1
Let us Code
2
More than fifteen chars are here!
Output
tweet_id
2
SELECT tweet_id FROM tweetsWHERE LENGTH(content) > 15;
SELECT customer_id, COUNT(*) AS count_no_trans FROM visits vWHERE NOT EXISTS( SELECT t.visit_id FROM transactions t WHERE t.visit_id = v.visit_id)GROUP BY customer_id;SELECT customer_id, count(*) AS count_no_trans FROM visits vLEFT JOIN transactions t ON v.visit_id = t.visit_idWHERE transaction_id IS NULLGROUP BY customer_id;
197. Rising Temperature
Write a solution to find all dates’ id with higher temperatures compared to its previous dates (yesterday).
SELECT a1.machine_id, ROUND((SUM(a2.timestamp - a1.timestamp) / COUNT(*))::numeric, 3) AS processing_timeFROM activity a1JOIN activity a2 ON a1.activity_type = 'start' AND a2.activity_type = 'end'AND a1.machine_id = a2.machine_id AND a1.process_id = a2.process_idGROUP BY a1.machine_id;SELECT a1.machine_id, ROUND(AVG(a2.timestamp - a1.timestamp)::numeric, 3) AS processing_timeFROM activity a1JOIN activity a2 ON a1.activity_type = 'start' AND a2.activity_type = 'end'AND a1.machine_id = a2.machine_id AND a1.process_id = a2.process_idGROUP BY a1.machine_id;
577. Employee Bonus
Write a solution to report the name and bonus amount of each employee with a bonus less than 1000.
Input: Employee table
empId
name
supervisor
salary
3
Brad
null
4000
1
John
3
1000
2
Dan
3
2000
4
Thomas
3
4000
Input: Bonus table
empId
bonus
2
500
4
2000
Output
name
bonus
Brad
null
John
null
Dan
500
SELECT name, bonus FROM employee eLEFT JOIN bonus b ON e.empid = b.empidWHERE bonus IS NULL OR bonus < 1000;
1280. Students and Examinations
Write a solution to find the number of times each student attended each exam. Return the result table ordered by student_id and subject_name.
Input: Students table
student_id
student_name
1
Alice
2
Bob
13
John
6
Alex
Input: Examinations table
student_id
subject_name
1
Math
1
Physics
1
Programming
2
Programming
1
Physics
1
Math
13
Math
13
Programming
13
Physics
2
Math
1
Math
Input: Subjects table
subject_name
Math
Physics
Programming
Output
student_id
student_name
subject_name
attended_exams
1
Alice
Math
3
1
Alice
Physics
2
1
Alice
Programming
1
2
Bob
Math
1
2
Bob
Physics
0
2
Bob
Programming
1
6
Alex
Math
0
6
Alex
Physics
0
6
Alex
Programming
0
13
John
Math
1
13
John
Physics
1
13
John
Programming
1
SELECT s.student_id, student_name, sub.subject_name, count(e) AS attended_exams FROM students s CROSS JOIN subjects sub LEFT JOIN examinations e ON s.student_id = e.student_id AND sub.subject_name = e.subject_nameGROUP BY s.student_id, student_name, sub.subject_nameORDER BY s.student_id, sub.subject_name;
SELECT m.name FROM Employee m JOIN Employee e ON m.id = e.managerIdGROUP BY m.id, m.name HAVING COUNT(1) >= 5;
1934. Confirmation Rate
Write a solution to find the confirmation rate of each user. Round to two decimal places. The confirmation rate is the number of ‘confirmed’ messages divided by the total number of requested confirmation messages.
Input: Signups table
user_id
time_stamp
3
2020-03-21 10:16:13
7
2020-01-04 13:57:59
2
2020-07-29 23:09:44
6
2020-12-09 10:39:37
Input: Confirmations table
user_id
time_stamp
action
3
2021-01-06 03:30:46
timeout
3
2021-07-14 14:00:00
timeout
7
2021-06-12 11:57:29
confirmed
7
2021-06-13 12:58:28
confirmed
7
2021-06-14 13:59:27
confirmed
2
2021-01-22 00:00:00
confirmed
2
2021-02-28 23:59:59
timeout
Output
user_id
confirmation_rate
6
0.00
3
0.00
7
1.00
2
0.50
SELECT s.user_id,ROUND(AVG(CASE WHEN c.action = 'confirmed' THEN 1 ELSE 0 END), 2) as confirmation_rateFROM signups sLEFT JOIN confirmations c on s.user_id = c.user_idGROUP BY s.user_id
Write a solution to report the movies with an odd-numbered ID and a description that is not “boring”. Return the result table ordered by rating in descending order.
Input: Cinema table
id
movie
description
rating
1
War
great 3D
8.9
2
Science
fiction
8.5
3
irish
boring
6.2
4
Ice song
Fantacy
8.6
5
House card
Interesting
9.1
Output
id
movie
description
rating
5
House card
Interesting
9.1
1
War
great 3D
8.9
SELECT * FROM cinemaWHERE description <> 'boring' AND id % 2 = 1ORDER BY id DESC;
Write a solution to find the average selling price for each product. Round to 2 decimal places. If a product does not have any sold units, its average price is 0.
Input: Prices table
product_id
start_date
end_date
price
1
2019-02-17
2019-02-28
5
1
2019-03-01
2019-03-22
20
2
2019-02-01
2019-02-20
15
2
2019-02-21
2019-03-31
30
Input: UnitsSold table
product_id
purchase_date
units
1
2019-02-25
100
1
2019-03-01
15
2
2019-02-10
200
2
2019-03-22
30
Output
product_id
average_price
1
6.96
2
16.96
SELECT p.product_id, COALESCE(ROUND(SUM(units * price)/SUM(units)::numeric, 2), 0) AS average_priceFROM prices pLEFT JOIN unitssold u ON p.product_id = u.product_idAND u.purchase_date BETWEEN p.start_date AND p.end_dateGROUP BY p.product_id;
Write a solution to report the average experience years of all the employees for each project, rounded to 2 digits.
Input: Project table
project_id
employee_id
1
1
1
2
1
3
2
1
2
4
Employee table
employee_id
name
experience_years
1
Khaled
3
2
Ali
2
3
John
1
4
Doe
2
Output
project_id
average_years
1
2.00
2
2.50
SELECT p.project_id, ROUND(AVG(e.experience_years), 2) AS average_years FROM Project p JOIN Employee e ON p.employee_id = e.employee_idGROUP BY p.project_id;
Write a solution to find the percentage of the users registered in each contest rounded to two decimals. Return the result table ordered by percentage in descending order. In case of a tie, order by contest_id in ascending order.
Input: Users table
user_id
user_name
6
Alice
2
Bob
7
Alex
Register table
contest_id
user_id
215
6
209
2
208
2
210
6
208
6
209
7
209
6
215
7
208
7
210
2
207
2
210
7
Output
contest_id
percentage
208
100.00
209
100.00
210
100.00
215
66.67
207
33.33
SELECT r.contest_id, ROUND(COUNT(DISTINCT r.user_id)::numeric * 100 / (SELECT COUNT(*) FROM Users), 2) AS percentageFROM Register r GROUP BY r.contest_idORDER BY percentage DESC, r.contest_id;
WITH total AS (SELECT count(1) AS c from users)SELECT contest_id,ROUND(count(distinct user_id)::numeric * 100 / total.c, 2) as percentageFROM registerCROSS JOIN totalGROUP BY contest_id, cORDER BY percentage desc, contest_id;
Write a solution to find each query_name, the quality and poor_query_percentage. Both quality and poor_query_percentage should be rounded to 2 decimal places. Quality is the average of the ratio between query rating and position. A query is poor if its rating is less than 3.
Input: Queries table
query_name
result
position
rating
Dog
Golden Retriever
1
5
Dog
German Shepherd
2
5
Dog
Mule
200
1
Cat
Shirazi
5
2
Cat
Siamese
3
3
Cat
Sphynx
7
4
Output
query_name
quality
poor_query_percentage
Dog
2.50
33.33
Cat
0.66
33.33
SELECT query_name, ROUND(AVG(rating::numeric / position), 2) AS quality,ROUND(AVG(CASE WHEN rating < 3 THEN 1 ELSE 0 END)::numeric * 100, 2) AS poor_query_percentageFROM queriesGROUP BY query_name;
Write a solution to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.
Input: Transactions table
id
country
state
amount
trans_date
121
US
approved
1000
2018-12-18
122
US
declined
2000
2018-12-19
123
US
approved
2000
2019-01-01
124
DE
approved
2000
2019-01-07
Output
month
country
trans_count
approved_count
trans_total_amount
approved_total_amount
2018-12
US
2
1
3000
1000
2019-01
US
1
1
2000
2000
2019-01
DE
1
1
2000
2000
SELECT TO_CHAR(trans_date, 'YYYY-MM') AS month, country, count(*) AS trans_count,SUM(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) AS approved_count,SUM(amount) AS trans_total_amount,SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) AS approved_total_amountFROM transactionsGROUP BY month, country;
Write a solution to find the percentage of immediate orders in the first orders of all customers. Round to 2 decimal places. An order is immediate if the order date equals the customer preferred delivery date.
Input: Delivery table
delivery_id
customer_id
order_date
customer_pref_delivery_date
1
1
2019-08-01
2019-08-02
2
2
2019-08-02
2019-08-02
3
1
2019-08-11
2019-08-12
4
3
2019-08-24
2019-08-24
5
3
2019-08-21
2019-08-22
6
2
2019-08-11
2019-08-13
7
4
2019-08-09
2019-08-09
Output
immediate_percentage
50.00
SELECT ROUND(AVG(CASE WHEN order_date = customer_pref_delivery_date THEN 1 ELSE 0 END) * 100, 2) AS immediate_percentageFROM deliveryWHERE (customer_id, order_date) IN ( SELECT customer_id, MIN(order_date) AS first_order FROM delivery GROUP BY customer_id)
WITH first_orders AS ( SELECT customer_id, MIN(order_date) AS first_order_date FROM Delivery GROUP BY customer_id ) SELECT ROUND(SUM(CASE WHEN d.order_date = d.customer_pref_delivery_date THEN 1 ELSE 0 END)::numeric * 100 / COUNT(*), 2) AS immediate_percentage FROM Delivery d JOIN first_orders f ON d.customer_id = f.customer_id AND d.order_date = f.first_order_date;
Write a solution to report the fraction of players that logged in again on the day after the day they first logged in, rounded to 2 decimal places.
Input: Activity table
player_id
device_id
event_date
games_played
1
2
2016-03-01
5
1
2
2016-03-02
6
2
3
2017-06-25
1
3
1
2016-03-02
0
3
4
2018-07-03
5
Output
fraction
0.33
WITH first_login AS ( SELECT player_id, MIN(event_date) AS first_login_date FROM activity GROUP BY player_id)SELECT ROUND(COUNT(a2.player_id)::NUMERIC / COUNT(a1.player_id), 2) AS fractionFROM first_login a1LEFT JOIN activity a2 ON a1.player_id = a2.player_idAND a1.first_login_date = a2.event_date - INTERVAL '1 day';
2356. Number of Unique Subjects Taught by Each Teacher
Write a solution to find the daily active user count for a period of 30 days ending 2019-07-27 inclusively. A user was active on some day if they made at least one activity on that day.
Input: Activity table
user_id
session_id
activity_date
activity_type
1
1
2019-07-20
open_session
1
1
2019-07-20
scroll_down
1
1
2019-07-20
end_session
2
4
2019-07-20
open_session
2
4
2019-07-21
send_message
2
4
2019-07-21
end_session
3
2
2019-07-21
open_session
3
2
2019-07-21
send_message
3
2
2019-07-21
end_session
4
3
2019-06-25
open_session
4
3
2019-06-25
end_session
Output
day
active_users
2019-07-20
2
2019-07-21
2
SELECT activity_date AS day, count(DISTINCT user_id) AS active_users FROM activityWHERE activity_date BETWEEN '2019-07-27'::date - INTERVAL '29 DAYS' AND '2019-07-27'GROUP BY activity_date;
Write a solution to select the product id, year, quantity, and price for the first year of every product sold.
Input: Sales table
sale_id
product_id
year
quantity
price
1
100
2008
10
5000
2
100
2009
12
5000
7
200
2011
15
9000
Product table
product_id
product_name
100
Nokia
200
Apple
300
Samsung
Output
product_id
first_year
quantity
price
100
2008
10
5000
200
2011
15
9000
WITH first_year AS ( SELECT product_id, MIN(year) as year FROM sales GROUP BY product_id)SELECT s.product_id, s.year AS first_year, s.quantity, s.price FROM sales sJOIN first_year f ON s.product_id = f.product_id AND s.year = f.year;
Write a solution to report the ids and names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer. Return the result table ordered by employee_id.
Input: Employees table
employee_id
name
reports_to
age
9
Hercy
null
43
6
Alice
9
41
4
Bob
9
36
2
Winston
null
37
Output
employee_id
name
reports_count
average_age
9
Hercy
2
39
SELECT e1.employee_id, e1.name, COUNT(*) AS reports_count, ROUND(AVG(e2.age)) AS average_ageFROM employees e1JOIN employees e2 ON e1.employee_id = e2.reports_toGROUP BY e1.employee_id, e1.nameORDER BY e1.employee_id;
Write a solution to report all the employees with their primary department. For employees who belong to one department, report their only department.
Input: Employee table
employee_id
department_id
primary_flag
1
1
N
2
1
Y
2
2
N
3
3
N
4
2
N
4
3
Y
4
4
N
Output
employee_id
department_id
1
1
2
1
3
3
4
3
SELECT employee_id, department_id FROM employeeWHERE primary_flag = 'Y'UNIONSELECT employee_id, department_id FROM employeeWHERE employee_id IN ( SELECT employee_id FROM employee GROUP BY employee_id HAVING COUNT(*) = 1);SELECT employee_id, department_id FROM employeeWHERE primary_flag = 'Y'OR employee_id IN ( SELECT employee_id FROM employee GROUP BY employee_id HAVING COUNT(*) = 1);
Write a solution to find the prices of all products on 2019-08-16. Assume the price of all products before any change is 10.
Input: Products table
product_id
new_price
change_date
1
20
2019-08-14
2
50
2019-08-14
1
30
2019-08-15
1
35
2019-08-16
2
65
2019-08-17
3
20
2019-08-18
Output
product_id
price
2
50
1
35
3
10
WITH latest_prices AS ( SELECT product_id, new_price, change_date, ROW_NUMBER() OVER ( PARTITION BY product_id ORDER BY change_date DESC ) AS rn FROM products WHERE change_date <= '2019-08-16') SELECT product_id, new_price AS price FROM latest_prices WHERE rn = 1UNIONSELECT DISTINCT product_id, 10 AS price FROM products WHERE product_id NOT IN ( SELECT product_id FROM products WHERE change_date <= '2019-08-16');
WITH unique_products AS ( SELECT product_id , MAX(change_date) AS date FROM products WHERE change_date <= '2019-08-16' GROUP BY product_id),unique_prices AS ( SELECT p.product_id, new_price AS price FROM products p JOIN unique_products u ON p.product_id = u.product_id AND p.change_date = u.date)SELECT DISTINCT p.product_id, COALESCE(price, 10) AS price FROM products pLEFT JOIN unique_prices u ON p.product_id = u.product_id;