Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Snowflake Database — A Beginner-Friendly Guide

 

Snowflake Database — A Beginner-Friendly Guide 


1. What is Snowflake?

Snowflake is a cloud-native data warehouse designed for storing, processing, and analyzing large-scale data.
Unlike traditional databases, Snowflake is:

  • Fully managed

  • Extremely fast and scalable

  • Designed only for cloud

  • Supports structured, semi-structured, and unstructured data

It runs on AWS, Azure, and GCP without you worrying about servers.


2. Why Snowflake Is Different

FeatureTraditional DB (MySQL/Postgres)Snowflake
Compute + StorageTied togetherFully separate
ScalingHard, slowInstant, elastic
PerformanceSlows with big dataOptimized for huge datasets
MaintenanceManual tuning requiredZero management
ConcurrencyLimitedNearly unlimited
Semi-StructuredBasic supportNative JSON, Parquet, Avro

3. How Snowflake Stores Data Internally

Snowflake stores all data in compressed, columnar micro-partitions (50–500 MB each).
Each partition includes metadata such as:

  • Min/max values

  • Statistics

  • Row count

This lets Snowflake skip entire chunks of data during queries → much faster.

Internally:

+-----------------------------------------------+ | Micro-Partition 1 | | - Columnar data | | - Compressed | | - Stats (min/max/values) | +-----------------------------------------------+ +-----------------------------------------------+ | Micro-Partition 2 | | ... | +-----------------------------------------------+ ⬇ Stored in cloud AWS S3 / Azure Blob / Google Cloud Storage

4. Snowflake Architecture (Text-Based Diagram)

The core of Snowflake is a 3-layer architecture.
Here is a clean text-based diagram:

┌────────────────────────────┐ │ Cloud Services Layer │ │-----------------------------│ │ Authentication & Security │ │ Metadata & Catalog │ │ Query Optimization │ │ Transaction Management │ └───────────▲────────────────┘ │ │ ┌─────────────┴───────────────┐ │ Compute Layer (VMs) │ │------------------------------│ │ Virtual Warehouses │ │ - Independent compute │ │ - Auto-scale / auto-pause │ │ - No contention │ └────────────▲─────────────────┘ │ │ ┌──────────────┴────────────────┐ │ Storage Layer │ │-------------------------------│ │ Micro-partitions (Columnar) │ │ Compression & Encryption │ │ Stored in Cloud Storage │ │ (S3 / Blob / GCS) │ └────────────────────────────────┘

Explanation of the layers:

1️⃣ Storage Layer

  • Data is stored in micro-partitions

  • Automatically compressed and optimized

  • Very cheap because it uses cloud object storage

2️⃣ Compute Layer

  • Queries run on Virtual Warehouses

  • You can create multiple warehouses for different workloads

  • Compute never affects other teams (no blocking)

3️⃣ Cloud Services Layer

  • Manages metadata

  • Handles SQL compilation

  • Provides security & governance

  • Controls access, optimization, caching


5. Why Snowflake is Special

✔ Separation of Storage and Compute

Scale storage and compute independently.

✔ Zero Maintenance

No indexing, partitioning, tuning, vacuuming.

✔ Time Travel

Look at data from 1–90 days in the past.

✔ Native Support for JSON/Parquet

Query JSON directly using SQL.

✔ Instant Elasticity

Warehouse can auto-scale within seconds.

✔ Global Data Sharing

Share live data without copying.


6. Advantages (Pros)

πŸ‘ 1. Extremely Fast

Columnar storage + micro partitions = lightning performance.

πŸ‘ 2. Highly Scalable

Run XS, S, M, L, XL warehouses depending on load.

πŸ‘ 3. Pay Only for What You Use

Pause compute → no extra charges.

πŸ‘ 4. Multi-Cloud Support

Move across AWS, Azure, GCP without rewrite.

πŸ‘ 5. Easy Integration

Kafka, Spark, Airflow, dbt, BI tools (Tableau/Looker).

πŸ‘ 6. Strong Security

Auto-encryption, IAM, SSO, MFA, roles.


7. Limitations (Cons)

πŸ‘Ž Not for OLTP

Snowflake is not good for small frequent transactions.

πŸ‘Ž Compute Cost Can Grow

If warehouses run 24/7 accidentally.

πŸ‘Ž Vendor Lock-In

Difficult to migrate out due to architecture.

πŸ‘Ž Not Very Good for Real-Time

Sub-second latency workloads need other systems.


8. Snowflake vs Other Databases

▶ Snowflake vs PostgreSQL

  • Postgres is for OLTP

  • Snowflake is for analytics

▶ Snowflake vs Redshift

  • Snowflake separates compute & storage

  • Redshift requires cluster resizing

▶ Snowflake vs BigQuery

  • Snowflake uses warehouse model

  • BigQuery is serverless “pay per query”


9. When to Use Snowflake

Use it when you need:
✔ Analytics
✔ BI dashboards
✔ ETL/ELT
✔ Data lakes
✔ Huge datasets (TB–PB)
✔ Multi-team workloads

Don’t use it for:
✘ Banking transactions
✘ Real-time transactional apps
✘ High-frequency small writes


10. Summary

Snowflake is a modern, cloud-native data warehouse that offers:

  • Micro-partition storage

  • Separation of compute & storage

  • Blazing-fast analytics

  • Zero management

  • Time-travel

  • Multi-cloud support

  • Massive parallelism

It is ideal for any company that needs fast analytics on large data without managing servers.

How to create Procedure in MySql?

1.Create a database and procedure

CREATE DATABASE `pretech`
DELIMITER $$
DROP PROCEDURE IF EXISTS `pretech`.`Helloworld` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Helloworld`()
BEGIN
 Select 'HELLO WORLD';
END $$
DELIMITER ;

2. Run it

CALL Helloworld()

3.Call procedure



image


Comparison of SQL Delete, Truncate and Drop


Delete

  1. It is a DML statement
  2. Rollback is possible after execution
  3. Triggers can fire out
  4. Where clause can use for condition
  5. Table structure will exists

Truncate

  1. It is a DDL statement
  2. Rollback not possible
  3. Trigger fire out not possible
  4. Cant apply where clause
  5. It is very fast compare to delete
  6. Table structure will exists

Drop

  1. Table structure will not exists 
  2. Roll back not possible

SQL Joins Simple examples

Here are some simple examples for SQL joins.(Inner,Outer,Full). I hope this will help to understand the basics of SQL joins.

Tables Used

Below two tables used to describe the queries (pretechEmployee and pretechDepartment)

clip_image002

clip_image004

INNER JOIN

Inner join is using the fetch the common records from both the tables, here i want to fetch the records which are there in the both employee and department table.

clip_image006

SELECT * FROM pretechEmployee e1 INNER JOIN pretechDepartment d1 ON e1.emp_id=d1.emp_id

clip_image008

 

LEFT OUTER JOIN

As its names says it is using to fetch all records of employee table(left) and fetch the department details of employees

clip_image010

SELECT * FROM pretechEmployee e1 LEFT OUTER JOIN pretechDepartment d1 ON e1.emp_id=d1.emp_id

clip_image012

See one scenario, I want the employee details who is not under any department see below query.

clip_image014

SELECT * FROM pretechEmployee e1 LEFT OUTER JOIN pretechDepartment d1 ON e1.emp_id=d1.emp_id WHERE d1.emp_id is null

clip_image016

 

RIGHT OUTER JOIN

Right outer join help us to fetch all records of department (Right) and the employee details who all are having the department.

clip_image018

SELECT * FROM pretechEmployee e1 RIGHT OUTER JOIN pretechDepartment d1 ON e1.emp_id=d1.emp_id

clip_image020

clip_image022

SELECT * FROM pretechEmployee e1 RIGHT OUTER JOIN pretechDepartment d1 ON e1.emp_id=d1.emp_id WHERE e1.emp_id is null

clip_image024

 

FULL OUTER JOIN

Full outer join will fetch all the records from employee and Departments.

clip_image026

SELECT * FROM pretechEmployee e1 FULL OUTER JOIN pretechDepartment d1 ON e1.emp_id=d1.emp_id

clip_image028

Below full outer join query will fetch all the records from employee and department except employees are in department

clip_image030

clip_image032

How to create Triggers in Oracle


List all triggers

SELECT * FROM USER_TRIGGERS

Create Trigger

CREATE TRIGGER TRI_PRETECHEMPLOYEE
BEFORE INSERT OR UPDATE ON PRETECHEMPLOYEE
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('Trigger started');
END;

Note: PRETECHEMPLOYEE table needs to be created.

Drop a trigger

DROP TRIGGER TRI_PRETECHEMPLOYEE

Please see this link to know more about Triggers Oracle Triggers

Confusion Matrix + Precision/Recall (Super Simple, With Examples)

  Confusion Matrix + Precision/Recall (Super Simple, With Examples) 1) Binary Classification Setup Binary classification means the model p...

Featured Posts