SQLIZE EVENT 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:
● parts_assembly table contains all parts currently in production, each at
varying stages of the assembly process.
● An unfinished part is one that lacks a finish_date.
Code:
CREATE TABLE Assembely (
part VARCHAR(255),
finish_date DATETIME NULL,
assembly_step INT
);
INSERT INTO Assembely (part, finish_date, assembly_step) VALUES
('battery', '2022-01-22 00:00:00', 1),
('battery', '2022-02-22 00:00:00', 2),
('battery', '2022-03-22 00:00:00', 3),
('bumper', '2022-01-22 00:00:00', 1),
('bumper', '2022-02-22 00:00:00', 2),
('bumper', NULL, 3),
('bumper', NULL, 4);
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:
CREATE TABLE viewer (
user_id INT,
device_type VARCHAR(50),
view_time DATETIME
);
-- Insert sample data
INSERT INTO viewer (user_id, device_type, view_time) VALUES
(123, 'tablet', '2022-01-02 00:00:00'),
(125, 'laptop', '2022-01-07 00:00:00'),
(128, 'laptop', '2022-02-09 00:00:00'),
(129, 'phone', '2022-02-09 00:00:00'),
(145, 'tablet', '2022-02-24 00:00:00');
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
);
INSERT INTO jobs (job_id, company_id, title, description) VALUES
(248, 827, 'Business Analyst', 'Business analyst evaluates past and current business data
with the primary goal of improving decision-making processes within organizations.'),
(149, 845, 'Business Analyst', 'Business analyst evaluates past and current business data
with the primary goal of improving decision-making processes within organizations.'),
(945, 345, 'Data Analyst', 'Data analyst reviews data to identify key insights into a
business''s customers and ways the data can be used to solve problems.'),
(164, 345, 'Data Analyst', 'Data analyst reviews data to identify key insights into a
business''s customers and ways the data can be used to solve problems.'),
(172, 244, 'Data Engineer', 'Data engineer works in a variety of settings to build systems
that collect, manage, and convert raw data into usable information for data scientists and
business analysts to interpret.');
Expected output:
compan title description job_co
y_id unt
827 Data Data scientist uses data to understand and 2
Scientist explain the phenomena around them, and help
organizations make better decisions.
244 Data Data engineer works in a variety of settings to 1
Engineer build systems that collect, manage, and convert
raw data into usable information for data
scientists and business analysts to interpret.
845 Business Business analyst evaluates past and current 1
Analyst business data with the primary goal of improving
decision-making processes within organizations.
244 Software Software engineers design and create computer 2
Engineer systems and applications to solve real-world
problems.
345 Data Data analyst reviews data to identify key insights 2
Analyst into a business's customers and ways the data
can be used to solve problems.
827 Business Business analyst evaluates past and current 1
Analyst business data with the primary goal of improving
decision-making processes within organizations.
400 Business Business intelligence analyst reviews data to 1
Intelligence produce finance and market intelligence reports.
Analyst
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
);
-- Insert sample data
INSERT INTO reviews (review_id, user_id, submit_date, product_id, stars) VALUES
(6171, 123, '2022-06-08 00:00:00', 50001, 4),
(7802, 265, '2022-06-10 00:00:00', 69852, 4),
(5293, 362, '2022-06-18 00:00:00', 50001, 3),
(6352, 192, '2022-07-26 00:00:00', 69852, 3),
(4517, 981, '2022-07-05 00:00:00', 69852, 2);
Expected output:
mth product_id avg_stars
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:
CREATE TABLE traders (
order_id INT PRIMARY KEY,
user_id INT,
quantity INT,
status VARCHAR(20),
order_date DATETIME,
price DECIMAL(5,2)
);
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);
CREATE TABLE users (
user_id INT PRIMARY KEY,
city VARCHAR(50),
email VARCHAR(100),
signup_date DATETIME
);
INSERT INTO users (user_id, city, email, signup_date) VALUES
(111, 'San Francisco', 'rrok10@gmail.com', '2021-08-03 12:00:00'),
(148, 'Boston', 'sailor9820@gmail.com', '2021-08-20 12:00:00'),
(178, 'San Francisco', 'harrypotterfan182@gmail.com', '2022-01-05 12:00:00'),
(265, 'Denver', 'shadower_@hotmail.com', '2022-02-26 12:00:00'),
(300, 'San Francisco', 'houstoncowboy1122@hotmail.com', '2022-06-30 12:00:00');
Expected Output:
city order_id
San Francisco 100102
San Francisco 100101
Boston 100900
Boston 100259
Boston 100264
San Francisco 100777
San Francisco 100400
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
);
-- Insert sample data
INSERT INTO transactions (id, country, state, amount, trans_date) VALUES
(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');
Expected output:
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
2. If the customer's preferred delivery date is the same as the order date, then the
order is called immediate; otherwise, it is called scheduled. The first order of a
customer is the order with the earliest order date that the customer made. It is
guaranteed that a customer has precisely one first order. Write a solution to find the
percentage of immediate orders in the first orders of all customers, rounded to 2
decimal places.
Code:
CREATE TABLE delivery (
delivery_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
customer_pref_delivery_date DATE
);
-- Insert sample data
INSERT INTO delivery (customer_id, order_date, customer_pref_delivery_date,delivery_id)
VALUES
(1, '2019-08-01', '2019-08-02',1),
(2, '2019-08-02', '2019-08-02',2),
(1, '2019-08-11', '2019-08-12',3),
(3, '2019-08-24', '2019-08-24',4),
(3, '2019-08-21', '2019-08-22',5),
(2, '2019-08-11', '2019-08-13',6),
(4, '2019-08-09', '2019-08-09',7);
Expected Output:
deliver_id customer_id order_date customer_pref_deliver
y_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
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
CREATE TABLE Employee (
id INT,
name VARCHAR(255),
salary DECIMAL(10,2),
departmentId INT,
PRIMARY KEY (id)
);
-- Insert sample data into Employee table
INSERT INTO Employee (id, name, salary, departmentId) VALUES
(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);
-- Create Department table
CREATE TABLE Department (
id INT,
name VARCHAR(255),
PRIMARY KEY (id)
);
-- Insert sample data into Department table
INSERT INTO Department (id, name) VALUES
(1, 'IT'),
(2, 'Sales');
Expected output:
| id | name | salary | departmentId |
| -- | ----- | ------ | ------------ |
| 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:
CREATE TABLE artists (
artist_id INTEGER PRIMARY KEY,
artist_name VARCHAR(255) NOT NULL,
label_owner VARCHAR(255) NOT NULL
);
CREATE TABLE songs (
song_id INTEGER PRIMARY KEY,
artist_id INTEGER,
name VARCHAR(255) NOT NULL,
FOREIGN KEY (artist_id) REFERENCES artists(artist_id)
);
CREATE TABLE global_song_rank (
day INTEGER CHECK (day BETWEEN 1 AND 52),
song_id INTEGER,
rank INTEGER CHECK (rank BETWEEN 1 AND 1000000),
FOREIGN KEY (song_id) REFERENCES songs(song_id)
);
INSERT INTO artists (artist_id, artist_name, label_owner) VALUES
(101, 'Ed Sheeran', 'Warner Music Group'),
(120, 'Drake', 'Warner Music Group'),
(125, 'Bad Bunny', 'Rimas Entertainment');
INSERT INTO songs (song_id, artist_id, name) VALUES
(55511, 101, 'Perfect'),
(45202, 101, 'Shape of You'),
(22222, 120, 'One Dance'),
(19960, 120, 'Hotline Bling');
INSERT INTO global_song_rank (day, song_id, rank) VALUES
(1, 45202, 5),
(3, 45202, 2),
(1, 19960, 3),
(9, 19960, 15);
Expected output:
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
);
INSERT INTO transactions (user_id, spend, transaction_date) VALUES
(111, 100.50, '2022-08-01 12:00:00'),
(111, 55.00, '2022-10-01 12:00:00'),
(121, 36.00, '2022-18-01 12:00:00'),
(145, 24.99, '2022-26-01 12:00:00'),
(111, 89.60, '2022-05-02 12:00:00'),
(145, 45.30, '2022-28-02 12:00:00'),
(121, 22.20, '2022-01-04 12:00:00'),
(121, 67.90, '2022-03-04 12:00:00'),
(263, 156.00, '2022-11-04 12:00:00'),
(230, 78.30, '2022-14-06 12:00:00'),
(263, 68.12, '2022-11-07 12:00:00'),
(263, 100.00, '2022-12-07 12:00:00');
Expected output:
user_id spend transaction_date
111 89.60 02/05/2022 12:00:00
HARD
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
college only holds screening contest.
Input Format
The following tables hold interview data:
● Contests: The contest_id is the id of the contest, hacker_id is the id of the hacker who
created the contest, and name is the name of the hacker.
Colleges: The college_id is the id of the college, and contest_id is the id of the contest that
Samantha used to screen the candidates.
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
was given to candidates.
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
challenge was viewed by unique candidates.
Submission_Stats: The challenge_id is the id of the challenge, total_submissions is the number
of submissions for the challenge, and total_accepted_submission is the number of submissions
that achieved full scores.
Code:
CREATE TABLE Contest (
contest_id INTEGER PRIMARY KEY,
hacker_id INTEGER,
name VARCHAR(255) NOT NULL
);
INSERT INTO Contest (contest_id, hacker_id, name) VALUES
(66406, 17973, 'Rose'),
(66556, 79153, 'Angela'),
(94828, 80275, 'Frank');
CREATE TABLE Colleges (
college_id INTEGER PRIMARY KEY,
contest_id INTEGER,
FOREIGN KEY (contest_id) REFERENCES Contest(contest_id)
);
INSERT INTO Colleges (college_id, contest_id) VALUES
(11219, 66406),
(32473, 66556),
(56685, 94828);
CREATE TABLE Challenges (
challenge_id INTEGER PRIMARY KEY,
college_id INTEGER,
FOREIGN KEY (college_id) REFERENCES Colleges(college_id)
);
INSERT INTO Challenges (challenge_id, college_id) VALUES
(18765, 11219),
(47127, 11219),
(60292, 32473),
(72974, 56685);
CREATE TABLE View_Stats (
challenge_id INTEGER,
total_views INTEGER,
total_unique_views INTEGER,
FOREIGN KEY (challenge_id) REFERENCES Challenges(challenge_id)
);
INSERT INTO View_Stats (challenge_id, total_views, total_unique_views) VALUES
(47127, 26, 19),
(47127, 15, 14),
(18765, 43, 10),
(18765, 72, 13),
(75516, 35, 17),
(60292, 11, 10),
(72974, 41, 15),
(75516, 75, 11);
CREATE TABLE Submission_Stats (
challenge_id INTEGER,
total_submissions INTEGER,
total_accepted_submissions INTEGER,
FOREIGN KEY (challenge_id) REFERENCES Challenges(challenge_id)
);
INSERT INTO Submission_Stats (challenge_id, total_submissions,
total_accepted_submissions) VALUES
(75516, 34, 12),
(47127, 27, 10),
(47127, 56, 18),
(75516, 74, 12),
(75516, 83, 8),
(72974, 68, 24),
(72974, 82, 14),
(47127, 28, 11);
Expected Output
845 579 Rose 1987 580 1635 566
858 1053 Angela 703 160 1002 384
883 1055 Frank 1121 319 1217 338
1793 2655 Patrick 1337 360 1216 412
2374 2765 Lisa 2733 815 3368 904
2963 2845 Kimberly 4306 1221 3603 1184
3584 2873 Bonnie 2492 652 3019 954
4044 3067 Michael 1323 449 1722 528
4249 3116 Todd 1452 376 1767 463
4269 3256 Joe 1018 372 1766 530
4483 3386 Earl 1911 572 1644 477
4541 3608 Robert 1886 516 1694 504
4601 3868 Amy 1900 639 1738 548
4710 4255 Pamela 2752 639 2378 705
4982 5639 Maria 2705 759 2558 711
5913 5669 Joe 2646 790 3181 835
5994 5713 Linda 3369 967 3048 954
6939 6550 Melissa 2842 859 3574 1004
7266 6947 Carol 2758 665 3044 835
7280 7030 Paula 1963 554 886 259
7484 7033 Marilyn 3217 934 3795 1061
7734 7386 Jennifer 3780 1015 3637 1099
7831 7787 Harry 3190 883 2933 1012
7862 8029 David 1738 476 1475 472
8812 8147 Julia 1044 302 819 266
8825 8438 Kevin 2624 772 2187 689
9136 8727 Paul 4205 1359 3125 954
9613 8762 James 3438 943 3620 1046
10568 8802 Kelly 1907 620 2577 798
11100 8809 Robin 1929 613 1883 619
12742 9203 Ralph 1523 413 1344 383
12861 9644 Gloria 1596 536 2089 623
12865 10108 Victor 2076 597 1259 418
13503 10803 David 924 251 584 167
13537 11390 Joyce 1381 497 1784 538
13612 12592 Donna 1981 550 1487 465
14502 12923 Michelle 1510 463 1830 545
14867 13017 Stephanie 2471 676 2291 574
15164 13256 Gerald 2570 820 2085 607
15804 13421 Walter 1454 459 1396 476
15891 13569 Christina 2188 710 2266 786
16063 14287 Brandon 1804 580 1621 521
16415 14311 Elizabeth 4535 1366 3631 1071
18477 14440 Joseph 1320 391 1419 428
18855 16973 Lawrence 2967 1020 3371 1011
19097 17123 Marilyn 2956 807 2554 750
19575 17562 Lori 2590 863 2627 760
Hard 2
Julia conducted a days of learning SQL contest. The start date of the contest was March 01,
2016 and the end date was March 15, 2016.
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
each day of the contest, sorted by the date.
For the following sample input, assume that the end date of the contest was March 06, 2016.
Code:
CREATE TABLE Hackers (
hacker_id INTEGER PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
INSERT INTO Hackers (hacker_id, name) VALUES
(15758, 'Rose'),
(20703, 'Angela'),
(36396, 'Frank'),
(38289, 'Patrick'),
(44065, 'Lisa'),
(53473, 'Kimberly'),
(62529, 'Bonnie'),
(79722, 'Michael');
CREATE TABLE submissions (
submission_id INT PRIMARY KEY,
submission_date DATE,
hacker_id INT,
score INT,
FOREIGN KEY (hacker_id) REFERENCES Hackers(hacker_id)
);
INSERT INTO submissions (submission_id, submission_date, hacker_id,
score) VALUES
(8494, '2016-03-01', 20703, 0),
(22403, '2016-03-01', 53473, 15),
(23965, '2016-03-01', 79722, 60),
(30173, '2016-03-01', 36396, 70),
(34928, '2016-03-02', 20703, 0),
(38740, '2016-03-02', 15758, 60),
(42769, '2016-03-02', 79722, 25),
(44364, '2016-03-02', 79722, 60),
(45440, '2016-03-03', 20703, 0),
(49050, '2016-03-03', 36396, 70),
(50273, '2016-03-03', 79722, 5),
(50344, '2016-03-04', 20703, 0),
(51360, '2016-03-04', 44065, 90),
(54404, '2016-03-04', 53473, 65),
(61533, '2016-03-04', 79722, 45),
(72852, '2016-03-05', 20703, 0),
(74546, '2016-03-05', 38289, 0),
(76487, '2016-03-05', 62529, 0),
(82439, '2016-03-05', 36396, 10),
(90006, '2016-03-05', 36396, 40),
(90404, '2016-03-06', 20703, 0);
Expected output:
2016-03-01 112 81314 Denise
2016-03-02 59 39091 Ruby
2016-03-03 51 18105 Roy
2016-03-04 49 533 Patrick
2016-03-05 49 7891 Stephanie
2016-03-06 49 84307 Evelyn
2016-03-07 35 80682 Deborah
2016-03-08 35 10985 Timothy
2016-03-09 35 31221 Susan
2016-03-10 35 43192 Bobby
2016-03-11 35 3178 Melissa
2016-03-12 35 54967 Kenneth
2016-03-13 35 30061 Julia
2016-03-14 35 32353 Rose
2016-03-15 35 27789 Helen
Hard 3
Table: Customer
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| customer_id | int |
| product_key | int |
+-------------+---------+
This table may contain duplicates rows.
customer_id is not NULL.
product_key is a foreign key (reference column) to Product table.
Product
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| 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
the Product table.
Return the result table in any order.
The result format is in the following example.
Code:
CREATE TABLE Product (
product_key INT PRIMARY KEY
);
INSERT INTO Product (product_key) VALUES
(5),
(6);
CREATE TABLE Customer (
customer_id INT NOT NULL,
product_key INT,
FOREIGN KEY (product_key) REFERENCES Product(product_key)
);
INSERT INTO Customer (customer_id, product_key) VALUES
(1, 5),
(2, 6),
(3, 5),
(3, 6),
(1, 6);
+-------------+
| customer_id |
+-------------+
|1 |
|3 |
+-------------+
The customers who bought all the products (5 and 6) are customers with IDs 1 and 3.