Question
Question
Easy
1. Tesla is investigating production bottlenecks and they need your help to extract
the relevant data. Write a query to determine which parts have begun the assembly
process but are not yet finished.
Assumptions:
part VARCHAR(255),
finish_date DATETIME NULL,
assembly_step INT
);
Expected Output
part assembly_ste
p
bumper 3
bumper 4
engine 5
2. Assume you're given the table on user viewership categorised by device type
where the three types are laptop, tablet, and phone.
Write a query that calculates the total viewership for laptops and mobile devices
where mobile is defined as the sum of tablet and phone viewership. Output the total
viewership for laptops as laptop_reviews and the total viewership for mobile devices
as mobile_views.
Code:
laptop_views mobile_views
2 3
3. Assume you're given a table containing job postings from various companies on
the LinkedIn platform. Write a query to retrieve the count of companies that have
posted duplicate job listings.
Definition:
● Duplicate job listings are defined as two job listings within the same company
that share identical titles and descriptions.
Code:
CREATE TABLE jobs (
job_id INT PRIMARY KEY, -- Ensures job_id is unique and non-null
company_id INT,
title VARCHAR(255) NOT NULL,
description TEXT
);
Expected output:
4. Given the reviews table, write a query to retrieve the average star rating for each
product, grouped by month. The output should display the month as a numerical
value, product ID, and average star rating rounded to two decimal places. Sort the
output first by month and then by product ID.
Code:
CREATE TABLE reviews (
review_id INT,
user_id INT,
submit_date DATETIME,
product_id INT,
stars INT
);
Expected output:
8 50001 4.00
9 69852 2.00
10 50001 3.00
10 69852 3.50
4. Assume you're given the tables containing completed trade orders and user
details in a Robinhood trading system.
Write a query to retrieve the top three cities that have the highest number of
completed trade orders listed in descending order. Output the city name and the
corresponding number of completed trade orders.
Code:
INSERT INTO traders (order_id, user_id, quantity, status, order_date, price) VALUES
(100101, 111, 10, 'Cancelled', '2022-08-17 12:00:00', 9.80),
(100102, 111, 10, 'Completed', '2022-08-17 12:00:00', 10.00),
(100259, 148, 35, 'Completed', '2022-08-25 12:00:00', 5.10),
(100264, 148, 40, 'Completed', '2022-08-26 12:00:00', 4.80),
(100305, 300, 15, 'Completed', '2022-09-05 12:00:00', 10.00),
(100400, 178, 32, 'Completed', '2022-09-17 12:00:00', 12.00),
(100565, 265, 2, 'Completed', '2022-09-27 12:00:00', 8.70);
Expected Output:
city order_id
Boston 100900
Boston 100259
Boston 100264
Denver 100565
MEDIUM
1. Write an SQL query to find for each month and country, the number of
transactions and their total amount, the number of approved transactions and their
total amount. Return the result table in any order.
Code:
CREATE TABLE transactions (
id INT,
country VARCHAR(50),
state VARCHAR(50),
amount DECIMAL(10,2),
trans_date DATE
);
Expected output:
Code:
CREATE TABLE delivery (
Expected Output:
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
3. A company's executives are interested in seeing who earns the most money in
each of the company's departments. A high earner in a department is an employee
who has a salary in the top three unique salaries for that department. Write a
solution to find the employees who are high earners in each of the departments.
Return the result table in any order.
Code:
Create Employee table
Expected output:
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
| 7 | Will | 70000 | 1 |
| id | name |
| -- | ----- |
| 1 | IT |
| 2 | Sales |
4. Assume there are three Spotify tables: artists, songs, and global_song_rank, which
contain information about the artists, songs, and music charts, respectively.
Write a query to find the top 5 artists whose songs appear most frequently in the Top
10 of the global_song_rank table. Display the top 5 artist names in ascending order,
along with their song appearance ranking.
If two or more artists have the same number of song appearances, they should be
assigned the same ranking, and the rank numbers should be continuous (i.e. 1, 2, 2,
3,4,5)
Code:
artist_name artist_rank
Taylor Swift 1
Bad Bunny 2
Drake 2
Ed Sheeran 3
Adele 3
Lady Gaga 4
Katy Perry 5
5. Assume you are given the table below on Uber transactions made by users. Write
a query to obtain the third transaction of every user. Output the user id, spend and
transactiondate.
Code:
CREATE TABLE transactions (
user_id INT,
spend DECIMAL(10,2),
transaction_date DATETIME
);
Expected output:
1. Samantha interviews many candidates from different colleges using coding challenges and
contests. Write a query to print the contest_id, hacker_id, name, and the sums of
total_submissions, total_accepted_submissions, total_views, and total_unique_views for each
contest sorted by contest_id. Exclude the contest from the result if all four sums are .
Note: A specific contest can be used to screen candidates at more than one college, but each
Input Format
● Contests: The contest_id is the id of the contest, hacker_id is the id of the hacker who
Colleges: The college_id is the id of the college, and contest_id is the id of the contest that
Challenges: The challenge_id is the id of the challenge that belongs to one of the contests
whose contest_id Samantha forgot, and college_id is the id of the college where the challenge
View_Stats: The challenge_id is the id of the challenge, total_views is the number of times the
challenge was viewed by candidates, and total_unique_views is the number of times the
Code:
Expected Output
Write a query to print total number of unique hackers who made at least submission each day
(starting on the first day of the contest), and find the hacker_id and name of the hacker who
made maximum number of submissions each day. If more than one such hacker has a maximum
number of submissions, print the lowest hacker_id. The query should print this information for
For the following sample input, assume that the end date of the contest was March 06, 2016.
Code:
submission_date DATE,
hacker_id INT,
score INT,
Table: Customer
+-------------+---------+
+-------------+---------+
| customer_id | int |
| product_key | int |
+-------------+---------+
Product
+-------------+---------+
+-------------+---------+
| product_key | int |
+-------------+---------+
product_key is the primary key (column with unique values) for this table.
Write a solution to report the customer ids from the Customer table that bought all the products in
Code:
+-------------+
| customer_id |
+-------------+
|1 |
|3 |
+-------------+
The customers who bought all the products (5 and 6) are customers with IDs 1 and 3.