Skip to content

SQL Exercises #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions MYSQL/1757_RecyclableAndLowFatProducts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 1757. Recyclable and Low Fat Products
* **Topic** --> **_SQL Schema_**
* Has been asked in **Facebook** interviews **4** times.
---
## Task Introduction
* **_product_id_** is the primary key for this table. low_fats is an ENUM of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not. **_recyclable_** is an ENUM of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.

```sh
Table Name: Products
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| low_fats | enum |
| recyclable | enum |
+-------------+---------+
```

#### **_To Do 1_**: Write an SQL query to find the ids of products that are both low fat and recyclable.
#### **_To Do 2_**: Return the result table in **any order**.
#### **_To Do 3_**: The query result format is in the following example.

##### **Example 1**
```sh
Input:
Products table:
+-------------+----------+------------+
| product_id | low_fats | recyclable |
+-------------+----------+------------+
| 0 | Y | N |
| 1 | Y | Y |
| 2 | N | Y |
| 3 | Y | Y |
| 4 | N | N |
+-------------+----------+------------+
Output:
+-------------+
| product_id |
+-------------+
| 1 |
| 3 |
+-------------+
Explanation: Only products 1 and 3 are both low fat and recyclable.
```
---
## Solution
```SQL
SELECT product_id -- to find the ids of products that are both low fat and recyclable
FROM Products
WHERE low_fats = 'Y'
AND recyclable = 'Y'
```

```sh
Input: Products =
+------------+----------+------------+
| product_id | low_fats | recyclable |
| ---------- | -------- | ---------- |
| 0 | Y | N |
| 1 | Y | Y |
| 2 | N | Y |
| 3 | Y | Y |
| 4 | N | N |
+------------+----------+------------+

Expected:
+------------+
| product_id |
| ---------- |
| 1 |
| 3 |
+------------+
```
4 changes: 4 additions & 0 deletions MYSQL/1757_RecyclableandLowFatProducts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT product_id -- to find the ids of products that are both low fat and recyclable
FROM Products
WHERE low_fats = 'Y'
AND recyclable = 'Y'
38 changes: 38 additions & 0 deletions MYSQL/584_FindCustomerReferee.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 584 Find Customer Referee
* **Topic** --> **_SQL Schema_**

---
## Task Introduction
* Find the names of the customer that **are not referred** by the customer with **id = 2**. Return the result table in **any order**.The result format is in the following example.

```sh
Input: Customer table:
+----+------+------------+
| id | name | referee_id |
+----+------+------------+
| 1 | Will | null |
| 2 | Jane | null |
| 3 | Alex | 2 |
| 4 | Bill | null |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
+----+------+------------+
Output:
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+
```

---
## Solution
```SQL
SELECT name
FROM Customer
WHERE referee_id <> 2
OR referee_id IS NULL
```
5 changes: 5 additions & 0 deletions MYSQL/584_FindCustomerReferee.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT name
FROM Customer
WHERE referee_id <> 2
OR referee_id IS NULL

68 changes: 68 additions & 0 deletions MYSQL/595_BigCountries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 595.Big Countries
* **Topic** --> **_SQL Schema_**
---
## Task Introduction
* Name is the primary ey column for the below table. Each row of this table gives information about the name of a country, the continent to which it blongs, its area, the population, and its GDP value.

```sh
Table Name: World
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | bigint |
+-------------+---------+
```

* A country is **_big_** if:
* it has an area of at least three million (i.e 300000 km^2), or
* it has a population of at least twenty-five million (i.e. 25000000).
#### **_To Do 1_**: Write an SQL query to report the name, population, and area of the **big countries**.
#### **_To Do 2_**: Return the result table in **any order**.
#### **_To Do 3_**: Return the result table in **any order**.
##### **Example 1**
```sh
Input:
World table:
+-------------+-----------+---------+------------+--------------+
| name | continent | area | population | gdp |
+-------------+-----------+---------+------------+--------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
| Albania | Europe | 28748 | 2831741 | 12960000000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000000 |
| Andorra | Europe | 468 | 78115 | 3712000000 |
| Angola | Africa | 1246700 | 20609294 | 100990000000 |
+-------------+-----------+---------+------------+--------------+
Output:
+-------------+------------+---------+
| name | population | area |
+-------------+------------+---------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+-------------+------------+---------+
```
---
## Solution
```SQL
SELECT name, population,area
FROM World
WHERE area >= 3000000
OR population >= 25000000
```

```sh
Output:
| name | population | area |
| ----------- | ---------- | ------- |
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |

Expected:
| name | population | area |
| ----------- | ---------- | ------- |
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
```
4 changes: 4 additions & 0 deletions MYSQL/595_BigCountries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT name, population,area
FROM World
WHERE area >= 3000000
OR population >= 25000000