From b8c7bd5f90dbe728b1cffa0afd95c3a735673359 Mon Sep 17 00:00:00 2001 From: gayda Date: Fri, 1 Mar 2024 01:55:18 +0100 Subject: [PATCH 1/5] SQL Exercises --- SQL/SQL/1757_RecyclableAndLowFatProducts.md | 73 +++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 SQL/SQL/1757_RecyclableAndLowFatProducts.md diff --git a/SQL/SQL/1757_RecyclableAndLowFatProducts.md b/SQL/SQL/1757_RecyclableAndLowFatProducts.md new file mode 100644 index 0000000..0dc6747 --- /dev/null +++ b/SQL/SQL/1757_RecyclableAndLowFatProducts.md @@ -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 | ++------------+ +``` \ No newline at end of file From 08cfae1a103db61e58917f4a70a18d757c768f19 Mon Sep 17 00:00:00 2001 From: gayda Date: Fri, 1 Mar 2024 02:10:34 +0100 Subject: [PATCH 2/5] Update in SQL --- {SQL/SQL => MYSQL}/1757_RecyclableAndLowFatProducts.md | 0 MYSQL/1757_RecyclableandLowFatProducts.sql | 4 ++++ 2 files changed, 4 insertions(+) rename {SQL/SQL => MYSQL}/1757_RecyclableAndLowFatProducts.md (100%) create mode 100644 MYSQL/1757_RecyclableandLowFatProducts.sql diff --git a/SQL/SQL/1757_RecyclableAndLowFatProducts.md b/MYSQL/1757_RecyclableAndLowFatProducts.md similarity index 100% rename from SQL/SQL/1757_RecyclableAndLowFatProducts.md rename to MYSQL/1757_RecyclableAndLowFatProducts.md diff --git a/MYSQL/1757_RecyclableandLowFatProducts.sql b/MYSQL/1757_RecyclableandLowFatProducts.sql new file mode 100644 index 0000000..2bdc131 --- /dev/null +++ b/MYSQL/1757_RecyclableandLowFatProducts.sql @@ -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' \ No newline at end of file From b1cf7cf35562564c59ff4bd3e71d8d017e5cc44b Mon Sep 17 00:00:00 2001 From: gayda Date: Fri, 1 Mar 2024 02:32:41 +0100 Subject: [PATCH 3/5] New SQL Exercise --- MYSQL/584_FindCustomerReferee.md | 38 +++++++++++++++++++++++++++++++ MYSQL/584_FindCustomerReferee.sql | 5 ++++ 2 files changed, 43 insertions(+) create mode 100644 MYSQL/584_FindCustomerReferee.md create mode 100644 MYSQL/584_FindCustomerReferee.sql diff --git a/MYSQL/584_FindCustomerReferee.md b/MYSQL/584_FindCustomerReferee.md new file mode 100644 index 0000000..12743ec --- /dev/null +++ b/MYSQL/584_FindCustomerReferee.md @@ -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 +``` diff --git a/MYSQL/584_FindCustomerReferee.sql b/MYSQL/584_FindCustomerReferee.sql new file mode 100644 index 0000000..be53028 --- /dev/null +++ b/MYSQL/584_FindCustomerReferee.sql @@ -0,0 +1,5 @@ +SELECT name +FROM Customer +WHERE referee_id <> 2 +OR referee_id IS NULL + From b232eb34487d8c604c5de053be2cfc8ac70eaa90 Mon Sep 17 00:00:00 2001 From: gayda Date: Sat, 2 Mar 2024 19:53:20 +0100 Subject: [PATCH 4/5] 595.Big Countries --- MYSQL/595_BigCountries.md | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 MYSQL/595_BigCountries.md diff --git a/MYSQL/595_BigCountries.md b/MYSQL/595_BigCountries.md new file mode 100644 index 0000000..ff7c197 --- /dev/null +++ b/MYSQL/595_BigCountries.md @@ -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 | +``` From ec54e1f4d6f6f6652972b98de8ec24c5b1fcb1c3 Mon Sep 17 00:00:00 2001 From: gayda Date: Sat, 2 Mar 2024 19:54:24 +0100 Subject: [PATCH 5/5] 595.sql --- MYSQL/595_BigCountries.sql | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 MYSQL/595_BigCountries.sql diff --git a/MYSQL/595_BigCountries.sql b/MYSQL/595_BigCountries.sql new file mode 100644 index 0000000..97f73a3 --- /dev/null +++ b/MYSQL/595_BigCountries.sql @@ -0,0 +1,4 @@ +SELECT name, population,area +FROM World +WHERE area >= 3000000 +OR population >= 25000000 \ No newline at end of file