Databases | Tasks for Practical Class 12
Creating and using VIEWs in SQL
Variant 1: Online Bookstore
Scenario: You are managing the database for an online bookstore. The database tracks books, authors, customers, and their orders. You need to create several views to simplify common queries and restrict access to certain data.
Database Schema:
-- Authors Table
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
author_name VARCHAR(100) NOT NULL,
nationality VARCHAR(50)
);
-- Books Table
CREATE TABLE books (
book_id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT REFERENCES authors(author_id),
genre VARCHAR(50),
price NUMERIC(8, 2),
publication_year INT,
stock_count INT DEFAULT 0
);
-- Customers Table
CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
registration_date DATE DEFAULT CURRENT_DATE
);
-- Orders Table
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INT REFERENCES customers(customer_id),
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(20) DEFAULT 'Pending' -- e.g., Pending, Shipped, Delivered
);
-- Order_Items Table (Connects Orders and Books)
CREATE TABLE order_items (
order_item_id SERIAL PRIMARY KEY,
order_id INT REFERENCES orders(order_id),
book_id INT REFERENCES books(book_id),
quantity INT NOT NULL,
price_at_purchase NUMERIC(8, 2) NOT NULL -- Price when the order was placed
);
-- Sample Data
INSERT INTO authors (author_name, nationality) VALUES
('Jane Austen', 'British'),
('George Orwell', 'British'),
('Haruki Murakami', 'Japanese'),
('Leo Tolstoy', 'Russian');
INSERT INTO books (title, author_id, genre, price, publication_year, stock_count) VALUES
('Pride and Prejudice', 1, 'Romance', 12.99, 1813, 50),
('1984', 2, 'Dystopian', 10.50, 1949, 35),
('Norwegian Wood', 3, 'Fiction', 14.00, 1987, 20),
('War and Peace', 4, 'Historical Fiction', 19.95, 1869, 15),
('Emma', 1, 'Romance', 11.50, 1815, 40),
('Animal Farm', 2, 'Political Satire', 9.00, 1945, 60);
INSERT INTO customers (first_name, last_name, email, registration_date) VALUES
('Alice', ' Wonderland', 'alice.w@mail.com', '2023-01-10'),
('Bob', 'The Builder', 'bob.b@mail.com', '2023-02-15'),
('Charlie', 'Chaplin', 'charlie.c@mail.com', '2023-03-20');
INSERT INTO orders (customer_id, order_date, status) VALUES
(1, '2023-10-01 10:00:00', 'Shipped'),
(2, '2023-10-05 14:30:00', 'Pending'),
(1, '2023-10-06 09:15:00', 'Pending');
INSERT INTO order_items (order_id, book_id, quantity, price_at_purchase) VALUES
(1, 1, 1, 12.99), -- Alice buys Pride and Prejudice
(1, 2, 1, 10.50), -- Alice buys 1984
(2, 3, 2, 14.00), -- Bob buys 2 Norwegian Wood
(3, 6, 1, 9.00); -- Alice buys Animal Farm
Tasks:
- Create
book_catalogView: Create a view namedbook_catalogthat shows thebook_id,title,author_name, andpricefor all books. Join thebooksandauthorstables. - Create
customer_emailsView: Create a view namedcustomer_emailsshowing only thefirst_name,last_name, andemailof all customers. - Create
low_stock_alertView: Create a view namedlow_stock_alertthat lists thetitleandstock_countfor books with astock_countless than 20. - Create
pending_orders_summaryView: Create a view namedpending_orders_summarythat displays theorder_id,customer_id, andorder_datefor all orders with the status ‘Pending’. - Create
detailed_order_infoView: Create a view nameddetailed_order_infoshowingorder_id,order_date, customer’sfirst_nameandlast_name, booktitle,quantity, andprice_at_purchase. This will require joiningorders,customers,order_items, andbooks. - Create
author_book_countView: Create a view namedauthor_book_countthat shows eachauthor_nameand the total number of distinct book titles (COUNT(b.book_id)) they have in thebookstable. Group by author name. - Query
book_catalogView: Write a query to select all books from thebook_catalogview where the author is ‘George Orwell’. - Query
detailed_order_infoView: Write a query to find all entries in thedetailed_order_infoview related toorder_id1. - Modify
customer_emailsView: UseCREATE OR REPLACE VIEWto modify thecustomer_emailsview to also include theregistration_date. - Drop
low_stock_alertView: Remove thelow_stock_alertview from the database.
Variant 2: Project Management System
Scenario: You are responsible for a database that tracks projects, the tasks within those projects, employees, and their assignments to tasks. Views are needed to simplify reporting and access control.
Database Schema:
-- Employees Table
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
emp_name VARCHAR(100) NOT NULL,
emp_role VARCHAR(50), -- e.g., Developer, Manager, QA Tester
hire_date DATE
);
-- Projects Table
CREATE TABLE projects (
project_id SERIAL PRIMARY KEY,
project_name VARCHAR(150) NOT NULL UNIQUE,
start_date DATE,
deadline DATE,
budget NUMERIC(12, 2)
);
-- Tasks Table
CREATE TABLE tasks (
task_id SERIAL PRIMARY KEY,
project_id INT REFERENCES projects(project_id),
task_description TEXT NOT NULL,
status VARCHAR(20) DEFAULT 'To Do', -- e.g., To Do, In Progress, Done, Blocked
priority INT -- e.g., 1 (High), 2 (Medium), 3 (Low)
);
-- Assignments Table (Links Employees to Tasks)
CREATE TABLE assignments (
assignment_id SERIAL PRIMARY KEY,
task_id INT REFERENCES tasks(task_id),
employee_id INT REFERENCES employees(employee_id),
assigned_date DATE DEFAULT CURRENT_DATE,
hours_logged NUMERIC(5, 2) DEFAULT 0
);
-- Sample Data
INSERT INTO employees (emp_name, emp_role, hire_date) VALUES
('John Smith', 'Manager', '2020-05-15'),
('Alice Brown', 'Developer', '2021-08-20'),
('Bob White', 'Developer', '2021-09-01'),
('Carol Green', 'QA Tester', '2022-01-10');
INSERT INTO projects (project_name, start_date, deadline, budget) VALUES
('Website Redesign', '2023-09-01', '2024-03-01', 50000.00),
('Mobile App Launch', '2023-10-15', '2024-06-15', 75000.00),
('Internal CRM Update', '2023-11-01', '2024-02-01', 30000.00);
INSERT INTO tasks (project_id, task_description, status, priority) VALUES
(1, 'Define new site structure', 'Done', 1),
(1, 'Develop homepage mockups', 'In Progress', 1),
(1, 'Implement user authentication', 'To Do', 2),
(2, 'Setup development environment', 'Done', 1),
(2, 'Code core features', 'In Progress', 1),
(3, 'Analyze current CRM issues', 'In Progress', 2);
INSERT INTO assignments (task_id, employee_id, assigned_date, hours_logged) VALUES
(1, 1, '2023-09-05', 10.0), -- John managed task 1
(2, 2, '2023-09-10', 15.5), -- Alice working on task 2
(3, 3, '2023-09-15', 0.0), -- Bob assigned task 3
(4, 2, '2023-10-16', 8.0), -- Alice did task 4
(5, 3, '2023-10-20', 25.0), -- Bob working on task 5
(6, 1, '2023-11-02', 5.0); -- John working on task 6
Tasks:
- Create
active_projectsView: Create a view namedactive_projectsshowingproject_name,start_date, anddeadlinefor all projects where thedeadlineis in the future (or NULL, assuming NULL means ongoing indefinitely - for simplicity, let’s focus ondeadline > CURRENT_DATE). - Create
employee_rolesView: Create a view namedemployee_rolesshowing only theemp_nameandemp_rolefor all employees. - Create
high_priority_tasksView: Create a view namedhigh_priority_tasksthat lists thetask_descriptionandstatusfor tasks withpriority= 1. - Create
project_task_listView: Create a view namedproject_task_listthat displays theproject_nameand thetask_descriptionfor all tasks. Joinprojectsandtaskstables. - Create
employee_assignments_detailedView: Create a view namedemployee_assignments_detailedshowing theemp_name,project_name,task_description, andassigned_date. This requires joiningemployees,assignments,tasks, andprojects. - Create
project_task_status_countView: Create a view namedproject_task_status_countthat shows eachproject_nameand the count of tasks in eachstatus(e.g., ‘To Do’, ‘In Progress’, ‘Done’). Group byproject_nameandstatus. (Hint:COUNT(*)orCOUNT(t.task_id)). - Query
employee_rolesView: Write a query to select all employees from theemployee_rolesview who have the role ‘Developer’. - Query
employee_assignments_detailedView: Write a query to find all assignments in theemployee_assignments_detailedview for the ‘Website Redesign’ project, ordered byassigned_date. - Modify
active_projectsView: UseCREATE OR REPLACE VIEWto modify theactive_projectsview to also include thebudget. - Drop
high_priority_tasksView: Remove thehigh_priority_tasksview from the database.
Variant 3: University Course Enrollment
Scenario: You are managing a university database that stores information about departments, courses offered by these departments, students, and their enrollments in courses. Views are required to simplify access to commonly needed information.
Database Schema:
-- Departments Table
CREATE TABLE departments (
dept_id SERIAL PRIMARY KEY,
dept_name VARCHAR(100) NOT NULL UNIQUE,
building VARCHAR(50)
);
-- Courses Table
CREATE TABLE courses (
course_id SERIAL PRIMARY KEY,
course_code VARCHAR(10) NOT NULL UNIQUE, -- e.g., CS101, MATH203
course_title VARCHAR(150) NOT NULL,
dept_id INT REFERENCES departments(dept_id),
credits INT CHECK (credits > 0)
);
-- Students Table
CREATE TABLE students (
student_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
enrollment_year INT
);
-- Enrollments Table
CREATE TABLE enrollments (
enrollment_id SERIAL PRIMARY KEY,
student_id INT REFERENCES students(student_id),
course_id INT REFERENCES courses(course_id),
enrollment_date DATE DEFAULT CURRENT_DATE,
grade CHAR(1) -- e.g., A, B, C, D, F (or NULL if not graded yet)
);
-- Sample Data
INSERT INTO departments (dept_name, building) VALUES
('Computer Science', 'Engineering Hall'),
('Mathematics', 'Science Wing'),
('History', 'Arts Building');
INSERT INTO courses (course_code, course_title, dept_id, credits) VALUES
('CS101', 'Introduction to Programming', 1, 3),
('CS305', 'Databases', 1, 4),
('MATH201', 'Calculus I', 2, 4),
('HIST101', 'World History I', 3, 3),
('MATH202', 'Calculus II', 2, 4);
INSERT INTO students (first_name, last_name, email, enrollment_year) VALUES
('Peter', 'Pan', 'peter.p@uni.edu', 2022),
('Wendy', 'Darling', 'wendy.d@uni.edu', 2021),
('James', 'Hook', 'james.h@uni.edu', 2022),
('Mary', 'Poppins', 'mary.p@uni.edu', 2023);
INSERT INTO enrollments (student_id, course_id, enrollment_date, grade) VALUES
(1, 1, '2023-09-01', 'A'), -- Peter takes CS101
(1, 3, '2023-09-01', 'B'), -- Peter takes MATH201
(2, 1, '2023-09-01', 'B'), -- Wendy takes CS101
(2, 4, '2023-09-01', NULL), -- Wendy takes HIST101
(3, 1, '2023-09-02', 'C'), -- James takes CS101
(3, 2, '2023-09-02', NULL), -- James takes CS305 (Databases)
(1, 2, '2024-01-15', NULL); -- Peter enrolls in CS305 later
Tasks:
- Create
course_directoryView: Create a view namedcourse_directorythat showscourse_code,course_title,credits, and thedept_namefor all courses. Joincoursesanddepartments. - Create
student_contact_infoView: Create a view namedstudent_contact_infoshowing thestudent_id,first_name,last_name, andemailof all students. - Create
ungraded_enrollmentsView: Create a view namedungraded_enrollmentsthat lists theenrollment_id,student_id, andcourse_idfor all enrollments where thegradeis NULL. - Create
cs_coursesView: Create a view namedcs_coursesthat displays thecourse_codeandcourse_titlefor all courses offered by the ‘Computer Science’ department. - Create
student_enrollment_detailsView: Create a view namedstudent_enrollment_detailsshowing the student’sfirst_name,last_name, thecourse_code,course_title, and thegrade. This requires joiningstudents,enrollments, andcourses. - Create
department_course_countView: Create a view nameddepartment_course_countthat shows eachdept_nameand the total number of courses (COUNT(c.course_id)) offered by that department. Group by department name. - Query
course_directoryView: Write a query to select all courses from thecourse_directoryview that are worth 4 credits. - Query
student_enrollment_detailsView: Write a query to find all enrollments in thestudent_enrollment_detailsview for the student ‘Peter Pan’, ordered bycourse_code. - Modify
student_contact_infoView: UseCREATE OR REPLACE VIEWto modify thestudent_contact_infoview to also include theenrollment_year. - Drop
ungraded_enrollmentsView: Remove theungraded_enrollmentsview from the database.
Variant 4: Music Streaming Service
Scenario: You are designing the database for a music streaming service. The database stores information about artists, albums, tracks, users, and their playlists. You need to create views to simplify common queries for displaying music catalogs and user libraries.
Database Schema:
-- Artists Table
CREATE TABLE artists (
artist_id SERIAL PRIMARY KEY,
artist_name VARCHAR(150) NOT NULL UNIQUE,
country VARCHAR(50)
);
-- Albums Table
CREATE TABLE albums (
album_id SERIAL PRIMARY KEY,
album_title VARCHAR(200) NOT NULL,
artist_id INT REFERENCES artists(artist_id),
release_year INT,
genre VARCHAR(50)
);
-- Tracks Table
CREATE TABLE tracks (
track_id SERIAL PRIMARY KEY,
track_title VARCHAR(200) NOT NULL,
album_id INT REFERENCES albums(album_id),
duration_seconds INT CHECK (duration_seconds > 0),
track_number INT -- Position of the track within the album
);
-- Users Table
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) UNIQUE,
join_date DATE DEFAULT CURRENT_DATE
);
-- Playlists Table
CREATE TABLE playlists (
playlist_id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(user_id),
playlist_name VARCHAR(100) NOT NULL,
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_public BOOLEAN DEFAULT FALSE
);
-- Playlist_Tracks Table (Associative table linking playlists and tracks)
CREATE TABLE playlist_tracks (
playlist_track_id SERIAL PRIMARY KEY,
playlist_id INT REFERENCES playlists(playlist_id),
track_id INT REFERENCES tracks(track_id),
added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(playlist_id, track_id) -- Prevent adding the same track twice to one playlist
);
-- Sample Data
INSERT INTO artists (artist_name, country) VALUES
('Queen', 'UK'),
('Daft Punk', 'France'),
('Led Zeppelin', 'UK'),
('Taylor Swift', 'USA');
INSERT INTO albums (album_title, artist_id, release_year, genre) VALUES
('A Night at the Opera', 1, 1975, 'Rock'),
('Discovery', 2, 2001, 'Electronic'),
('Led Zeppelin IV', 3, 1971, 'Hard Rock'),
('1989', 4, 2014, 'Pop'),
('News of the World', 1, 1977, 'Rock');
INSERT INTO tracks (track_title, album_id, duration_seconds, track_number) VALUES
('Bohemian Rhapsody', 1, 355, 1),
('You''re My Best Friend', 1, 171, 4),
('One More Time', 2, 320, 1),
('Digital Love', 2, 301, 3),
('Stairway to Heaven', 3, 482, 4),
('Black Dog', 3, 295, 1),
('We Will Rock You', 5, 121, 1),
('We Are The Champions', 5, 177, 2),
('Shake It Off', 4, 219, 6);
INSERT INTO users (username, email, join_date) VALUES
('musicfan1', 'fan1@email.com', '2023-01-15'),
('rocklover', 'rock@email.com', '2023-03-22'),
('djcool', 'dj@email.com', '2023-05-10');
INSERT INTO playlists (user_id, playlist_name, is_public) VALUES
(1, 'Workout Mix', TRUE),
(2, 'Classic Rock Anthems', TRUE),
(1, 'Chill Vibes', FALSE);
INSERT INTO playlist_tracks (playlist_id, track_id, added_date) VALUES
(1, 3, '2023-06-01 10:00:00'), -- Workout Mix: One More Time
(1, 7, '2023-06-01 10:01:00'), -- Workout Mix: We Will Rock You
(1, 9, '2023-06-01 10:02:00'), -- Workout Mix: Shake It Off
(2, 1, '2023-06-05 15:30:00'), -- Classic Rock: Bohemian Rhapsody
(2, 5, '2023-06-05 15:31:00'), -- Classic Rock: Stairway to Heaven
(2, 6, '2023-06-05 15:32:00'), -- Classic Rock: Black Dog
(3, 4, '2023-07-11 09:00:00'); -- Chill Vibes: Digital Love
Tasks:
- Create
full_track_infoView: Create a view namedfull_track_infoshowingtrack_title,artist_name,album_title,release_year, andduration_seconds. This requires joiningtracks,albums, andartists. - Create
user_summaryView: Create a view nameduser_summaryshowing only theusernameandjoin_datefor all users. - Create
long_tracksView: Create a view namedlong_trackslisting thetrack_titleandduration_secondsfor all tracks longer than 300 seconds (5 minutes). - Create
public_playlists_overviewView: Create a view namedpublic_playlists_overviewthat displays theplaylist_name, theusernameof the creator, and thecreation_datefor all playlists whereis_publicis TRUE. Joinplaylistsandusers. - Create
playlist_detailsView: Create a view namedplaylist_detailsshowingplaylist_name,track_title,artist_name, and theadded_datefor tracks within playlists. This involves joiningplaylists,playlist_tracks,tracks,albums, andartists. - Create
artist_album_countView: Create a view namedartist_album_countthat shows eachartist_nameand the total number of albums (COUNT(al.album_id)) they have in thealbumstable. Group by artist name. - Query
full_track_infoView: Write a query to select all track information from thefull_track_infoview for the artist ‘Queen’. - Query
playlist_detailsView: Write a query to find all tracks in theplaylist_detailsview belonging to the playlist named ‘Workout Mix’, ordered byadded_date. - Modify
user_summaryView: UseCREATE OR REPLACE VIEWto modify theuser_summaryview to also include the user’semail. - Drop
long_tracksView: Remove thelong_tracksview from the database.
Variant 5: Airline Flight System
Scenario: You manage the database for an airline flight system. It tracks airports, airlines, flight schedules, passengers, and their bookings. Views are essential for generating flight information displays and passenger lists efficiently.
Database Schema:
-- Airports Table
CREATE TABLE airports (
airport_code CHAR(3) PRIMARY KEY, -- e.g., LHR, JFK, CDG
airport_name VARCHAR(100) NOT NULL,
city VARCHAR(50),
country VARCHAR(50)
);
-- Airlines Table
CREATE TABLE airlines (
airline_id SERIAL PRIMARY KEY,
airline_name VARCHAR(100) NOT NULL UNIQUE,
iata_code CHAR(2) UNIQUE -- e.g., BA, AA, AF
);
-- Flights Table
CREATE TABLE flights (
flight_id SERIAL PRIMARY KEY,
flight_number VARCHAR(10) NOT NULL, -- e.g., BA215, AA100
airline_id INT REFERENCES airlines(airline_id),
origin_airport CHAR(3) REFERENCES airports(airport_code),
destination_airport CHAR(3) REFERENCES airports(airport_code),
departure_time TIMESTAMP,
arrival_time TIMESTAMP,
status VARCHAR(20) DEFAULT 'Scheduled' -- e.g., Scheduled, Delayed, Cancelled, Departed, Arrived
);
-- Passengers Table
CREATE TABLE passengers (
passenger_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
passport_number VARCHAR(20) UNIQUE,
nationality VARCHAR(50)
);
-- Bookings Table
CREATE TABLE bookings (
booking_id SERIAL PRIMARY KEY,
flight_id INT REFERENCES flights(flight_id),
passenger_id INT REFERENCES passengers(passenger_id),
booking_date DATE DEFAULT CURRENT_DATE,
seat_number VARCHAR(4) -- e.g., 12A, 30F
);
-- Sample Data
INSERT INTO airports (airport_code, airport_name, city, country) VALUES
('LHR', 'Heathrow Airport', 'London', 'UK'),
('JFK', 'John F. Kennedy International Airport', 'New York', 'USA'),
('CDG', 'Charles de Gaulle Airport', 'Paris', 'France'),
('LAX', 'Los Angeles International Airport', 'Los Angeles', 'USA');
INSERT INTO airlines (airline_name, iata_code) VALUES
('British Airways', 'BA'),
('American Airlines', 'AA'),
('Air France', 'AF');
INSERT INTO flights (flight_number, airline_id, origin_airport, destination_airport, departure_time, arrival_time, status) VALUES
('BA175', 1, 'LHR', 'JFK', '2024-05-10 10:00:00', '2024-05-10 13:00:00', 'Scheduled'),
('AA101', 2, 'JFK', 'LAX', '2024-05-10 15:00:00', '2024-05-10 18:30:00', 'Scheduled'),
('AF008', 3, 'CDG', 'JFK', '2024-05-11 09:00:00', '2024-05-11 11:30:00', 'Scheduled'),
('BA287', 1, 'LHR', 'LAX', '2024-05-11 14:00:00', '2024-05-11 17:15:00', 'Delayed');
INSERT INTO passengers (first_name, last_name, passport_number, nationality) VALUES
('Clark', 'Kent', 'US123456', 'USA'),
('Lois', 'Lane', 'US654321', 'USA'),
('Bruce', 'Wayne', 'UK987654', 'UK'),
('Diana', 'Prince', 'FR112233', 'France');
INSERT INTO bookings (flight_id, passenger_id, booking_date, seat_number) VALUES
(1, 1, '2024-04-01', '10A'), -- Clark on BA175
(1, 2, '2024-04-02', '10B'), -- Lois on BA175
(2, 1, '2024-04-05', '22F'), -- Clark on AA101
(3, 4, '2024-04-10', '05C'), -- Diana on AF008
(4, 3, '2024-04-15', '01A'); -- Bruce on BA287 (delayed)
Tasks:
- Create
flight_schedule_detailedView: Create a view namedflight_schedule_detailedshowingflight_number,airline_name, origin airport name (orig_airport_name), destination airport name (dest_airport_name),departure_time, andarrival_time. This requires joiningflights,airlines, andairports(twice, using aliases). - Create
passenger_listView: Create a view namedpassenger_listshowing only thefirst_name,last_name, andnationalityof all passengers. - Create
delayed_flightsView: Create a view nameddelayed_flightsthat lists theflight_number,origin_airport, anddestination_airportfor flights with the status ‘Delayed’. - Create
jfk_departuresView: Create a view namedjfk_departuresdisplaying theflight_number,destination_airport, anddeparture_timefor all flights originating from ‘JFK’. - Create
booking_manifestView: Create a view namedbooking_manifestshowingflight_number,departure_time, passengerfirst_nameandlast_name, andseat_number. Joinbookings,passengers, andflights. - Create
airline_flight_countView: Create a view namedairline_flight_countthat shows eachairline_nameand the total number of flights (COUNT(f.flight_id)) associated with that airline in theflightstable. Group by airline name. - Query
flight_schedule_detailedView: Write a query to select all flights from theflight_schedule_detailedview operated by ‘British Airways’. - Query
booking_manifestView: Write a query to find all passenger bookings listed in thebooking_manifestview for flightBA175. - Modify
passenger_listView: UseCREATE OR REPLACE VIEWto modify thepassenger_listview to also include thepassport_number. - Drop
delayed_flightsView: Remove thedelayed_flightsview from the database.
Variant 6: E-commerce Product Inventory
Scenario: You are managing the database for an e-commerce platform’s inventory system. The database tracks product categories, suppliers, products, warehouses, and the stock levels of each product in different warehouses. Views are needed to provide easy access to catalog information and stock levels.
Database Schema:
-- Suppliers Table
CREATE TABLE suppliers (
supplier_id SERIAL PRIMARY KEY,
supplier_name VARCHAR(150) NOT NULL UNIQUE,
contact_email VARCHAR(100),
country VARCHAR(50)
);
-- Categories Table
CREATE TABLE categories (
category_id SERIAL PRIMARY KEY,
category_name VARCHAR(100) NOT NULL UNIQUE,
description TEXT
);
-- Products Table
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
product_name VARCHAR(200) NOT NULL,
sku VARCHAR(50) UNIQUE, -- Stock Keeping Unit
category_id INT REFERENCES categories(category_id),
supplier_id INT REFERENCES suppliers(supplier_id),
unit_price NUMERIC(10, 2) CHECK (unit_price >= 0)
);
-- Warehouses Table
CREATE TABLE warehouses (
warehouse_id SERIAL PRIMARY KEY,
warehouse_name VARCHAR(100) NOT NULL,
location_city VARCHAR(50),
capacity_sqm INT -- Square meters
);
-- Inventory Table (Stock levels of products in warehouses)
CREATE TABLE inventory (
inventory_id SERIAL PRIMARY KEY,
product_id INT REFERENCES products(product_id),
warehouse_id INT REFERENCES warehouses(warehouse_id),
quantity_on_hand INT NOT NULL DEFAULT 0 CHECK (quantity_on_hand >= 0),
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(product_id, warehouse_id) -- Ensure only one entry per product per warehouse
);
-- Sample Data
INSERT INTO suppliers (supplier_name, contact_email, country) VALUES
('TechGadgets Inc.', 'sales@techgadgets.com', 'USA'),
('Office Supplies Co.', 'contact@officesupplies.co', 'Canada'),
('Home Decor Ltd.', 'info@homedecor.ltd', 'UK');
INSERT INTO categories (category_name, description) VALUES
('Electronics', 'Consumer electronics and accessories'),
('Office', 'Stationery, furniture, and equipment for offices'),
('Home Goods', 'Items for household use and decoration');
INSERT INTO products (product_name, sku, category_id, supplier_id, unit_price) VALUES
('Wireless Mouse', 'WM-101', 1, 1, 25.50),
('Mechanical Keyboard', 'MK-205', 1, 1, 75.00),
('Stapler (Heavy Duty)', 'STP-HD-50', 2, 2, 15.99),
('Desk Lamp (LED)', 'DL-LED-01', 2, 2, 35.00),
('Throw Pillow (Velvet)', 'TP-VL-BLUE', 3, 3, 19.95),
('Wall Clock (Modern)', 'WC-MOD-003', 3, 3, 45.00);
INSERT INTO warehouses (warehouse_name, location_city, capacity_sqm) VALUES
('Main Distribution Center', 'Chicago', 10000),
('West Coast Hub', 'Los Angeles', 5000),
('East Coast Depot', 'New York', 6000);
INSERT INTO inventory (product_id, warehouse_id, quantity_on_hand) VALUES
(1, 1, 500), -- Wireless Mouse in Main DC
(1, 2, 200), -- Wireless Mouse in West Coast Hub
(2, 1, 150), -- Keyboard in Main DC
(3, 1, 300), -- Stapler in Main DC
(3, 3, 100), -- Stapler in East Coast Depot
(4, 2, 50), -- Desk Lamp in West Coast Hub
(5, 1, 80), -- Throw Pillow in Main DC
(6, 3, 40); -- Wall Clock in East Coast Depot
Tasks:
- Create
full_product_catalogView: Create a view namedfull_product_catalogshowingproduct_name,sku,category_name,supplier_name, andunit_price. This requires joiningproducts,categories, andsuppliers. - Create
supplier_contactsView: Create a view namedsupplier_contactsshowing only thesupplier_nameandcontact_emailfor all suppliers. - Create
low_stock_itemsView: Create a view namedlow_stock_itemslisting theproduct_id,warehouse_id, andquantity_on_handfor all inventory records wherequantity_on_handis less than 50. - Create
electronics_catalogView: Create a view namedelectronics_catalogdisplaying theproduct_name,sku, andunit_pricefor all products belonging to the ‘Electronics’ category. - Create
warehouse_inventory_detailsView: Create a view namedwarehouse_inventory_detailsshowingwarehouse_name,location_city,product_name,sku, andquantity_on_hand. Joininventory,products, andwarehouses. - Create
category_product_countView: Create a view namedcategory_product_countthat shows eachcategory_nameand the total number of products (COUNT(p.product_id)) listed in that category. Group by category name. - Query
full_product_catalogView: Write a query to select all products from thefull_product_catalogview supplied by ‘TechGadgets Inc.’. - Query
warehouse_inventory_detailsView: Write a query to find all inventory details in thewarehouse_inventory_detailsview for the ‘Main Distribution Center’, ordered byproduct_name. - Modify
supplier_contactsView: UseCREATE OR REPLACE VIEWto modify thesupplier_contactsview to also include the supplier’scountry. - Drop
low_stock_itemsView: Remove thelow_stock_itemsview from the database.
Variant 7: Hospital Patient Records
Scenario: You are working with a database for a hospital clinic. The database stores information about doctors, patients, their appointments, and diagnoses made during those appointments. Creating views will help simplify common data retrieval tasks for administrative staff and medical professionals.
Database Schema:
-- Doctors Table
CREATE TABLE doctors (
doctor_id SERIAL PRIMARY KEY,
doc_name VARCHAR(100) NOT NULL,
specialization VARCHAR(100),
phone_number VARCHAR(20)
);
-- Patients Table
CREATE TABLE patients (
patient_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
date_of_birth DATE,
contact_number VARCHAR(20) UNIQUE
);
-- Appointments Table
CREATE TABLE appointments (
appointment_id SERIAL PRIMARY KEY,
patient_id INT REFERENCES patients(patient_id),
doctor_id INT REFERENCES doctors(doctor_id),
appointment_datetime TIMESTAMP NOT NULL,
reason_for_visit TEXT,
status VARCHAR(20) DEFAULT 'Scheduled' -- e.g., Scheduled, Completed, Cancelled, No-Show
);
-- Diagnoses Table
CREATE TABLE diagnoses (
diagnosis_id SERIAL PRIMARY KEY,
appointment_id INT REFERENCES appointments(appointment_id) UNIQUE, -- Assuming one primary diagnosis per appointment for simplicity
condition_name VARCHAR(150) NOT NULL,
diagnosis_date DATE DEFAULT CURRENT_DATE,
treatment_plan TEXT
);
-- Sample Data
INSERT INTO doctors (doc_name, specialization, phone_number) VALUES
('Dr. Evelyn Reed', 'Cardiology', '555-0101'),
('Dr. Samuel Green', 'Pediatrics', '555-0102'),
('Dr. Anya Sharma', 'General Medicine', '555-0103');
INSERT INTO patients (first_name, last_name, date_of_birth, contact_number) VALUES
('Michael', 'Jones', '1985-06-15', '555-0201'),
('Sarah', 'Chen', '1992-11-22', '555-0202'),
('David', 'Lee', '2018-03-10', '555-0203'),
('Laura', 'Martinez', '1970-01-30', '555-0204');
INSERT INTO appointments (patient_id, doctor_id, appointment_datetime, reason_for_visit, status) VALUES
(1, 1, '2023-10-20 09:00:00', 'Chest pain evaluation', 'Completed'),
(2, 3, '2023-10-21 11:30:00', 'Annual check-up', 'Completed'),
(3, 2, '2023-10-22 14:00:00', 'Vaccination', 'Completed'),
(1, 3, '2023-10-28 10:00:00', 'Follow-up consultation', 'Scheduled'),
(4, 1, '2023-11-05 15:00:00', 'Blood pressure check', 'Scheduled');
INSERT INTO diagnoses (appointment_id, condition_name, diagnosis_date, treatment_plan) VALUES
(1, 'Angina Pectoris', '2023-10-20', 'Prescribe medication, recommend lifestyle changes.'),
(2, 'Healthy', '2023-10-21', 'Continue healthy habits, return next year.'),
(3, 'Up-to-date Immunizations', '2023-10-22', 'No further action needed.');
Tasks:
- Create
patient_directoryView: Create a view namedpatient_directoryshowingpatient_id,first_name,last_name, andcontact_numberfor all patients. - Create
doctor_specializationsView: Create a view nameddoctor_specializationslistingdoc_nameandspecializationfor all doctors. - Create
upcoming_appointmentsView: Create a view namedupcoming_appointmentsthat displaysappointment_id, patient’s full name (concatenatedfirst_nameandlast_name),doc_name, andappointment_datetimefor all appointments with a status of ‘Scheduled’ andappointment_datetimein the future (use> CURRENT_TIMESTAMP). Requires joiningpatients,appointments, anddoctors. - Create
patient_diagnosis_historyView: Create a view namedpatient_diagnosis_historyshowing the patient’s full name,appointment_datetime,condition_name, andtreatment_plan. This requires joiningpatients,appointments, anddiagnoses. - Create
cardiology_patientsView: Create a view namedcardiology_patientsthat lists thefirst_name,last_name, andcontact_numberof patients who have had an appointment with a doctor whose specialization is ‘Cardiology’. Requires joiningpatients,appointments, anddoctors. UseDISTINCTto avoid duplicates if a patient saw the cardiologist multiple times. - Create
doctor_appointment_loadView: Create a view nameddoctor_appointment_loadshowingdoc_nameand the total count of ‘Completed’ appointments (COUNT(ap.appointment_id)) associated with each doctor. Group by doctor name. Include doctors even if they have 0 completed appointments (useLEFT JOINfromdoctorstoappointments). - Query
upcoming_appointmentsView: Write a query to select all upcoming appointments from theupcoming_appointmentsview scheduled with ‘Dr. Evelyn Reed’. - Query
patient_diagnosis_historyView: Write a query to find all diagnoses recorded for the patient ‘Michael Jones’ using thepatient_diagnosis_historyview, ordered byappointment_datetime. - Modify
patient_directoryView: UseCREATE OR REPLACE VIEWto modify thepatient_directoryview to also include thedate_of_birth. - Drop
doctor_specializationsView: Remove thedoctor_specializationsview from the database.
Variant 8: Rental Car Agency
Scenario: You manage the database for a car rental agency. The system tracks customers, the vehicle fleet, rental locations, and individual rental transactions. Views are needed to simplify reporting on vehicle availability, customer rentals, and location activity.
Database Schema:
-- Customers Table
CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
drivers_license_no VARCHAR(50) UNIQUE NOT NULL,
member_since DATE DEFAULT CURRENT_DATE
);
-- Locations Table
CREATE TABLE locations (
location_id SERIAL PRIMARY KEY,
location_name VARCHAR(100) NOT NULL, -- e.g., Airport Branch, Downtown Office
city VARCHAR(50),
address VARCHAR(255)
);
-- Vehicles Table
CREATE TABLE vehicles (
vehicle_id SERIAL PRIMARY KEY,
make VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
year INT,
license_plate VARCHAR(15) UNIQUE NOT NULL,
category VARCHAR(20), -- e.g., Sedan, SUV, Van, Luxury
current_location_id INT REFERENCES locations(location_id),
daily_rental_rate NUMERIC(6, 2)
);
-- Rentals Table
CREATE TABLE rentals (
rental_id SERIAL PRIMARY KEY,
vehicle_id INT REFERENCES vehicles(vehicle_id),
customer_id INT REFERENCES customers(customer_id),
pickup_location_id INT REFERENCES locations(location_id),
return_location_id INT REFERENCES locations(location_id), -- Can be different from pickup
pickup_datetime TIMESTAMP NOT NULL,
expected_return_datetime TIMESTAMP NOT NULL,
actual_return_datetime TIMESTAMP, -- NULL if currently rented out
total_amount NUMERIC(8, 2) -- Calculated upon return
);
-- Sample Data
INSERT INTO customers (first_name, last_name, email, drivers_license_no, member_since) VALUES
('Arthur', 'Dent', 'adent@galaxy.com', 'DL12345', '2022-01-10'),
('Ford', 'Prefect', 'fprefect@guide.com', 'DL67890', '2022-02-15'),
('Zaphod', 'Beeblebrox', 'zbeeblebrox@prez.gov', 'DL11111', '2023-03-20');
INSERT INTO locations (location_name, city, address) VALUES
('Airport Branch', 'Metropolis', '1 Airport Rd'),
('Downtown Office', 'Metropolis', '123 Main St'),
('Suburb Depot', 'Gotham', '45 Sidekick Ln');
INSERT INTO vehicles (make, model, year, license_plate, category, current_location_id, daily_rental_rate) VALUES
('Toyota', 'Camry', 2022, 'ABC-123', 'Sedan', 1, 55.00),
('Ford', 'Explorer', 2023, 'XYZ-789', 'SUV', 2, 75.00),
('Honda', 'Civic', 2021, 'DEF-456', 'Sedan', 1, 50.00),
('Chevrolet', 'Suburban', 2023, 'GHI-000', 'SUV', 3, 80.00);
INSERT INTO rentals (vehicle_id, customer_id, pickup_location_id, return_location_id, pickup_datetime, expected_return_datetime, actual_return_datetime, total_amount) VALUES
(1, 1, 1, 1, '2023-10-15 09:00:00', '2023-10-18 09:00:00', '2023-10-18 08:45:00', 165.00),
(2, 2, 2, 2, '2023-10-20 12:00:00', '2023-10-25 12:00:00', NULL, NULL), -- Currently rented
(3, 1, 1, 2, '2023-10-22 10:00:00', '2023-10-24 10:00:00', NULL, NULL); -- Currently rented
Tasks:
- Create
vehicle_inventoryView: Create a view namedvehicle_inventoryshowinglicense_plate,make,model,category,daily_rental_rate, and thelocation_nameof the vehicle’scurrent_location_id. Joinvehiclesandlocations. - Create
customer_detailsView: Create a view namedcustomer_detailsshowingcustomer_id, full name (concatenatedfirst_nameandlast_name),email, anddrivers_license_no. - Create
active_rentals_summaryView: Create a view namedactive_rentals_summarylisting therental_id, customer’s full name, vehicle’smakeandmodel, and theexpected_return_datetimefor all rentals whereactual_return_datetimeIS NULL. Requires joiningrentals,customers, andvehicles. - Create
available_vehicles_nowView: Create a view namedavailable_vehicles_nowshowinglicense_plate,make,model,category,daily_rental_rate, andlocation_namefor vehicles that are NOT currently rented out (i.e., theirvehicle_iddoes not appear in therentalstable with anactual_return_datetimethat IS NULL). You might need a subquery or a LEFT JOIN approach. - Create
rental_history_customerView: Create a view namedrental_history_customershowing customer’s full name, vehicle’smakeandmodel,pickup_datetime,actual_return_datetime, andtotal_amountfor completed rentals (actual_return_datetimeIS NOT NULL). Requires joiningrentals,customers, andvehicles. - Create
location_vehicle_countView: Create a view namedlocation_vehicle_countthat shows eachlocation_nameand the total number of vehicles (COUNT(v.vehicle_id)) currently assigned to that location (current_location_id). Group by location name. Include locations with 0 vehicles. - Query
available_vehicles_nowView: Write a query to select all available ‘SUV’ category vehicles from theavailable_vehicles_nowview located at the ‘Airport Branch’. - Query
active_rentals_summaryView: Write a query to find the active rental details for the customer ‘Arthur Dent’ using theactive_rentals_summaryview. - Modify
customer_detailsView: UseCREATE OR REPLACE VIEWto modify thecustomer_detailsview to also include themember_sincedate. - Drop
rental_history_customerView: Remove therental_history_customerview from the database.
Variant 9: Blog Platform
Scenario: You are the database administrator for a blog platform. The database stores information about users (authors), blog posts, categories for posts, and comments left by users on posts. You need to create views to simplify common queries for displaying blog content and managing users.
Database Schema:
-- Users Table
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) UNIQUE,
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Categories Table
CREATE TABLE categories (
category_id SERIAL PRIMARY KEY,
category_name VARCHAR(50) NOT NULL UNIQUE,
description TEXT
);
-- Posts Table
CREATE TABLE posts (
post_id SERIAL PRIMARY KEY,
author_id INT REFERENCES users(user_id),
category_id INT REFERENCES categories(category_id),
title VARCHAR(255) NOT NULL,
content TEXT,
published_datetime TIMESTAMP, -- NULL if draft
status VARCHAR(15) DEFAULT 'Draft' -- e.g., Draft, Published, Archived
);
-- Comments Table
CREATE TABLE comments (
comment_id SERIAL PRIMARY KEY,
post_id INT REFERENCES posts(post_id) ON DELETE CASCADE, -- Delete comments if post is deleted
commenter_id INT REFERENCES users(user_id), -- NULL for anonymous comments
commenter_name VARCHAR(50), -- Used if commenter_id is NULL
comment_text TEXT NOT NULL,
comment_datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Sample Data
INSERT INTO users (username, email, registration_date) VALUES
('john_doe', 'john.doe@email.com', '2023-01-15 10:00:00'),
('jane_smith', 'jane.s@email.com', '2023-02-20 11:00:00'),
('blog_admin', 'admin@blog.com', '2023-01-01 09:00:00');
INSERT INTO categories (category_name, description) VALUES
('Technology', 'Latest tech news and reviews'),
('Travel', 'Adventures around the world'),
('Food', 'Recipes and restaurant reviews');
INSERT INTO posts (author_id, category_id, title, content, published_datetime, status) VALUES
(1, 1, 'Understanding SQL Views', 'Views are virtual tables...', '2023-10-26 14:00:00', 'Published'),
(2, 2, 'Trip to the Mountains', 'The scenery was breathtaking...', '2023-11-01 09:30:00', 'Published'),
(1, 1, 'Introduction to Docker', 'Docker helps containerize apps...', NULL, 'Draft'),
(2, 3, 'Best Pizza Places', 'Reviewing local pizza joints...', '2023-11-05 16:00:00', 'Published');
INSERT INTO comments (post_id, commenter_id, commenter_name, comment_text, comment_datetime) VALUES
(1, 2, NULL, 'Great explanation, thanks!', '2023-10-26 15:00:00'), -- Jane comments on John's post
(1, NULL, 'Visitor', 'Very helpful article.', '2023-10-27 10:00:00'), -- Anonymous comment
(2, 1, NULL, 'Looks like an amazing trip!', '2023-11-01 11:00:00'); -- John comments on Jane's post
Tasks:
- Create
published_posts_feedView: Create a view namedpublished_posts_feedthat showspost_id,title, author’susername,category_name, andpublished_datetimefor all posts with status ‘Published’. Order bypublished_datetimedescending. Requires joiningposts,users, andcategories. - Create
user_profilesView: Create a view nameduser_profilesshowinguser_id,username, andemailfor all registered users. - Create
draft_posts_listView: Create a view nameddraft_posts_listthat lists thepost_id,title, and author’susernamefor all posts currently in ‘Draft’ status. Joinpostsandusers. - Create
post_comment_summaryView: Create a view namedpost_comment_summaryshowingpost_id,title, and thecomment_text, along with the commenter’s identity (useCOALESCE(u.username, c.commenter_name, 'Anonymous') AS commenter_display_name). Requires joiningposts,comments, and optionallyusers(useLEFT JOINfor users). - Create
category_overviewView: Create a view namedcategory_overviewthat lists thecategory_nameand itsdescription. - Create
author_post_statisticsView: Create a view namedauthor_post_statisticsshowing each author’susernameand the total count of posts (COUNT(p.post_id)) they have created (regardless of status). Group by username. Include authors with 0 posts (useLEFT JOINfromuserstoposts). - Query
published_posts_feedView: Write a query to select posts from thepublished_posts_feedview belonging to the ‘Technology’ category. - Query
post_comment_summaryView: Write a query to find all comments associated with the post titled ‘Understanding SQL Views’ using thepost_comment_summaryview. - Modify
user_profilesView: UseCREATE OR REPLACE VIEWto modify theuser_profilesview to also include theregistration_date. - Drop
draft_posts_listView: Remove thedraft_posts_listview from the database.
Variant 10: Library System
Scenario: You are developing the database backend for a public library. The system needs to track books, authors, library members, and book loans. Creating views will help librarians easily access common information and generate reports.
Database Schema:
-- Authors Table
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
author_name VARCHAR(100) NOT NULL,
birth_year INT
);
-- Books Table
CREATE TABLE books (
book_id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT REFERENCES authors(author_id),
isbn VARCHAR(20) UNIQUE,
genre VARCHAR(50),
publication_year INT,
total_copies INT DEFAULT 1,
available_copies INT DEFAULT 1 CHECK (available_copies <= total_copies AND available_copies >= 0)
);
-- Members Table
CREATE TABLE members (
member_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
join_date DATE DEFAULT CURRENT_DATE,
membership_status VARCHAR(15) DEFAULT 'Active' -- e.g., Active, Expired, Suspended
);
-- Loans Table
CREATE TABLE loans (
loan_id SERIAL PRIMARY KEY,
book_id INT REFERENCES books(book_id),
member_id INT REFERENCES members(member_id),
loan_date DATE DEFAULT CURRENT_DATE,
due_date DATE NOT NULL,
return_date DATE -- NULL means the book is still on loan
);
-- Sample Data
INSERT INTO authors (author_name, birth_year) VALUES
('J.R.R. Tolkien', 1892),
('Isaac Asimov', 1920),
('Ursula K. Le Guin', 1929),
('Frank Herbert', 1920);
INSERT INTO books (title, author_id, isbn, genre, publication_year, total_copies, available_copies) VALUES
('The Hobbit', 1, '978-0547928227', 'Fantasy', 1937, 5, 3),
('Foundation', 2, '978-0553293357', 'Science Fiction', 1951, 3, 1),
('A Wizard of Earthsea', 3, '978-0547773742', 'Fantasy', 1968, 4, 4),
('Dune', 4, '978-0441172719', 'Science Fiction', 1965, 2, 0),
('The Left Hand of Darkness', 3, '978-0441478125', 'Science Fiction', 1969, 3, 3);
INSERT INTO members (first_name, last_name, email, join_date, membership_status) VALUES
('Arthur Dent', 'Ford', 'a.dent@galaxy.net', '2022-08-15', 'Active'),
('Zaphod', 'Beeblebrox', 'z.beeblebrox@galaxy.net', '2023-01-20', 'Active'),
('Trillian', 'Astra', 't.astra@galaxy.net', '2022-08-15', 'Expired');
INSERT INTO loans (book_id, member_id, loan_date, due_date, return_date) VALUES
(1, 1, '2023-10-01', '2023-10-15', '2023-10-14'), -- The Hobbit, returned
(2, 1, '2023-10-10', '2023-10-24', NULL), -- Foundation, currently loaned
(1, 2, '2023-10-12', '2023-10-26', NULL), -- The Hobbit, currently loaned
(4, 2, '2023-09-15', '2023-09-29', NULL), -- Dune, overdue loan
(4, 1, '2023-08-01', '2023-08-15', '2023-08-14'); -- Dune, loaned and returned previously
Tasks:
- Create
available_books_listView: Create a view namedavailable_books_listshowing thebook_id,title,isbn, andauthor_namefor all books whereavailable_copiesis greater than 0. Joinbooksandauthors. - Create
active_member_contactsView: Create a view namedactive_member_contactsshowing themember_id,first_name,last_name, andemailfor members whosemembership_statusis ‘Active’. - Create
overdue_loans_alertView: Create a view namedoverdue_loans_alertthat lists theloan_id,book_id(you can join to get title if desired),member_id,loan_date, anddue_datefor all loans where thereturn_dateis NULL AND thedue_dateis before the current date (CURRENT_DATE). - Create
book_details_with_authorView: Create a simple view namedbook_details_with_authorjoiningbooksandauthorsto showbook_id,title,isbn,genre,publication_year, andauthor_name. -
Create member_loan_historyView: Create a view namedmember_loan_historyshowing member’s full name (first_name’ ‘ last_name), booktitle,loan_date,due_date, andreturn_date. This requires joiningmembers,loans, andbooks. - Create
author_book_inventoryView: Create a view namedauthor_book_inventoryshowing eachauthor_nameand the total number of book copies (SUM(b.total_copies)) associated with them in the library. Group byauthor_name. - Query
available_books_listView: Write a query to select all books fromavailable_books_listview within the ‘Fantasy’ genre. - Query
member_loan_historyView: Write a query to find all loan records inmember_loan_historyfor the member ‘Arthur Dent Ford’, showing only loans that have not yet been returned. - Modify
active_member_contactsView: UseCREATE OR REPLACE VIEWto modify theactive_member_contactsview to also include thejoin_date. - Drop
overdue_loans_alertView: Remove theoverdue_loans_alertview from the database.
Variant 11: Event Management System
Scenario: You’re building a database for a company that organizes professional events like conferences and workshops. The database tracks events, venues where they are held, attendees, and their registrations. Views will simplify common queries for event organizers and attendees.
Database Schema:
-- Venues Table
CREATE TABLE venues (
venue_id SERIAL PRIMARY KEY,
venue_name VARCHAR(150) NOT NULL,
address TEXT,
capacity INT CHECK (capacity > 0)
);
-- Events Table
CREATE TABLE events (
event_id SERIAL PRIMARY KEY,
event_name VARCHAR(200) NOT NULL,
venue_id INT REFERENCES venues(venue_id),
event_date TIMESTAMP NOT NULL, -- Date and time of the event
description TEXT,
category VARCHAR(50) -- e.g., Conference, Workshop, Networking, Webinar
);
-- Attendees Table
CREATE TABLE attendees (
attendee_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
company VARCHAR(100)
);
-- Registrations Table
CREATE TABLE registrations (
registration_id SERIAL PRIMARY KEY,
event_id INT REFERENCES events(event_id),
attendee_id INT REFERENCES attendees(attendee_id),
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
ticket_type VARCHAR(20) DEFAULT 'Standard', -- e.g., Standard, VIP, Early Bird
UNIQUE(event_id, attendee_id) -- Ensure an attendee registers only once per event
);
-- Sample Data
INSERT INTO venues (venue_name, address, capacity) VALUES
('Grand Conference Hall', '123 Main St, Cityville', 500),
('Tech Incubator Hub', '456 Tech Ave, Cityville', 150),
('Downtown Convention Center', '789 Market St, Cityville', 2000);
INSERT INTO events (event_name, venue_id, event_date, description, category) VALUES
('Future of AI Conference', 1, '2024-05-10 09:00:00', 'Exploring advancements in AI and ML.', 'Conference'),
('Web Development Workshop', 2, '2024-05-15 10:00:00', 'Hands-on workshop for modern web tech.', 'Workshop'),
('Digital Marketing Summit', 1, '2024-06-20 09:30:00', 'Strategies for online growth.', 'Conference'),
('Startup Networking Night', 2, '2024-06-25 18:00:00', 'Connect with local entrepreneurs.', 'Networking');
INSERT INTO attendees (first_name, last_name, email, company) VALUES
('Alice', 'Smith', 'alice.s@techcorp.com', 'TechCorp'),
('Bob', 'Johnson', 'bob.j@innovate.com', 'Innovate LLC'),
('Charlie', 'Davis', 'charlie.d@startup.io', 'Startup Inc.'),
('Diana', 'Miller', 'diana.m@freelance.com', NULL);
INSERT INTO registrations (event_id, attendee_id, registration_date, ticket_type) VALUES
(1, 1, '2024-03-01 11:00:00', 'Early Bird'), -- Alice for AI Conf
(1, 2, '2024-03-05 14:20:00', 'Standard'), -- Bob for AI Conf
(2, 1, '2024-03-10 09:00:00', 'Standard'), -- Alice for Web Workshop
(2, 3, '2024-03-11 16:45:00', 'Standard'), -- Charlie for Web Workshop
(3, 2, '2024-04-01 10:15:00', 'VIP'), -- Bob for Marketing Summit
(4, 4, '2024-05-01 12:00:00', 'Standard'); -- Diana for Networking
Tasks:
- Create
upcoming_events_scheduleView: Create a view namedupcoming_events_scheduleshowingevent_name,event_date,venue_name, andaddressfor all events scheduled for today or later (event_date >= CURRENT_DATE). Joineventsandvenues. -
Create attendee_directoryView: Create a view namedattendee_directoryshowingattendee_id, full name (first_name’ ‘ last_name),email, andcompanyfor all attendees. - Create
events_at_tech_hubView: Create a view namedevents_at_tech_hubthat lists theevent_name,category, andevent_datefor all events taking place at the ‘Tech Incubator Hub’ venue. - Create
conference_attendee_listView: Create a view namedconference_attendee_listshowing theevent_name, attendee’sfirst_name,last_name, andemailspecifically for events where thecategoryis ‘Conference’. Requires joiningregistrations,attendees, andevents. - Create
detailed_registration_reportView: Create a view nameddetailed_registration_reportshowingregistration_id,event_name,event_date, attendee’s full name, attendee’semail,registration_date, andticket_type. This requires joiningregistrations,events, andattendees. - Create
event_registration_countView: Create a view namedevent_registration_countshowing eachevent_nameand the total number of registered attendees (COUNT(r.attendee_id)). Group byevent_name. Include events with zero registrations if applicable (use appropriate JOIN). - Query
upcoming_events_scheduleView: Write a query to select events fromupcoming_events_schedulethat are happening at the ‘Grand Conference Hall’. - Query
detailed_registration_reportView: Write a query to find all registrations in thedetailed_registration_reportview for the ‘Future of AI Conference’ event, showing only those with ‘Early Bird’ticket_type. - Modify
attendee_directoryView: UseCREATE OR REPLACE VIEWto modify theattendee_directoryview to show email before the company name. - Drop
events_at_tech_hubView: Remove theevents_at_tech_hubview from the database.
Variant 12: Gym Membership System
Scenario: You are managing the database for a fitness center. The system tracks members, their membership types, fitness classes offered, trainers conducting classes, and member attendance. Views will help staff manage memberships, schedules, and track class popularity.
Database Schema:
-- MembershipTypes Table
CREATE TABLE membership_types (
type_id SERIAL PRIMARY KEY,
type_name VARCHAR(50) NOT NULL UNIQUE, -- e.g., Basic, Premium, Annual, Off-Peak
monthly_fee NUMERIC(6, 2) NOT NULL
);
-- Members Table
CREATE TABLE members (
member_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
join_date DATE DEFAULT CURRENT_DATE,
type_id INT REFERENCES membership_types(type_id),
status VARCHAR(15) DEFAULT 'Active' -- e.g., Active, Inactive, Frozen
);
-- Trainers Table
CREATE TABLE trainers (
trainer_id SERIAL PRIMARY KEY,
trainer_name VARCHAR(100) NOT NULL,
specialization VARCHAR(100) -- e.g., Yoga, Strength Training, Cardio
);
-- Classes Table
CREATE TABLE classes (
class_id SERIAL PRIMARY KEY,
class_name VARCHAR(100) NOT NULL,
trainer_id INT REFERENCES trainers(trainer_id),
schedule_day VARCHAR(10), -- e.g., Monday, Tuesday
schedule_time TIME,
duration_minutes INT DEFAULT 60,
max_capacity INT
);
-- AttendanceLog Table
CREATE TABLE attendance_log (
log_id SERIAL PRIMARY KEY,
member_id INT REFERENCES members(member_id),
class_id INT REFERENCES classes(class_id),
attendance_date DATE DEFAULT CURRENT_DATE -- Records the specific date the member attended
);
-- Sample Data
INSERT INTO membership_types (type_name, monthly_fee) VALUES
('Basic', 30.00),
('Premium', 50.00),
('Annual', 500.00); -- Let's simplify and assume Annual is paid upfront but track 'type'
INSERT INTO members (first_name, last_name, email, join_date, type_id, status) VALUES
('Clark', 'Kent', 'clark.k@dailyplanet.com', '2023-01-10', 2, 'Active'),
('Lois', 'Lane', 'lois.l@dailyplanet.com', '2023-01-10', 2, 'Active'),
('Bruce', 'Wayne', 'bruce.w@waynecorp.com', '2022-11-01', 3, 'Active'),
('Diana', 'Prince', 'diana.p@museum.org', '2023-03-15', 1, 'Frozen');
INSERT INTO trainers (trainer_name, specialization) VALUES
('John Doe', 'Strength Training'),
('Jane Smith', 'Yoga & Pilates'),
('Robert Paulson', 'Cardio & HIIT');
INSERT INTO classes (class_name, trainer_id, schedule_day, schedule_time, duration_minutes, max_capacity) VALUES
('Morning Yoga', 2, 'Monday', '08:00:00', 60, 20),
('Power Lifting', 1, 'Tuesday', '18:00:00', 75, 15),
('HIIT Blast', 3, 'Wednesday', '19:00:00', 45, 25),
('Evening Pilates', 2, 'Thursday', '18:30:00', 60, 20),
('Circuit Training', 1, 'Friday', '17:00:00', 60, 20);
INSERT INTO attendance_log (member_id, class_id, attendance_date) VALUES
(1, 1, '2023-10-02'), -- Clark attended Morning Yoga on Oct 2nd
(2, 1, '2023-10-02'), -- Lois attended Morning Yoga on Oct 2nd
(1, 2, '2023-10-03'), -- Clark attended Power Lifting on Oct 3rd
(3, 5, '2023-10-06'), -- Bruce attended Circuit Training on Oct 6th
(1, 1, '2023-10-09'); -- Clark attended Morning Yoga on Oct 9th
Tasks:
- Create
active_members_infoView: Create a view namedactive_members_infoshowingmember_id,first_name,last_name,email, andtype_namefor all members whosestatusis ‘Active’. Joinmembersandmembership_types. - Create
member_primary_contactView: Create a view namedmember_primary_contactshowing only thefirst_name,last_name, andemailof all members, regardless of status. - Create
yoga_pilates_scheduleView: Create a view namedyoga_pilates_schedulethat lists theclass_name,schedule_day,schedule_time, andtrainer_namefor classes taught by trainers specializing in ‘Yoga & Pilates’ (or specifically by ‘Jane Smith’). Joinclassesandtrainers. - Create
class_schedule_detailedView: Create a view namedclass_schedule_detailedthat displaysclass_name,trainer_name,specialization,schedule_day,schedule_time,duration_minutes, andmax_capacity. Joinclassesandtrainers. - Create
member_attendance_historyView: Create a view namedmember_attendance_historyshowing the member’s full name,class_name, andattendance_date. Requires joiningmembers,attendance_log, andclasses. - Create
class_popularity_reportView: Create a view namedclass_popularity_reportshowing eachclass_nameand the total number of attendances logged (COUNT(al.log_id)) for that class. Group byclass_name. - Query
active_members_infoView: Write a query to select all members fromactive_members_infowho have a ‘Premium’ membership type. - Query
member_attendance_historyView: Write a query to find all attendance records inmember_attendance_historyfor ‘Clark Kent’, ordered byattendance_datedescending. - Modify
member_primary_contactView: UseCREATE OR REPLACE VIEWto modify themember_primary_contactview to also include the member’sjoin_date. - Drop
yoga_pilates_scheduleView: Remove theyoga_pilates_scheduleview from the database.
Variant 13: Recipe Database
Scenario: You are managing a database for a recipe-sharing website. The database stores information about recipes, ingredients, the cuisines they belong to, and user ratings. You need to create views to simplify common data retrieval tasks.
Database Schema:
-- Cuisines Table
CREATE TABLE cuisines (
cuisine_id SERIAL PRIMARY KEY,
cuisine_name VARCHAR(50) NOT NULL UNIQUE -- e.g., Italian, Mexican, Indian
);
-- Ingredients Table
CREATE TABLE ingredients (
ingredient_id SERIAL PRIMARY KEY,
ingredient_name VARCHAR(100) NOT NULL UNIQUE,
category VARCHAR(50) -- e.g., Vegetable, Meat, Spice, Dairy
);
-- Recipes Table
CREATE TABLE recipes (
recipe_id SERIAL PRIMARY KEY,
recipe_name VARCHAR(150) NOT NULL,
cuisine_id INT REFERENCES cuisines(cuisine_id),
prep_time_minutes INT, -- Preparation time
cook_time_minutes INT, -- Cooking time
instructions TEXT
);
-- Recipe_Ingredients Table (Many-to-Many between Recipes and Ingredients)
CREATE TABLE recipe_ingredients (
recipe_ingredient_id SERIAL PRIMARY KEY,
recipe_id INT REFERENCES recipes(recipe_id),
ingredient_id INT REFERENCES ingredients(ingredient_id),
quantity VARCHAR(50) -- e.g., '2 cups', '1 tsp', '100g'
);
-- User_Ratings Table
CREATE TABLE user_ratings (
rating_id SERIAL PRIMARY KEY,
recipe_id INT REFERENCES recipes(recipe_id),
user_id INT, -- Assuming user IDs exist elsewhere, simplified here
rating INT CHECK (rating >= 1 AND rating <= 5), -- e.g., 1 to 5 stars
rating_date DATE DEFAULT CURRENT_DATE
);
-- Sample Data
INSERT INTO cuisines (cuisine_name) VALUES
('Italian'), ('Mexican'), ('Indian');
INSERT INTO ingredients (ingredient_name, category) VALUES
('Tomato', 'Vegetable'), ('Onion', 'Vegetable'), ('Garlic', 'Vegetable'),
('Pasta', 'Grain'), ('Chicken Breast', 'Meat'), ('Chili Powder', 'Spice'),
('Cumin', 'Spice'), ('Olive Oil', 'Oil'), ('Cheese', 'Dairy');
INSERT INTO recipes (recipe_name, cuisine_id, prep_time_minutes, cook_time_minutes, instructions) VALUES
('Spaghetti Bolognese', 1, 20, 40, 'Brown meat, add onions, garlic, tomatoes... Simmer... Serve over pasta.'),
('Chicken Tacos', 2, 15, 20, 'Cook chicken with spices... Warm tortillas... Assemble tacos.'),
('Simple Pasta Pomodoro', 1, 10, 15, 'Sauté garlic in olive oil, add tomatoes... Simmer... Toss with pasta.'),
('Chicken Curry', 3, 25, 35, 'Sauté onions, garlic, ginger... Add spices, chicken, tomatoes, coconut milk... Simmer.');
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, quantity) VALUES
(1, 1, '1 can'), (1, 2, '1 medium'), (1, 3, '2 cloves'), (1, 4, '400g'), (1, 5, '500g'), (1, 8, '2 tbsp'),
(2, 5, '2 medium'), (2, 2, '1 small'), (2, 6, '1 tbsp'), (2, 7, '1 tsp'),
(3, 1, '1 large can'), (3, 3, '3 cloves'), (3, 8, '3 tbsp'), (3, 4, '300g'), (3, 9, '50g grated'),
(4, 5, '600g'), (4, 2, '1 large'), (4, 3, '4 cloves'), (4, 7, '1.5 tsp');
INSERT INTO user_ratings (recipe_id, user_id, rating, rating_date) VALUES
(1, 101, 5, '2023-10-01'),
(1, 102, 4, '2023-10-02'),
(2, 101, 4, '2023-10-05'),
(3, 103, 5, '2023-10-06'),
(1, 103, 4, '2023-11-01');
Tasks:
- Create
recipe_summaryView: Create a view namedrecipe_summarythat showsrecipe_name,cuisine_name,prep_time_minutes, andcook_time_minutes. Joinrecipesandcuisines. - Create
ingredient_listView: Create a view namedingredient_listshowing only theingredient_nameandcategoryfor all ingredients, ordered bycategorytheningredient_name. - Create
quick_italian_recipesView: Create a view namedquick_italian_recipeslisting therecipe_namefor Italian recipes (cuisine_name= ‘Italian’) where the total time (prep_time_minutes+cook_time_minutes) is less than 45 minutes. - Create
recipe_ingredient_detailsView: Create a view namedrecipe_ingredient_detailsthat displays therecipe_name,ingredient_name, andquantityneeded for each ingredient in a recipe. Joinrecipes,recipe_ingredients, andingredients. - Create
average_recipe_ratingsView: Create a view namedaverage_recipe_ratingsshowingrecipe_nameand the average rating (AVG(rating)) rounded to 2 decimal places, aliased asaverage_rating. Only include recipes that have at least one rating. Group byrecipe_name. - Create
recipes_using_chickenView: Create a view namedrecipes_using_chickenthat lists therecipe_namefor all recipes that use ‘Chicken Breast’. Joinrecipes,recipe_ingredients, andingredients. - Query
recipe_summaryView: Write a query to select all recipes from therecipe_summaryview belonging to the ‘Mexican’ cuisine. - Query
average_recipe_ratingsView: Write a query to find recipes in theaverage_recipe_ratingsview with an average rating greater than 4.0. - Modify
ingredient_listView: UseCREATE OR REPLACE VIEWto modify theingredient_listview to also include theingredient_id. - Drop
quick_italian_recipesView: Remove thequick_italian_recipesview from the database.
Variant 14: Real Estate Agency
Scenario: You are managing the database for a real estate agency. The database tracks properties for sale, real estate agents, clients (buyers/sellers), and property viewings. Views are needed to simplify common queries for agents and managers.
Database Schema:
-- Agents Table
CREATE TABLE agents (
agent_id SERIAL PRIMARY KEY,
agent_name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE,
phone VARCHAR(20),
hire_date DATE
);
-- Properties Table
CREATE TABLE properties (
property_id SERIAL PRIMARY KEY,
address VARCHAR(255) NOT NULL,
city VARCHAR(50),
zip_code VARCHAR(10),
property_type VARCHAR(50), -- e.g., House, Apartment, Condo
bedrooms INT,
bathrooms NUMERIC(3,1), -- e.g., 2.5 for 2 full, 1 half bath
square_footage INT,
listing_price NUMERIC(12, 2),
listing_date DATE,
status VARCHAR(20) DEFAULT 'For Sale', -- e.g., For Sale, Under Contract, Sold
agent_id INT REFERENCES agents(agent_id) -- Agent responsible for the listing
);
-- Clients Table
CREATE TABLE clients (
client_id SERIAL PRIMARY KEY,
client_name VARCHAR(100) NOT NULL,
email VARCHAR(100),
phone VARCHAR(20),
client_type VARCHAR(10) -- 'Buyer' or 'Seller'
);
-- Viewings Table
CREATE TABLE viewings (
viewing_id SERIAL PRIMARY KEY,
property_id INT REFERENCES properties(property_id),
client_id INT REFERENCES clients(client_id), -- Client who viewed
agent_id INT REFERENCES agents(agent_id), -- Agent who conducted the viewing
viewing_date TIMESTAMP,
client_feedback TEXT
);
-- Sample Data
INSERT INTO agents (agent_name, email, phone, hire_date) VALUES
('Alice Green', 'alice.g@realestate.com', '555-1111', '2019-03-15'),
('Bob White', 'bob.w@realestate.com', '555-2222', '2020-07-01'),
('Carol Black', 'carol.b@realestate.com', '555-3333', '2021-11-20');
INSERT INTO properties (address, city, zip_code, property_type, bedrooms, bathrooms, square_footage, listing_price, listing_date, status, agent_id) VALUES
('123 Oak St', 'Metropolis', '12345', 'House', 3, 2.0, 1800, 350000.00, '2023-08-01', 'For Sale', 1),
('456 Maple Ave', 'Metropolis', '12346', 'Apartment', 2, 1.0, 950, 180000.00, '2023-09-10', 'For Sale', 2),
('789 Pine Ln', 'Gotham', '67890', 'House', 4, 2.5, 2400, 480000.00, '2023-07-15', 'Sold', 1),
('101 Birch Rd', 'Metropolis', '12345', 'Condo', 2, 2.0, 1200, 250000.00, '2023-10-05', 'Under Contract', 3);
INSERT INTO clients (client_name, email, phone, client_type) VALUES
('David King', 'david.k@email.com', '555-4444', 'Buyer'),
('Eva Queen', 'eva.q@email.com', '555-5555', 'Seller'), -- Corresponds to 123 Oak St (implied)
('Frank Prince', 'frank.p@email.com', '555-6666', 'Buyer');
INSERT INTO viewings (property_id, client_id, agent_id, viewing_date, client_feedback) VALUES
(1, 1, 1, '2023-08-10 14:00:00', 'Liked the backyard, kitchen needs update.'),
(2, 1, 2, '2023-09-15 11:00:00', 'Good location, but a bit small.'),
(1, 3, 1, '2023-08-12 16:30:00', 'Very interested, considering an offer.'),
(4, 3, 3, '2023-10-10 10:00:00', 'Loved the condo amenities.');
Tasks:
- Create
active_listingsView: Create a view namedactive_listingsshowingproperty_id,address,city,listing_price, andagent_namefor all properties currently ‘For Sale’. Joinpropertiesandagents. - Create
agent_contact_listView: Create a view namedagent_contact_listshowing only theagent_name,email, andphonefor all agents. - Create
large_homes_metropolisView: Create a view namedlarge_homes_metropolisthat lists theaddressandsquare_footagefor properties in ‘Metropolis’ with 3 or morebedroomsand over 2000square_footage. - Create
viewing_scheduleView: Create a view namedviewing_schedulethat displaysviewing_date,property_address(fromproperties.address),client_name, and theagent_namewho conducted the viewing. Joinviewings,properties,clients, andagents. - Create
agent_listing_countView: Create a view namedagent_listing_countshowing eachagent_nameand the total number of properties (COUNT(p.property_id)) they are currently listing (status = ‘For Sale’). Group byagent_name. Include agents even if they have 0 active listings. - Create
client_viewing_historyView: Create a view namedclient_viewing_historythat lists theclient_nameand theaddressof the properties they have viewed, along with theviewing_date. Joinclients,viewings, andproperties. - Query
active_listingsView: Write a query to select all listings from theactive_listingsview handled by ‘Alice Green’. - Query
viewing_scheduleView: Write a query to find all viewings in theviewing_scheduleview that occurred after ‘2023-09-01’, ordered by date. - Modify
agent_contact_listView: UseCREATE OR REPLACE VIEWto modify theagent_contact_listview to also include thehire_date. - Drop
large_homes_metropolisView: Remove thelarge_homes_metropolisview from the database.
Variant 15: Manufacturing Production Line
Scenario: You are managing a database for a factory’s production line. The database tracks manufactured products, the components they use, the production lines where they are assembled, and quality control checks. Views are needed for reporting and monitoring.
Database Schema:
-- Components Table
CREATE TABLE components (
component_id SERIAL PRIMARY KEY,
component_name VARCHAR(100) NOT NULL UNIQUE,
supplier VARCHAR(100),
cost NUMERIC(8, 2)
);
-- Products Table
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
product_sku VARCHAR(50) NOT NULL UNIQUE, -- Stock Keeping Unit
product_name VARCHAR(150) NOT NULL,
assembly_time_minutes INT -- Estimated time to assemble
);
-- Bill_Of_Materials Table (Links Products to Components)
CREATE TABLE bill_of_materials (
bom_id SERIAL PRIMARY KEY,
product_id INT REFERENCES products(product_id),
component_id INT REFERENCES components(component_id),
quantity_required INT NOT NULL
);
-- Production_Lines Table
CREATE TABLE production_lines (
line_id SERIAL PRIMARY KEY,
line_name VARCHAR(50) NOT NULL UNIQUE, -- e.g., Line A, Line B
location VARCHAR(100) -- e.g., Building 1, Floor 2
);
-- Production_Log Table
CREATE TABLE production_log (
log_id SERIAL PRIMARY KEY,
product_id INT REFERENCES products(product_id),
line_id INT REFERENCES production_lines(line_id),
units_produced INT NOT NULL,
production_date DATE DEFAULT CURRENT_DATE,
shift INT -- e.g., 1, 2, 3
);
-- Quality_Control Table
CREATE TABLE quality_control (
qc_id SERIAL PRIMARY KEY,
log_id INT REFERENCES production_log(log_id), -- Links to a specific production run
check_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(10) NOT NULL, -- 'Pass' or 'Fail'
notes TEXT
);
-- Sample Data
INSERT INTO components (component_name, supplier, cost) VALUES
('Casing A', 'SupplierX', 5.50),
('Circuit Board V1', 'SupplierY', 12.75),
('Power Supply Unit', 'SupplierX', 8.00),
('Screw Pack M3', 'SupplierZ', 0.50),
('Display Panel', 'SupplierY', 25.00);
INSERT INTO products (product_sku, product_name, assembly_time_minutes) VALUES
('WIDGET-001', 'Standard Widget', 30),
('GADGET-X', 'Advanced Gadget', 75),
('DOODAD-BLUE', 'Blue Doodad', 20);
INSERT INTO bill_of_materials (product_id, component_id, quantity_required) VALUES
(1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 4), -- Widget components
(2, 1, 1), (2, 2, 2), (2, 3, 1), (2, 4, 8), (2, 5, 1), -- Gadget components
(3, 1, 1), (3, 4, 2); -- Doodad components
INSERT INTO production_lines (line_name, location) VALUES
('Assembly Line 1', 'Building A - West Wing'),
('Assembly Line 2', 'Building B - East Wing'),
('Testing Bench', 'Building A - QC Area');
INSERT INTO production_log (product_id, line_id, units_produced, production_date, shift) VALUES
(1, 1, 100, '2023-11-01', 1),
(2, 2, 50, '2023-11-01', 1),
(1, 1, 120, '2023-11-01', 2),
(3, 1, 200, '2023-11-02', 1);
INSERT INTO quality_control (log_id, check_timestamp, status, notes) VALUES
(1, '2023-11-01 10:00:00', 'Pass', 'Sample check ok.'),
(2, '2023-11-01 11:30:00', 'Pass', NULL),
(3, '2023-11-01 18:00:00', 'Fail', '3 units had cosmetic defects.'),
(3, '2023-11-01 18:05:00', 'Pass', 'Reworked units passed re-inspection.'),
(4, '2023-11-02 09:45:00', 'Pass', NULL);
Tasks:
- Create
product_component_listView: Create a view namedproduct_component_listthat showsproduct_name,component_name, andquantity_requiredfor each product. Joinproducts,bill_of_materials, andcomponents. - Create
component_suppliersView: Create a view namedcomponent_suppliersshowing only thecomponent_nameandsupplierfor all components, ordered by supplier. - Create
failed_qc_checksView: Create a view namedfailed_qc_checksthat lists thelog_id,check_timestamp, andnotesfor all quality control checks with astatusof ‘Fail’. - Create
daily_production_summaryView: Create a view nameddaily_production_summarythat displaysproduction_date,product_name,line_name, andunits_produced. Joinproduction_log,products, andproduction_lines. - Create
product_component_costView: Create a view namedproduct_component_costshowingproduct_nameand the total cost of its components (Sum ofbom.quantity_required * c.cost), aliased astotal_component_cost. Group byproduct_name. Joinproducts,bill_of_materials, andcomponents. - Create
line_production_totalsView: Create a view namedline_production_totalsthat shows theline_nameand the totalunits_producedfor each production line across all dates/products recorded in the log. Group byline_name. - Query
product_component_listView: Write a query to select all components required for the ‘Advanced Gadget’ from theproduct_component_listview. - Query
daily_production_summaryView: Write a query to find all production records in thedaily_production_summaryview for ‘2023-11-01’. - Modify
component_suppliersView: UseCREATE OR REPLACE VIEWto modify thecomponent_suppliersview to also include the componentcost. - Drop
failed_qc_checksView: Remove thefailed_qc_checksview from the database.
Variant 16: Meteorological Data Tracking
Scenario: You are working with a database that collects weather data from various stations. You need to create views to simplify access to readings, station information, and summary statistics.
Database Schema:
-- Locations Table (Where stations are physically located)
CREATE TABLE locations (
location_id SERIAL PRIMARY KEY,
city VARCHAR(100) NOT NULL,
country_code CHAR(2) NOT NULL, -- ISO 3166-1 alpha-2
latitude NUMERIC(8, 6), -- e.g., 40.7128
longitude NUMERIC(9, 6) -- e.g., -74.0060
);
-- WeatherStations Table
CREATE TABLE weather_stations (
station_id SERIAL PRIMARY KEY,
station_name VARCHAR(100) NOT NULL UNIQUE,
location_id INT REFERENCES locations(location_id),
elevation_meters INT,
established_date DATE,
status VARCHAR(20) DEFAULT 'Active' -- Active, Inactive, Maintenance
);
-- Readings Table
CREATE TABLE readings (
reading_id BIGSERIAL PRIMARY KEY, -- Use BIGSERIAL for potentially large number of readings
station_id INT REFERENCES weather_stations(station_id),
reading_timestamp TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
temperature_celsius NUMERIC(4, 1), -- e.g., 25.5 C
humidity_percent NUMERIC(4, 1), -- e.g., 60.2 %
pressure_hpa NUMERIC(6, 1), -- e.g., 1013.2 hPa
wind_speed_kph NUMERIC(5, 1)
);
-- Meteorologists Table (Staff managing stations or analyzing data)
CREATE TABLE meteorologists (
met_id SERIAL PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
specialty VARCHAR(100), -- e.g., Climatology, Forecasting
assigned_station_id INT REFERENCES weather_stations(station_id) NULL -- A meteorologist might oversee a specific station
);
-- Sample Data
INSERT INTO locations (city, country_code, latitude, longitude) VALUES
('London', 'GB', 51.5074, -0.1278),
('Tokyo', 'JP', 35.6895, 139.6917),
('New York', 'US', 40.7128, -74.0060);
INSERT INTO weather_stations (station_name, location_id, elevation_meters, established_date, status) VALUES
('Heathrow Main', 1, 25, '1980-05-10', 'Active'),
('Shibuya Crossing Tower', 2, 40, '2005-11-20', 'Active'),
('Central Park Kiosk', 3, 50, '1995-03-15', 'Maintenance'),
('London City Airport', 1, 5, '1999-08-01', 'Active');
INSERT INTO readings (station_id, reading_timestamp, temperature_celsius, humidity_percent, pressure_hpa, wind_speed_kph) VALUES
(1, '2024-01-10 10:00:00+00', 8.5, 75.2, 1005.1, 15.3),
(2, '2024-01-10 19:00:00+09', 5.2, 50.0, 1015.5, 5.0),
(1, '2024-01-10 11:00:00+00', 9.0, 74.8, 1005.0, 16.1),
(4, '2024-01-10 11:00:00+00', 9.1, 78.0, 1004.9, 18.0),
(2, '2024-01-10 20:00:00+09', 4.9, 51.5, 1015.8, 5.5);
INSERT INTO meteorologists (full_name, specialty, assigned_station_id) VALUES
('Dr. Evelyn Reed', 'Climatology', 1),
('Kenji Tanaka', 'Forecasting', 2),
('Sam Weatherby', 'Atmospheric Physics', NULL);
Tasks:
- Create
station_locationsView: Create a view namedstation_locationsshowing thestation_name,status,city, andcountry_code. Joinweather_stationsandlocations. - Create
met_directoryView: Create a view namedmet_directoryshowing only thefull_nameandspecialtyof all meteorologists. - Create
high_humidity_readingsView: Create a view namedhigh_humidity_readingsshowingreading_id,station_id,reading_timestamp, andhumidity_percentfor readings where humidity is greater than 75%. - Create
stations_under_maintenanceView: Create a view namedstations_under_maintenanceshowing thestation_nameandestablished_datefor all stations withstatus= ‘Maintenance’. - Create
detailed_readings_infoView: Create a view nameddetailed_readings_infoshowingreading_timestamp,temperature_celsius,humidity_percent,pressure_hpa,wind_speed_kph, along with thestation_nameandcity. This requires joiningreadings,weather_stations, andlocations. - Create
avg_temp_per_stationView: Create a view namedavg_temp_per_stationshowing thestation_nameand the averagetemperature_celsiuscalculated from all its readings. UseAVG()andGROUP BY. Format the average temperature to one decimal place (::NUMERIC(4,1)). - Query
station_locationsView: Write a query to select all stations from thestation_locationsview located in the country ‘GB’. - Query
detailed_readings_infoView: Write a query to find all readings in thedetailed_readings_infoview from the ‘Heathrow Main’ station, ordered byreading_timestampdescending. - Modify
met_directoryView: UseCREATE OR REPLACE VIEWto modify themet_directoryview to also include themet_id. - Drop
stations_under_maintenanceView: Remove thestations_under_maintenanceview from the database.
Variant 17: Vehicle Fleet Management
Scenario: You manage a database for a company’s vehicle fleet, tracking vehicles, drivers, trips, and maintenance. Views are needed to streamline reporting and data access for different user roles.
Database Schema:
-- Drivers Table
CREATE TABLE drivers (
driver_id SERIAL PRIMARY KEY,
driver_name VARCHAR(100) NOT NULL,
license_number VARCHAR(50) NOT NULL UNIQUE,
hire_date DATE NOT NULL,
is_active BOOLEAN DEFAULT TRUE
);
-- Vehicles Table
CREATE TABLE vehicles (
vehicle_id SERIAL PRIMARY KEY,
license_plate VARCHAR(15) NOT NULL UNIQUE,
make VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
year INT,
vehicle_type VARCHAR(30), -- e.g., Sedan, Van, Truck
status VARCHAR(20) DEFAULT 'Operational' -- Operational, Maintenance, Decommissioned
);
-- MaintenanceLogs Table
CREATE TABLE maintenance_logs (
log_id SERIAL PRIMARY KEY,
vehicle_id INT REFERENCES vehicles(vehicle_id),
service_date DATE NOT NULL,
service_type VARCHAR(100) NOT NULL, -- e.g., Oil Change, Tire Rotation, Engine Repair
cost NUMERIC(8, 2),
notes TEXT
);
-- Trips Table
CREATE TABLE trips (
trip_id SERIAL PRIMARY KEY,
vehicle_id INT REFERENCES vehicles(vehicle_id),
driver_id INT REFERENCES drivers(driver_id),
start_datetime TIMESTAMP WITH TIME ZONE NOT NULL,
end_datetime TIMESTAMP WITH TIME ZONE, -- Can be NULL if trip is ongoing
start_odometer_km INT,
end_odometer_km INT,
purpose VARCHAR(255)
);
-- Sample Data
INSERT INTO drivers (driver_name, license_number, hire_date, is_active) VALUES
('Alice Ray', 'DL123456', '2020-03-15', TRUE),
('Bob Carr', 'DL987654', '2021-07-22', TRUE),
('Charlie Fox', 'DL555111', '2019-01-10', FALSE); -- Inactive driver
INSERT INTO vehicles (license_plate, make, model, year, vehicle_type, status) VALUES
('FLEET-001', 'Toyota', 'Camry', 2021, 'Sedan', 'Operational'),
('FLEET-002', 'Ford', 'Transit', 2020, 'Van', 'Operational'),
('FLEET-003', 'Toyota', 'Camry', 2021, 'Sedan', 'Maintenance'),
('FLEET-004', 'Honda', 'Civic', 2019, 'Sedan', 'Decommissioned');
INSERT INTO maintenance_logs (vehicle_id, service_date, service_type, cost, notes) VALUES
(1, '2023-12-01', 'Oil Change', 75.50, 'Synthetic oil used'),
(2, '2023-11-15', 'Tire Rotation', 50.00, NULL),
(3, '2024-01-05', 'Check Engine Light', 250.80, 'Replaced O2 sensor');
INSERT INTO trips (vehicle_id, driver_id, start_datetime, end_datetime, start_odometer_km, end_odometer_km, purpose) VALUES
(1, 1, '2024-01-08 09:00:00+00', '2024-01-08 11:30:00+00', 50100, 50185, 'Client Meeting Site A'),
(2, 2, '2024-01-09 13:00:00+00', '2024-01-09 17:00:00+00', 75200, 75350, 'Equipment Delivery'),
(1, 1, '2024-01-10 08:30:00+00', NULL, 50200, NULL, 'Client Meeting Site B - Ongoing'); -- Ongoing trip
Tasks:
- Create
operational_vehiclesView: Create a view namedoperational_vehiclesshowinglicense_plate,make,model, andyearfor all vehicles withstatus= ‘Operational’. - Create
active_driver_licensesView: Create a view namedactive_driver_licensesshowing only thedriver_nameandlicense_numberfor drivers whereis_activeis TRUE. - Create
recent_maintenanceView: Create a view namedrecent_maintenanceshowinglog_id,vehicle_id,service_date, andservice_typefor maintenance logs recorded within the last 90 days (service_date >= CURRENT_DATE - INTERVAL '90 days'). - Create
vans_listView: Create a view namedvans_listdisplaying thelicense_plate,make, andmodelfor all vehicles wherevehicle_type= ‘Van’. - Create
trip_summaryView: Create a view namedtrip_summaryshowingtrip_id,start_datetime,driver_name,license_plate, andpurpose. This requires joiningtrips,drivers, andvehicles. - Create
vehicle_maintenance_costView: Create a view namedvehicle_maintenance_costshowing thelicense_plateand the total maintenance cost (SUM(ml.cost)) for each vehicle. Joinvehiclesandmaintenance_logs. Group bylicense_plate. Handle vehicles with no maintenance logs (they should appear with 0 or NULL cost - use a LEFT JOIN). - Query
active_driver_licensesView: Write a query to select the driver name from theactive_driver_licensesview for license number ‘DL123456’. - Query
trip_summaryView: Write a query to find all trips in thetrip_summaryview driven by ‘Alice Ray’, ordered bystart_datetimedescending. - Modify
operational_vehiclesView: UseCREATE OR REPLACE VIEWto modify theoperational_vehiclesview to also include thevehicle_type. - Drop
vans_listView: Remove thevans_listview from the database.
Submission Instructions
- Create a new Google Document. Example
- Submit Your Google Doc Link (915-23 KII): Google Sheet
- Submit Your Google Doc Link (953-23 AXI): Google Sheet