0% found this document useful (0 votes)
37 views

Leet code Problems DBMS (1)

Uploaded by

blesswinsj12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Leet code Problems DBMS (1)

Uploaded by

blesswinsj12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1378.

Replace Employee ID With The


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.

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.

# Write your MySQL query statement below


select EmployeeUNI.unique_id,Employees.name from Employees
left outer join EmployeeUNI on EmployeeUNI.id=Employees.id;

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 |

1068. Product Sales Analysis I


Table: Sales

+-------------+-------+

| Column Name | Type |

+-------------+-------+

| 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.

product_id is a foreign key (reference column) to Product table.

Each row of this table shows a sale on the product product_id in a certain year.

Note that the price is per unit.

Table: Product

+--------------+---------+

| Column Name | Type |

+--------------+---------+

| 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.

Return the resulting table in any order.

The result format is in the following example.


Example 1:

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_name | year | price |

+--------------+-------+-------+

| Nokia | 2008 | 5000 |

| Nokia | 2009 | 5000 |

| Apple | 2011 | 9000 |

+--------------+-------+-------+

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 =

| sale_id | product_id | year | quantity | price |

| ------- | ---------- | ---- | -------- | ----- |

|1 | 100 | 2008 | 10 | 5000 |

|2 | 100 | 2009 | 12 | 5000 |

|7 | 200 | 2011 | 15 | 9000 |

Product =

| product_id | product_name |

| ---------- | ------------ |

| 100 | Nokia |

| 200 | Apple |

| 300 | Samsung |

Output

| product_name | year | price |

| ------------ | ---- | ----- |

| Nokia | 2009 | 5000 |

| Nokia | 2008 | 5000 |

| Apple | 2011 | 9000 |

Expected

| product_name | year | price |

| ------------ | ---- | ----- |

| Nokia | 2009 | 5000 |

| Nokia | 2008 | 5000 |


| Apple | 2011 | 9000 |

1280. Students and Examinations


Table: Students

+---------------+---------+

| Column Name | Type |

+---------------+---------+

| 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

+--------------+---------+

| Column Name | Type |

+--------------+---------+

| 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

+--------------+---------+

| Column Name | Type |

+--------------+---------+
| 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.

Return the result table ordered by student_id and subject_name.

The result format is in the following example.

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:

+------------+--------------+--------------+----------------+

| 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 |

+------------+--------------+--------------+----------------+

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.

Alex did not attend any exams.

John attended the Math exam 1 time, the Physics exam 1 time, and the Programming exam 1 time.

# Write your MySQL query statement below


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;

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

| 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 |

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

this gives us the "headers" ["student_id", "student_name", "subject_name"].


Then, we want to join the Examinations table where
the student_id and subject_name fit with our previously created table:

SELECT *
FROM Students
JOIN Subjects
LEFT JOIN Examinations
ON Students.student_id = Examinations.student_id
AND Subjects.subject_name = Examinations.subject_name

This gives us the "headers" ["student_id", "student_name", "subject_name", "student_id",


"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

This will give us the "headers" ["student_id", "student_name", "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

Return the result table ordered by student_id and subject_name.

So we add the last line:


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

You might also like