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_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_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_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_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 / 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 / tech_certifications_2020.csv
Last active June 28, 2020 14:19
Tech Certifications in 2020
Certification Authority Price Level
Cloud Practitioner AWS US$100 Beginner
Solutions Architect - Associate AWS US$150 Intermediate
SysOps Administrator - Associate AWS US$150 Intermediate
Developer - Associate AWS US$150 Intermediate
Solutions Architect - Professional AWS $300 Advanced
DevOps Engineer - Professional AWS $300 Advanced
Advanced Networking Specialty AWS $300 Specialty
Security Specialty AWS $300 Specialty
Machine Learning AWS $300 Specialty
@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 / weird_queries_0_1_evaluation.sql
Last active July 10, 2020 16:31
How integers are evaluated in SQL
/* Where evaluates the condition finally to a TRUE or FALSE */
SELECT 1 WHERE 1; -- returns one record
SELECT 1 WHERE 0; -- returns nothing
SELECT 1 WHERE 0.001; -- returns one record
/* Any non-zero number evaluates to TRUE and zero evaluates to FALSE */
SELECT 1 = TRUE; -- returns one record
SELECT 0 = FALSE; -- returns one record
SELECT 1 = FALSE; -- returns nothing
@kovid-r
kovid-r / weird_queries_handling_nulls.sql
Created July 10, 2020 16:35
Experimenting with Nulls in SQL
/* Let's look at how SQL handles NULLs */
SELECT 1 WHERE 1; -- returns one record
SELECT 1 WHERE NOT 1; -- returns nothing
/* You'd think that this applies everywhere, but it doesn't */
SELECT 1 WHERE NULL; -- returns nothing
SELECT 1 WHERE NOT NULL -- also returns nothing, what happened here?
/* What happened here? Check what these columns evaluate to */
SELECT 1, NOT 1; -- 1 evaluates to 1 and NOT 1 evaluates to 0