SQL Hacks - Day 1
- supriyamalla
- Aug 16, 2022
- 1 min read
Hey all,
I have started SQL challenge on Leetcode and IT IS SO MUCH FUN!
10/10 recommend to anyone who is starting their datascience journey to do more Leetcode.
I learnt a new thing today in SQL - if condition!
I know about if condition but using it in the query was new to me.
Here's the link to w3school resource if you want to learn: https://www.w3schools.com/sql/func_mysql_if.asp
This could also be used to swap values in a table. For example, in one Leetcode question, I had to swap sex values (m-> f and f->m), here's how I did it:
UPDATE Salary
SET sex = if(sex="m", "f", "m")
Next, I studied String Functions in SQL!
In leetcode question, I had to convert a string to consistent camel case.
There are only two functions - upper and lower which is available. So, I applied the same methodology as to what I would have applied while doing in Excel with a twist.
I basically concatenated two substrings which i extracted from "name" - 1 was the 1st letter of the "name" and the other was remaining characters from the "name". I then individually applied "upper" and "lower" and concatenated both such strings.
Okay, i think we are good for today! Solved 5 questions today, yay!
Comments