Leet code Problems DBMS (1)
Leet code Problems DBMS (1)
+---------------+---------+
| 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.
Example 1:
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 |
+-----------+----------+
Explanation:
Alice and Bob do not have a unique ID, We will show null instead.
The unique ID of Meir is 2.
The unique ID of Winston is 3.
The unique ID of Jonathan is 1.
Accepted
Runtime: 288 ms
Case 1
Input
Employees =
| id | name |
| -- | -------- |
| 1 | Alice |
| 7 | Bob |
| 11 | Meir |
| 90 | Winston |
| 3 | Jonathan |
EmployeeUNI =
| id | unique_id |
| -- | --------- |
|3 |1 |
| 11 | 2 |
| 90 | 3 |
Output
| unique_id | name |
| --------- | -------- |
| null | Alice |
| null | Bob |
|2 | Meir |
|3 | Winston |
|1 | Jonathan |
Expected
| unique_id | name |
| --------- | -------- |
| null | Alice |
| null | Bob |
|2 | Meir |
|3 | Winston |
|1 | Jonathan |
+-------------+-------+
+-------------+-------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+-------------+-------+
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
Each row of this table shows a sale on the product product_id in a certain year.
Table: Product
+--------------+---------+
+--------------+---------+
| product_id | int |
| product_name | varchar |
+--------------+---------+
product_id is the primary key (column with unique values) of this table.
Each row of this table indicates the product name of each product.
Write a solution to report the product_name, year, and price for each sale_id in the Sales table.
Input:
Sales table:
+---------+------------+------+----------+-------+
+---------+------------+------+----------+-------+
+---------+------------+------+----------+-------+
Product table:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
+------------+--------------+
Output:
+--------------+-------+-------+
+--------------+-------+-------+
+--------------+-------+-------+
Explanation:
From sale_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008.
From sale_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009.
From sale_id = 7, we can conclude that Apple was sold for 9000 in the year 2011.
# Write your MySQL query statement below
select Product.product_name, Sales.year, Sales.price from
Sales join Product on Sales.product_id=Product.product_id;
Accepted
Runtime: 193 ms
Case 1
Input
Sales =
Product =
| product_id | product_name |
| ---------- | ------------ |
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
Output
Expected
+---------------+---------+
+---------------+---------+
| student_id | int |
| student_name | varchar |
+---------------+---------+
student_id is the primary key (column with unique values) for this table.
Each row of this table contains the ID and the name of one student in the school.
Table: Subjects
+--------------+---------+
+--------------+---------+
| subject_name | varchar |
+--------------+---------+
subject_name is the primary key (column with unique values) for this table.
Each row of this table contains the name of one subject in the school.
Table: Examinations
+--------------+---------+
+--------------+---------+
| student_id | int |
| subject_name | varchar |
+--------------+---------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
Each student from the Students table takes every course from the Subjects table.
Each row of this table indicates that a student with ID student_id attended the exam of
subject_name.
Write a solution to find the number of times each student attended each exam.
Example 1:
Input:
Students table:
+------------+--------------+
| student_id | student_name |
+------------+--------------+
|1 | Alice |
|2 | Bob |
| 13 | John |
|6 | Alex |
+------------+--------------+
Subjects table:
+--------------+
| subject_name |
+--------------+
| Math |
| Physics |
| Programming |
+--------------+
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 |
+------------+--------------+
Output:
+------------+--------------+--------------+----------------+
+------------+--------------+--------------+----------------+
|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 |
+------------+--------------+--------------+----------------+
Explanation:
The result table should contain all students and all subjects.
Alice attended the Math exam 3 times, the Physics exam 2 times, and the Programming exam 1 time.
Bob attended the Math exam 1 time, the Programming exam 1 time, and did not attend the Physics
exam.
John attended the Math exam 1 time, the Physics exam 1 time, and the Programming exam 1 time.
Accepted
Runtime: 241 ms
Case 1
Input
Students =
| student_id | student_name |
| ---------- | ------------ |
|1 | Alice |
|2 | Bob |
| 13 | John |
|6 | Alex |
Subjects =
| subject_name |
| ------------ |
| Math |
| Physics |
| Programming |
Examinations =
| student_id | subject_name |
| ---------- | ------------ |
|1 | Math |
|1 | Physics |
|1 | Programming |
|2 | Programming |
|1 | Physics |
|1 | Math |
View more
Output
|1 | Alice | Math |3 |
|1 | Alice | Physics |2 |
|1 | Alice | Programming | 1 |
|2 | Bob | Math |1 |
|2 | Bob | Physics |0 |
|2 | Bob | Programming | 1 |
View more
Expected
| 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 |
Solution
SELECT
Students.student_id,
Students.student_name,
Subjects.subject_name,
COUNT(Examinations.subject_name) AS attended_exams
FROM Students
JOIN Subjects
LEFT JOIN Examinations
ON Students.student_id = Examinations.student_id
AND Subjects.subject_name = Examinations.subject_name
GROUP BY Students.student_id, Subjects.subject_name
ORDER BY student_id ASC, subject_name ASC
Explanations
First, we want to group every students of Students with every subjects
in Subjects:
SELECT *
FROM Students
JOIN Subjects
SELECT *
FROM Students
JOIN Subjects
LEFT JOIN Examinations
ON Students.student_id = Examinations.student_id
AND Subjects.subject_name = Examinations.subject_name
As you can see, student_id and subject_name here are duplicated columns with
the same values.
We can select them like this:
SELECT
Students.student_id,
Students.student_name,
Subjects.subject_name
FROM Students
JOIN Subjects
LEFT JOIN Examinations
ON Students.student_id = Examinations.student_id
AND Subjects.subject_name = Examinations.subject_name
If you run it, you would see duplicated values of students with the same
subject.
This is because There is no primary key for [the Examinations] table. [So] It may contain
duplicates.
We want to group them and count the number attended exams like this:
SELECT
Students.student_id,
Students.student_name,
Subjects.subject_name,
COUNT(Examinations.subject_name) AS attended_exams
FROM Students
JOIN Subjects
LEFT JOIN Examinations
ON Students.student_id = Examinations.student_id
AND Subjects.subject_name = Examinations.subject_name
GROUP BY Students.student_id, Subjects.subject_name
Finally, we need to