Introduction to Data Modeling in Snowflake
Nuno Rocha
Director of Engineering
ALTER TABLE students RENAME TO dim_students;
ALTER TABLE classes RENAME TO dim_classes;
ALTER TABLE schools RENAME TO dim_schools;
dim_date
table to store key fixed dates related to student enrollments in school:CREATE OR REPLACE TABLE dim_date (
date_id NUMBER(10,0) PRIMARY KEY,
year NUMBER(4,0),
semester VARCHAR(255)
);
CREATE OR REPLACE TABLE fact_enrollments (
enrollment_id NUMBER(10,0) PRIMARY KEY,
student_id NUMBER(10,0),
class_id NUMBER(10,0),
date_id NUMBER(10,0),
FOREIGN KEY (student_id) REFERENCES dim_students(student_id),
FOREIGN KEY (class_id) REFERENCES dim_classes(class_id),
FOREIGN KEY (date_id) REFERENCES dim_date(date_id)
);
SELECT name,
class_name
FROM fact_enrollments
JOIN dim_students -- Joining to get student names
ON fact_enrollments.student_id = dim_students.student_id
JOIN dim_classes -- Joining to get class names
ON fact_enrollments.class_id = dim_classes.class_id
JOIN dim_schools -- Joining to filter for the 'Science' school
ON dim_classes.school_id = dim_schools.school_id
JOIN dim_date -- Joining to restrict data to the year 2023
ON fact_enrollments.date_id = dim_date.date_id
WHERE dim_schools.school_name = 'Science'
AND dim_date.year = 2023;
ALTER TABLE
: SQL command used to modify the structure of an existing entityRENAME TO
: SQL command, used with ALTER TABLE
, to rename an entityJOIN ON
: SQL clause to combine rows from tables, based ON
a related columnWHERE
: SQL clause to filter records based on a set conditionAND
: Logical operator used with WHERE
clause to combine multiple conditions-- Modifying a entity
ALTER TABLE table_name
RENAME TO new_name;
-- Querying data from merged entities filtered by specific conditions
SELECT
table_name.column_name,
other_name.*
FROM table_name
JOIN other_table
ON table_name.FK = other_table.PK
WHERE column_name condition value
AND column_name condition value;
Introduction to Data Modeling in Snowflake