SQL Joins
1.Given the CITY and COUNTRY tables, query the names of all cities where the CONTINENT is 'Africa'.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
Input Format
The CITY and COUNTRY tables are described as follows
2. Given the CITY and COUNTRY tables, query the sum of the populations of all cities where
the CONTINENT is 'Asia'.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
Input Format
The CITY and COUNTRY tables are described as follows:
3. Rising Temperature
Table: Weather
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.
Write a solution to find all dates' id with higher temperatures compared to its previous dates
(yesterday).
Return the result table in any order.
The result format is in the following example.
4. Managers with atleast five direct reports
Table: Employee
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| department | varchar |
| managerId | int |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table indicates the name of an employee, their department, and the id of their
manager.
If managerId is null, then the employee does not have a manager.
No employee will be the manager of themself.
Write a solution to find managers with at least five direct reports.
Return the result table in any order.
The result format is in the following example.
5. Replace employee ID with Unique identifier
Table: Employees
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table contains the id and the name of an employee in a company.
Table: EmployeeUNI
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| unique_id | int |
+---------------+---------+
(id, unique_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the id and the corresponding unique id of an employee in the
company.
Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just
show null.
Return the result table in any order.
The result format is in the following example.
Input:
Employees table:
+----+----------+
| id | name |
+----+----------+
| 1 | Alice |
| 7 | Bob |
| 11 | Meir |
| 90 | Winston |
| 3 | Jonathan |
+----+----------+
EmployeeUNI table:
+----+-----------+
| id | unique_id |
+----+-----------+
|3 |1 |
| 11 | 2 |
| 90 | 3 |
+----+-----------+
Output:
+-----------+----------+
| unique_id | name |
+-----------+----------+
| null | Alice |
| null | Bob |
|2 | Meir |
|3 | Winston |
|1 | Jonathan |
+-----------+----------+
6. Median
A median is defined as a number separating the higher half of a data set from the lower half. Query
the median of the Northern Latitudes (LAT_N) from STATION and round your answer to decimal
places.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
7. Cities With Completed Trades
Robinhood SQL Interview Question
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.
trades Table:
Column Name Type
order_id integer
user_id integer
quantity integer
status string ('Completed', 'Cancelled')
Column Name Type
date timestamp
price decimal (5, 2)
trades Example Input:
order_id user_id quantity status date price
100101 111 10 Cancelled 08/17/2022 12:00:00 9.80
100102 111 10 Completed 08/17/2022 12:00:00 10.00
100259 148 35 Completed 08/25/2022 12:00:00 5.10
100264 148 40 Completed 08/26/2022 12:00:00 4.80
100305 300 15 Completed 09/05/2022 12:00:00 10.00
100400 178 32 Completed 09/17/2022 12:00:00 12.00
100565 265 2 Completed 09/27/2022 12:00:00 8.70
users Table:
Column Name Type
user_id integer
city string
email string
signup_date datetime
users Example Input:
user_id city email signup_date
111 San Francisco [email protected] 08/03/2021 12:00:00
user_id city email signup_date
178 San Francisco [email protected] 01/05/2022 12:00:00
300 San Francisco [email protected] 06/30/2022 12:00:00
Example Output:
city total_orders
San Francisco 3
Boston 2
Denver 1
In the given dataset, San Francisco has the highest number of completed trade orders with 3 orders.
Boston holds the second position with 2 orders, and Denver ranks third with 1 order.
8. Average Review Ratings
Amazon
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.
P.S. If you've read the Ace the Data Science Interview, and liked it, consider writing us a review?
reviews Table:
Column Name Type
review_id integer
user_id integer
submit_date datetime
product_id integer
stars integer (1-5)
reviews Example Input:
review_id user_id submit_date product_id stars
6171 123 06/08/2022 00:00:00 50001 4
review_id user_id submit_date product_id stars
7802 265 06/10/2022 00:00:00 69852 4
5293 362 06/18/2022 00:00:00 50001 3
6352 192 07/26/2022 00:00:00 69852 3
4517 981 07/05/2022 00:00:00 69852 2
Example Output:
mth product avg_stars
6 50001 3.50
6 69852 4.00
7 69852 2.50
Explanation
Product 50001 received two ratings of 4 and 3 in the month of June (6th month), resulting in an
average star rating of 3.5.
9. Well Paid Employees
FAANG
Companies often perform salary analyses to ensure fair compensation practices. One useful analysis
is to check if there are any employees earning more than their direct managers.
As a HR Analyst, you're asked to identify all employees who earn more than their direct managers.
The result should include the employee's ID and name.
employee Schema:
column_name type description
employee_id integer The unique ID of the employee.
name string The name of the employee.
salary integer The salary of the employee.
department_id integer The department ID of the employee.
manager_id integer The manager ID of the employee.
employee Example Input:
employee_id name salary department_id manager_id
1 Emma Thompson 3800 1 6
2 Daniel Rodriguez 2230 1 7
3 Olivia Smith 7000 1 8
4 Noah Johnson 6800 2 9
5 Sophia Martinez 1750 1 11
6 Liam Brown 13000 3 NULL
7 Ava Garcia 12500 3 NULL
8 William Davis 6800 2 NULL
Example Output:
employee_id employee_name
3 Olivia Smith
The output shows that Olivia Smith earns $7,000, surpassing her manager, William David who earns
$6,800.
10.