SQL Leetcode
SQL Leetcode
--*************************************************************************************************
Create table Customer1 (id int, name varchar(25), referee_id int);
insert into Customer1 (id, name, referee_id) values ('1', 'Will',NULL);
insert into Customer1 (id, name, referee_id) values ('2', 'Jane', NULL);
insert into Customer1 (id, name, referee_id) values ('3', 'Alex', '2');
insert into Customer1 (id, name, referee_id) values ('4', 'Bill', NULL);
insert into Customer1 (id, name, referee_id) values ('5', 'Zack', '1');
insert into Customer1 (id, name, referee_id) values ('6', 'Mark', '2');
/*
Write an SQL query to report the names of the customer that are not referred by
the customer with id = 2.Return the result table in any order.The query result format is in the
following example.
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 |
file:///C:/Users/RATNESH/Downloads/Telegram%20Desktop/leetcodesbasic 12-Jan-23
Page 2 of 6
| Jane |
| Bill |
| Zack |
+------+
*/
select * from Customer1;
select name from Customer1 where referee_id !=2 or referee_id is null;
select name from Customer1 where id not in (select id from Customer1 where referee_id=2);
select name from Customer1 where name not in (select name from Customer1 where referee_id=2);
--select name from Customer1 where referee_id not in (select referee_id from Customer1 where referee_id=2);
--will give only zack,hence change to id column as it has no null values.
--****************************************************************************************************************
Create table Customers2 (id int, name varchar(255));
Create table Orders2 (id int, customerId int);
insert into Customers2 (id, name) values ('1', 'Joe');
insert into Customers2 (id, name) values ('2', 'Henry');
insert into Customers2 (id, name) values ('3', 'Sam');
insert into Customers2 (id, name) values ('4', 'Max');
insert into Orders2 (id, customerId) values ('1', '3');
insert into Orders2 (id, customerId) values ('2', '1');
/*
Write an SQL query to report all customers who never order anything.
Return the result table in any order.
The query result format is in the following example.
Customers table:
+----+-------+
| id | name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders table:
+----+------------+
| id | customerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
Output:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
*/
select * from Customers2;
select * from Orders2;
--customers who oredered food
select c.id,c.name,o.id,o.customerId from Customers2 c
join Orders2 o on c.id=o.customerId;
--customers who ordered food display only name now
select c.name from Customers2 c
join Orders2 o on c.id=o.customerId;
--method1
--customers who didnt ordered food display only name
select c.name as Customers from Customers2 c
where c.name not in (select c.name from Customers2 c
join Orders2 o on c.id=o.customerId);
--method 2
select c.name as Customers from Customers2 c
where c.id not in (select customerid from orders2);
--********************************************************************************************************************
Create table Employees4 (employee_id int, name varchar(30), salary int);
insert into Employees4 (employee_id, name, salary) values ('2', 'Meir', '3000');
insert into Employees4 (employee_id, name, salary) values ('3', 'Michael', '3800');
insert into Employees4 (employee_id, name, salary) values ('7', 'Addilyn', '7400');
insert into Employees4 (employee_id, name, salary) values ('8', 'Juan', '6100');
insert into Employees4 (employee_id, name, salary) values ('9', 'Kannon', '7700');
/*
Write an SQL query to calculate the bonus of each employee. The bonus of an employee is 100% of their salary if the ID
of the employee is an odd number and the employee name does not start with the character 'M'. The bonus of an employee is 0
otherwise.Return the result table ordered by employee_id.The query result format is in the following example.
Employees table:
+-------------+---------+--------+
| employee_id | name | salary |
+-------------+---------+--------+
| 2 | Meir | 3000 |
| 3 | Michael | 3800 |
| 7 | Addilyn | 7400 |
| 8 | Juan | 6100 |
| 9 | Kannon | 7700 |
+-------------+---------+--------+
Output:
+-------------+-------+
| employee_id | bonus |
+-------------+-------+
| 2 | 0 |
| 3 | 0 |
| 7 | 7400 |
| 8 | 0 |
| 9 | 7700 |
+-------------+-------+
Explanation:
The employees with IDs 2 and 8 get 0 bonus because they have an even employee_id.
The employee with ID 3 gets 0 bonus because their name starts with 'M'.
The rest of the employees get a 100% bonus.
*/
file:///C:/Users/RATNESH/Downloads/Telegram%20Desktop/leetcodesbasic 12-Jan-23
Page 3 of 6
select employee_id,case when mod(employee_id,2)!=0 and name not like('M%') then salary
else 0
end as Bonus
from Employees4
order by employee_id;
file:///C:/Users/RATNESH/Downloads/Telegram%20Desktop/leetcodesbasic 12-Jan-23
Page 4 of 6
--*********************************************************************************************************************
Create table Users (user_id int, name varchar(40));
insert into Users (user_id, name) values ('1', 'aLice');
insert into Users (user_id, name) values ('2', 'bOB');
/*
Write an SQL query to fix the names so that only the first character is uppercase and the rest are lowercase.
Return the result table ordered by user_id.
The query result format is in the following example.
Users table:
+---------+-------+
| user_id | name |
+---------+-------+
| 1 | aLice |
| 2 | bOB |
+---------+-------+
Output:
+---------+-------+
| user_id | name |
+---------+-------+
| 1 | Alice |
| 2 | Bob |
+---------+-------+
*/
select user_id,upper( substr(name,1,1) ) || lower( substr(name,2) ) as name from Users;
select user_id,INITCAP(name) as name from Users;
--**********************************************************************************************************
Create table Activities(sell_date varchar(20), product varchar(20));
insert into Activities(sell_date, product) values ('2020-05-30', 'Headphone');
insert into Activities(sell_date, product) values ('2020-06-01', 'Pencil');
insert into Activities(sell_date, product) values ('2020-06-02', 'Mask');
insert into Activities(sell_date, product) values ('2020-05-30', 'Basketball');
insert into Activities(sell_date, product) values ('2020-06-01', 'Bible');
insert into Activities(sell_date, product) values ('2020-06-02', 'Mask');
insert into Activities(sell_date, product) values ('2020-05-30', 'T-Shirt');
select * from Activities;
/*
Input:
Activities table:
+------------+------------+
| sell_date | product |
+------------+------------+
| 2020-05-30 | Headphone |
| 2020-06-01 | Pencil |
| 2020-06-02 | Mask |
| 2020-05-30 | Basketball |
| 2020-06-01 | Bible |
| 2020-06-02 | Mask |
| 2020-05-30 | T-Shirt |
+------------+------------+
Output:
+------------+----------+------------------------------+
| sell_date | num_sold | products |
+------------+----------+------------------------------+
| 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
| 2020-06-01 | 2 | Bible,Pencil |
| 2020-06-02 | 1 | Mask |
+------------+----------+------------------------------+
Explanation:
For 2020-05-30, Sold items were (Headphone, Basketball, T-shirt), we sort them lexicographically and separate them by a comma.
For 2020-06-01, Sold items were (Pencil, Bible), we sort them lexicographically and separate them by a comma.
For 2020-06-02, the Sold item is (Mask), we just return it.
*/
--first group by sell_dates and count them
select distinct sell_date,count(product) from Activities group by sell_date order by sell_date;
--2020-05-30 3 --it has all unique products here.
--2020-06-01 2 --it has all unique products here.
--2020-06-02 2 --it has 2 masks we dont want count as 2 we want count as 1.
--now how to display the products name in front of the count of '1' in front of 2020-06-02
select distinct sell_date,product from Activities;--distinct values are displayed here.
--count will done on this only ,apply group by on this subquery result only
select sell_date,count(product) as num_sold from (select distinct sell_date,product from Activities)
group by sell_date order by sell_date;
--2020-05-30 3
--2020-06-01 2
--2020-06-02 1 --here we have one only.
--syntax of listagg
--List Concatenation aggregates a column or expression by combining the valuesinto a single string for each group.
--A string to delimit each value (either blank or a comma when omitted) and the order of the valuesin the result can be
--specified.
SELECT ColumnA,LISTAGG(ColumnB, ',') WITHIN GROUP (ORDER BY ColumnB) AS ColumnBs
FROM TableName GROUP BY ColumnA;
--*********************************************************************************************************************
/*Write an SQL query to report the patient_id, patient_name and conditions of the patients who have Type I Diabetes.
Type I Diabetes always starts with DIAB1 prefix.
Return the result table in any order.
The query result format is in the following example.
Patients table:
+------------+--------------+--------------+
| patient_id | patient_name | conditions |
+------------+--------------+--------------+
| 1 | Daniel | YFEV COUGH |
file:///C:/Users/RATNESH/Downloads/Telegram%20Desktop/leetcodesbasic 12-Jan-23
Page 5 of 6
| 2 | Alice | |
| 3 | Bob | DIAB100 MYOP |
| 4 | George | ACNE DIAB100 |
| 5 | Alain | DIAB201 |
+------------+--------------+--------------+
Output:
+------------+--------------+--------------+
| patient_id | patient_name | conditions |
+------------+--------------+--------------+
| 3 | Bob | DIAB100 MYOP |
| 4 | George | ACNE DIAB100 |
+------------+--------------+--------------+
Explanation: Bob and George both have a condition that starts with DIAB1. */
select * from Patients where regexp_like(conditions, '(\s+|^)DIAB1');
select * from Patients where conditions like 'DIAB1%' or conditions like '% DIAB1%';
--**********************************************************************************************************************
Create table Employ1 (employee_id int, name varchar(30));
Create table Salaries1 (employee_id int, salary int);
insert into Employ1 (employee_id, name) values ('2', 'Crew');
insert into Employ1 (employee_id, name) values ('4', 'Haven');
insert into Employ1 (employee_id, name) values ('5', 'Kristian');
insert into Salaries1 (employee_id, salary) values ('5', '76071');
insert into Salaries1 (employee_id, salary) values ('1', '22517');
insert into Salaries1 (employee_id, salary) values ('4', '63539');
/*
Write an SQL query to report the IDs of all the employees with missing information. The information of an employee is
missing if:The employee's name is missing, orThe employee's salary is missing.Return the result table ordered by employee_id in ascending order.
The query result format is in the following example.
Employees table:
+-------------+----------+
| employee_id | name |
+-------------+----------+
| 2 | Crew |
| 4 | Haven |
| 5 | Kristian |
+-------------+----------+
Salaries table:
+-------------+--------+
| employee_id | salary |
+-------------+--------+
| 5 | 76071 |
| 1 | 22517 |
| 4 | 63539 |
+-------------+--------+
Output:
+-------------+
| employee_id |
+-------------+
| 1 |
| 2 |
+-------------+
Explanation:
Employees 1, 2, 4, and 5 are working at this company.The name of employee 1 is missing.The salary of employee 2 is missing.
*/
--by union and not in concept we did.
select employee_id from Employ1 where employee_id not in (select employee_id from Salaries1)
union
select employee_id from Salaries1 where employee_id not in (select employee_id from Employ1);
--********************************************************************************************************************
Create table Products2 (product_id int, store1 int, store2 int, store3 int);
insert into Products2 (product_id, store1, store2, store3) values ('0', '95', '100', '105');
insert into Products2 (product_id, store1, store2, store3) values ('1', '70', NULL, '80');
select * from Products2;
/*Write an SQL query to rearrange the Products table so that each row has (product_id, store, price).
If a product is not available in a store, do not include a row with that product_id and store combination in the result table.
Return the result table in any order.
Products table:
+------------+--------+--------+--------+
| product_id | store1 | store2 | store3 |
+------------+--------+--------+--------+
| 0 | 95 | 100 | 105 |
| 1 | 70 | null | 80 |
+------------+--------+--------+--------+
Output:
+------------+--------+-------+
| product_id | store | price |
+------------+--------+-------+
| 0 | store1 | 95 |
| 0 | store2 | 100 |
file:///C:/Users/RATNESH/Downloads/Telegram%20Desktop/leetcodesbasic 12-Jan-23
Page 6 of 6
| 0 | store3 | 105 |
| 1 | store1 | 70 |
| 1 | store3 | 80 |
+------------+--------+-------+
Explanation:
Product 0 is available in all three stores with prices 95, 100, and 105 respectively.
Product 1 is available in store1 with price 70 and store3 with price 80. The product is not available in store2.*/
SELECT product_id, 'store1' AS store, store1 AS price FROM Products2 WHERE store1 IS NOT NULL;
SELECT product_id, 'store2' AS store, store2 AS price FROM Products2 WHERE store2 IS NOT NULL;
SELECT product_id, 'store3' AS store, store3 AS price FROM Products2 WHERE store3 IS NOT NULL;
SELECT product_id, 'store1' AS store, store1 AS price FROM Products2 WHERE store1 IS NOT NULL
union
SELECT product_id, 'store2' AS store, store2 AS price FROM Products2 WHERE store2 IS NOT NULL
union
SELECT product_id, 'store3' AS store, store3 AS price FROM Products2 WHERE store3 IS NOT NULL
order by product_id;
file:///C:/Users/RATNESH/Downloads/Telegram%20Desktop/leetcodesbasic 12-Jan-23