A hands-on series covering real queries, schema design, CSV imports, and core SQL concepts — built as you learn.
users Table — Basic Version
CREATE TABLE users( id SERIAL PRIMARY KEY, name VARCHAR(100), email VARCHAR(150), is_active BOOLEAN );
departments Table
CREATE TABLE departments ( id SERIAL PRIMARY KEY, name VARCHAR(100) UNIQUE NOT NULL );
UNIQUE on name ensures no duplicate department entries — useful with ON CONFLICT later.users Table with Constraints
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(150) UNIQUE NOT NULL, role VARCHAR(50) NOT NULL CHECK (role IN ('student','professor','admin')), department_id INT REFERENCES departments(id) ON DELETE SET NULL, metadata JSONB, is_active BOOLEAN DEFAULT TRUE, created_at TIMESTAMP DEFAULT NOW() );
JSONB for flexible metadata. ON DELETE SET NULL means removing a department won't delete users — their department_id just becomes NULL.pg_tables
SELECT * FROM pg_tables WHERE schemaname = 'public';
public schema — great for debugging your schema setup.DROP TABLE departments;
DROP TABLE is irreversible — it deletes the table and all its data permanently. Always double-check before running in production.INSERT INTO departments (name) VALUES ('Computer Science') ON CONFLICT (name) DO NOTHING;
ON CONFLICT DO NOTHING makes the insert idempotent — safe to run multiple times without errors or duplicates.INSERT INTO departments (id, name) VALUES (2, 'Mechanical Engineering'), (3, 'Electronics & Communication'), (4, 'Business Administration'), (5, 'Basic Sciences'), (6, 'Computer Science') ON CONFLICT (name) DO NOTHING;
SELECT * FROM users;
SELECT * and name your columns explicitly.courses Table
-- Task: Create a courses table with id, title, and fee -- Hint: Use NUMERIC(10, 2) for the fee column CREATE TABLE courses ( id SERIAL PRIMARY KEY, title VARCHAR(200) NOT NULL, fee NUMERIC(10,2) NOT NULL );
NUMERIC(10, 2) stores up to 10 digits total with exactly 2 decimal places — the standard choice for money. Never use FLOAT for currency!| # | Name | Role | Dept ID | Active |
|---|