Skip to content

Instantly share code, notes, and snippets.

View kovid-r's full-sized avatar
🏠
Working from home

Kovid Rathee kovid-r

🏠
Working from home
View GitHub Profile
@kovid-r
kovid-r / cast_ex_implicit_casting_example.sql
Last active May 19, 2020 13:09
Implicit Casting Example
select * from cast_ex.table_1 where val_int = 1;
select * from cast_ex.table_1 where val_int = '1';
select * from cast_ex.table_1 where val_int_char = 1;
select * from cast_ex.table_1 where val_int_char = '1';
@kovid-r
kovid-r / cast_ex_sample_data.sql
Last active May 19, 2020 13:08
Sample Data for Casting in SQL
create database if not exists cast_ex;
create table if not exists cast_ex.table_1 (
val_int int(10) not null,
val_int_char varchar(10) not null,
val_str varchar(10) not null
);
insert into cast_ex.table_1
with recursive seq
as (
@kovid-r
kovid-r / cast_ex_special_case_example.sql
Last active May 19, 2020 12:31
Special Case Example for Casting
select a, b, a = b
from (
select 13 a, '13dfgs' b union all
select 13 a, '130dfgs' b union all
select 13 a, '13' b union all
select 13 a, '0013dfgs' b union all
select 13 a, ' 13dfgs' b union all
select 13 a, 'dfgs13'
) as t;
@kovid-r
kovid-r / cast_ex_string_to_date_example.sql
Last active May 19, 2020 12:43
String to Date Example
with dates
as
(
select '2019-12-12' as _date union all
select '2019/12/12' as _date union all
select '2019.12.12' as _date union all
select '2019_12_12' as _date union all
select '2019\12\12' as _date union all
select '2012#12#12' as _date union all
select '2019$12$12' as _date union all
@kovid-r
kovid-r / cast_ex_index_not_used_example.sql
Last active May 19, 2020 13:49
Index Not Used Casting Example
alter table cast_ex.table_1
add index idx_val_int (val_int),
add index idx_val_int_char(val_int_char);
explain select * from cast_ex.table_1 where val_int = '1';
explain select * from cast_ex.table_1 where val_int_char = 1;
explain select * from cast_ex.table_1 force index (idx_val_int_char) where val_int_char = 1;
@kovid-r
kovid-r / null_is_null.sql
Last active July 10, 2020 16:24
Playing Around with Nulls
select null is null null_is_null,
null is not null null_is_not_null,
1 = 0 1_0,
0 is null 0_is_null,
1 is null 1_is_null,
0 = 1 is null 0_1_is_null,
null = null null_equals_null,
null <> null null_not_equals_null,
null != null null_not_equals_null_2,
null < null null_less_than_null,
@kovid-r
kovid-r / defensive_coding_example_1.sql
Last active June 1, 2020 15:32
Defensive Coding SQL Example LIMIT
SELECT t1.*,
(SELECT t2.field_name
FROM TABLE_2 AS t2
WHERE t1.id = t2.id
LIMIT 1
)
FROM TABLE_1 AS t1
@kovid-r
kovid-r / defensive_coding_sql_alias.sql
Created June 1, 2020 15:38
Defensive Coding SQL Alias
SELECT city,
FROM TABLE_1 AS t1
LEFT JOIN TABLE_2 as t2
ON t1.id = t2.id;
@kovid-r
kovid-r / pyspark_cheatsheet_init.py
Last active October 11, 2022 04:49
PySpark Cheat Sheet Application Initialization
import pyspark
from pyspark import SparkContext
from pyspark.sql import SparkSession
from pyspark.sql import SQLContext
# create a SparkSession instance with the name moviedb with Hive support enabled
# https://spark.apache.org/docs/latest/sql-data-sources-hive-tables.html
spark = SparkSession.builder.appName("moviedb").enableHiveSupport().getOrCreate()
# create a SparkContext instance which allows the Spark Application to access
@kovid-r
kovid-r / pyspark_cheatsheet_read_csv.py
Created June 10, 2020 15:08
Reading CSV - PySpark Cheatsheet
# set the file_path variable in the beginning of the file
# or if your Spark application interacts with other applications, parameterize it
file_path = '/Users/kovid-r/datasets/moviedb/movies_metadata.csv'
# method 1 for reading a CSV file
df = spark.read.csv(file_path, header=True)
# method 2 for reading a CSV file
df = spark.read.format(csv_plugin).options(header='true', inferSchema='true').load(file_path)