Posts

Showing posts from April, 2021

Basic Python Functions

 Rock those LeetCode easies with these basic Python functions 😛 Lists list.insert(<index>, <elem>) list.remove(<elem>) #removes first matching occurrence of element list.append(elem) list.extend(<another list>) #concatenates two lists (or a list and any iterable) list.sort() #check below to see more on sorting list.reverse() #this acts in place, returns none list.pop(<index>) x = list.pop(index) # returns popped value x = list.index(index) x = list.count(elem) Check if Val in List <elem> in list Clone a List x = list(<list>) Lists & Tuples x = tuple(<list>) y = list(<tuple>) String/List Manipulation Split String by Delimiter s tring.split(<separator, default whitespace>) txt = “hello how are you” x = txt.split() # this will output: [‘hello’, ‘how’, ‘are’, ‘you’] Split String by Character list(<string>) list(nice) # this will output [‘n’,’i’,’c’,’e’] Join a List into a String separator.join(<iterable>) numL...

Formatting SQL Queries

How I try to format my queries for better legibility (doesn't always happen though..)  Notes, tips always welcome. Basic rules: indent and new line whenever applicable (make sure they align) indent on: after a select statement, 2nd+ conditional, after a bracket lowercase for syntax and aliases you make, uppercase for original column/table names leading commas in select statements initiate where/joins with 1=1 if complex conditions temp tables > subqueries unless the subquery is basic comment often ---------------------------------------------------------------------------------- /* Name: Example Query Sections:     Section 1: Description     Section 2: Description     Section 3: Description Dependencies:     Section 1:          -Table A          -Table B     Section 2:          -Table C     Section 3: Output:     Output table, if applic...

SQL Syntax Notes

An ever-growing list of SQL syntax tricks I've collected! Mostly applicable to Spark SQL. Please comment if you have any tips you'd like to share, or any corrections. Basic Functions to be Aware Of Conditions on Aggregate Values Having: select       CATEGORY,  sum(VALUE) as sumValues from TABLE where CATEGORY = 'a' having sumValues > 0 ; Window Functions Rank, Partition, Qualify select      rank() over (partition by USER_ID order by VALUE desc) as ranking from TABLE qualify  ranking = 1 ; Other window functions: row_num():  If you don't want ties like rank will give you, use a row num percent_rank(): Returns a value between 0 (lowest val) and 1 (highest val), if there are multiple highest vals, none of them will get a 1. Ties get the same percent_rank val. Querying Off json Objects For Spark SQL: get_json_object(JSON_COL_HERE, '$.JSON_PATH_HERE') like '%3%' Neat Techniques Pivoting Tables w Case Statements Pivot and Sum a Numerical Va...