SQL - Leetcode-03
Question 1
Write a 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.
SQL Schema
SUBSTRING
The SUBSTRING function allows you to extract a portion of a string, based on a starting position and a length.
Extract the first three characters of each name:
QUESTION 2
Write a query to find for each date the number of different products sold and their names.
The sold products names for each date should be sorted lexicographically.
Return the result table ordered by sell_date.
SQL Schema
Solution
To aggregate the product names in one cell, we can use GROUP_CONCAT
QUESTION 3
Write a 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.
SQL Schema
Solution
Since the condition is that it must always start with DIAB1, to check for
- DIAB1CCC (Should alwasy start with DIAB1), we use 'DIAB1%'
- ABCDIAB1ABC (Should not have any character in front of DIAB1), we use '% DIAB1%'