0% found this document useful (0 votes)
22 views12 pages

SQL Queries for Hospital, Electricity, and Courier Systems

The document outlines the creation and management of three systems: Hospital Management, Electricity Management, and Courier Management. It includes SQL queries for Data Definition Language (DDL) and Data Manipulation Language (DML) tasks such as creating tables, altering tables, inserting data, selecting data, updating records, and deleting records. Each system has specific tables and operations tailored to its functions, such as managing patients and doctors in the hospital system, customers and bills in the electricity system, and couriers and deliveries in the courier system.

Uploaded by

sriyogesheeli
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views12 pages

SQL Queries for Hospital, Electricity, and Courier Systems

The document outlines the creation and management of three systems: Hospital Management, Electricity Management, and Courier Management. It includes SQL queries for Data Definition Language (DDL) and Data Manipulation Language (DML) tasks such as creating tables, altering tables, inserting data, selecting data, updating records, and deleting records. Each system has specific tables and operations tailored to its functions, such as managing patients and doctors in the hospital system, customers and bills in the electricity system, and couriers and deliveries in the courier system.

Uploaded by

sriyogesheeli
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Hospital Management System

DDL (Data Definition Language) Questions

Question 1: Create Tables for the Hospital Management System

Query

1. Create Patients Table

CREATE TABLE Patients (

patient_id INT,

name VARCHAR(100),

age INT,

gender VARCHAR(10),

address VARCHAR(255),

contact_number VARCHAR(15),

disease VARCHAR(100)

);

2. Create Doctors Table

CREATE TABLE Doctors (

doctor_id INT,

name VARCHAR(100),

specialization VARCHAR(100),

experience INT,

contact_number VARCHAR(15)

);

3. Create Appointments Table

CREATE TABLE Appointments (

appointment_id INT,

patient_id INT,

doctor_id INT,

appointment_date DATE,

status VARCHAR(50)

);
Question 2: Alter a Table (Add a Column)

Task: Add an email column to the Patients table

ALTER TABLE Patients

ADD email VARCHAR(100);

Question 3: Rename a Table

Task: Rename ‘Appointments’ to ‘Hospital_Appointments’

RENAME TABLE Appointments TO Hospital_Appointments;

Question 4: Truncate a Table

Task: Remove all data from the Doctors table but keep the structure

TRUNCATE TABLE Doctors;

Question 5: Drop a Table

Task: Drop the Hospital_Appointments table

DROP TABLE Hospital_Appointments;

DML (Data Manipulation Language) Questions

Question 6: Insert Data into Tables

Query

1. Insert Patients

INSERT INTO Patients (patient_id, name, age, gender, address, contact_number, disease)

VALUES

(1, 'John Doe', 35, 'Male', '123 Street, NY', '9876543210', 'Flu'),

(2, 'Alice Smith', 28, 'Female', '456 Avenue, LA', '8765432109', 'Migraine'),

(3, 'Mark Johnson', 40, 'Male', '789 Boulevard, SF', '7654321098', 'Diabetes');

2. Insert Doctors

INSERT INTO Doctors (doctor_id, name, specialization, experience, contact_number)

VALUES

(1, 'Dr. Sarah Lee', 'Cardiologist', 10, '9876541230'),


(2, 'Dr. Tom Brown', 'Neurologist', 8, '8765432190'),

(3, 'Dr. Emma Wilson', 'Dermatologist', 5, '7654321980');

3. Insert Appointments

INSERT INTO Hospital_Appointments (appointment_id, patient_id, doctor_id, appointment_date,


status)

VALUES

(1, 1, 1, '2025-02-10', 'Scheduled'),

(2, 2, 2, '2025-02-12', 'Completed'),

(3, 3, 3, '2025-02-15', 'Cancelled');

Question 7: Select Data from Tables

Query

1. Retrieve all patients who are older than 30

SELECT * FROM Patients

WHERE age > 30;

2. Retrieve all doctors who are either Cardiologists or Neurologists

SELECT * FROM Doctors

WHERE specialization = 'Cardiologist' OR specialization = 'Neurologist';

3. Retrieve all scheduled appointments

SELECT * FROM Hospital_Appointments

WHERE status = 'Scheduled';

Question 8: Use LIKE Operator

Query

1. Find patients whose name starts with ‘A’

SELECT * FROM Patients

WHERE name LIKE 'A%';

2. Find doctors whose specialization contains ‘Neuro’

SELECT * FROM Doctors

WHERE specialization LIKE '%Neuro%';


Question 9: Use BETWEEN Operator

Query

1. Find patients aged between 25 and 40

SELECT * FROM Patients

WHERE age BETWEEN 25 AND 40;

2. Find doctors with experience between 5 and 10 years

SELECT * FROM Doctors

WHERE experience BETWEEN 5 AND 10;

Question 10: Use IS NULL Operator

Query

1. Find patients whose email is not entered (NULL values)

SELECT * FROM Patients

WHERE email IS NULL;

2. Find appointments that have no status assigned

SELECT * FROM Hospital_Appointments

WHERE status IS NULL;

Question 11: Update Data in Tables

Query

1. Update a patient’s address

UPDATE Patients

SET address = '999 New Street, NY'

WHERE patient_id = 1;

2. Update a doctor’s experience

UPDATE Doctors

SET experience = 12

WHERE name = 'Dr. Sarah Lee';

3. Update an appointment status

UPDATE Hospital_Appointments

SET status = 'Completed'


WHERE appointment_id = 1;

Question 12: Delete Data from Tables

Query

1. Delete a patient record

DELETE FROM Patients

WHERE name = 'Mark Johnson';

2. Delete all cancelled appointments

DELETE FROM Hospital_Appointments

WHERE status = 'Cancelled';

2. Electricity Management System

DDL (Data Definition Language) Questions

Question 1: Create Tables for the Electricity Management System

Query

1. Create Customers Table

CREATE TABLE Customers (

customer_id INT,

name VARCHAR(100),

address VARCHAR(255),

contact_number VARCHAR(15),

email VARCHAR(100)

);

2. Create Meters Table

CREATE TABLE Meters (

meter_id INT,

customer_id INT,

meter_type VARCHAR(50),

installation_date DATE

);
3. Create Bills Table

CREATE TABLE Bills (

bill_id INT,

customer_id INT,

meter_id INT,

bill_date DATE,

amount DECIMAL(10,2),

status VARCHAR(50)

);

Question 2: Alter a Table (Add a Column)

Task: Add a column for ‘Due Date’ in the Bills table

ALTER TABLE Bills

ADD due_date DATE;

Question 3: Rename a Table

Task: Rename ‘Meters’ table to ‘Electric_Meters’

RENAME TABLE Meters TO Electric_Meters;

Question 4: Truncate a Table

Task: Remove all data from the Bills table but keep its structure

TRUNCATE TABLE Bills;

Question 5: Drop a Table

Task: Drop the Electric_Meters table

DROP TABLE Electric_Meters;

DML (Data Manipulation Language) Questions

Question 6: Insert Data into Tables

Query

1. Insert Customers
INSERT INTO Customers (customer_id, name, address, contact_number, email)

VALUES

(1, 'John Doe', '123 Main St, NY', '9876543210', 'john@[Link]'),

(2, 'Alice Smith', '456 Elm St, LA', '8765432109', 'alice@[Link]'),

(3, 'Mark Johnson', '789 Pine St, SF', '7654321098', NULL);

2. Insert Meters

INSERT INTO Meters (meter_id, customer_id, meter_type, installation_date)

VALUES

(101, 1, 'Residential', '2024-01-10'),

(102, 2, 'Commercial', '2023-12-15'),

(103, 3, 'Industrial', '2022-06-05');

3. Insert Bills

INSERT INTO Bills (bill_id, customer_id, meter_id, bill_date, amount, status)

VALUES

(201, 1, 101, '2024-02-01', 100.50, 'Paid'),

(202, 2, 102, '2024-02-05', 200.75, 'Unpaid'),

(203, 3, 103, '2024-02-10', 150.30, 'Pending');

Question 7: Select Data from Tables

Query

1. Retrieve all customers from New York

SELECT * FROM Customers

WHERE address LIKE '%NY%';

2. Retrieve all unpaid bills

SELECT * FROM Bills

WHERE status = 'Unpaid';

3. Retrieve all commercial meters installed after 2023

SELECT * FROM Meters

WHERE meter_type = 'Commercial' AND installation_date > '2023-01-01';

Question 8: Use LIKE Operator


Query

1. Find customers whose name starts with ‘A’

SELECT * FROM Customers

WHERE name LIKE 'A%';

2. Find all meter types containing ‘Industrial’

SELECT * FROM Meters

WHERE meter_type LIKE '%Industrial%';

Question 9: Use BETWEEN Operator

Query

1. Find bills between $100 and $200

SELECT * FROM Bills

WHERE amount BETWEEN 100 AND 200;

2. Find meters installed between ‘2022-01-01’ and ‘2023-12-31’

SELECT * FROM Meters

WHERE installation_date BETWEEN '2022-01-01' AND '2023-12-31';

Question 10: Use IS NULL Operator

Query

1. Find customers whose email is not entered (NULL values)

SELECT * FROM Customers

WHERE email IS NULL;

2. Find bills where due date is missing

SELECT * FROM Bills

WHERE due_date IS NULL;

Question 11: Update Data in Tables

Query

1. Update a customer’s address

UPDATE Customers

SET address = '999 New Avenue, NY'


WHERE customer_id = 1;

2. Update a bill’s status

UPDATE Bills

SET status = 'Paid'

WHERE bill_id = 202;

3. Update meter type from ‘Residential’ to ‘Smart Residential’

UPDATE Meters

SET meter_type = 'Smart Residential'

WHERE meter_id = 101;

Question 12: Delete Data from Tables

Query

1. Delete a customer record

DELETE FROM Customers

WHERE name = 'Mark Johnson';

2. Delete all unpaid bills

DELETE FROM Bills

WHERE status = 'Unpaid';

Courier Management System—Practice Exercise

DDL (Data Definition Language) Questions

Question 1: Create Tables for the Courier Management System

Task: Create tables for managing couriers, customers, and deliveries.

Query:

1. Create Customers Table

CREATE TABLE Customers (

customer_id INT,

name VARCHAR(100),

address VARCHAR(255),
phone VARCHAR(15),

email VARCHAR(100)

);

2. Create Couriers Table

CREATE TABLE Couriers (

courier_id INT,

sender_id INT,

receiver_id INT,

package_type VARCHAR(50),

weight DECIMAL(5,2),

status VARCHAR(50)

);

3. Create Deliveries Table

CREATE TABLE Deliveries (

delivery_id INT,

courier_id INT,

delivery_date DATE,

delivery_status VARCHAR(50),

assigned_agent VARCHAR(100)

);

Question 2: Alter a Table (Add a Column)

Task: Add a column for ‘Expected Delivery Date’ in the Deliveries table.

Question 3: Rename a Table

Task: Rename ‘Couriers’ table to ‘Shipment’.

Question 4: Truncate a Table

Task: Remove all data from the Deliveries table but keep its structure.

Question 5: Drop a Table


Task: Drop the Shipment table.

DML (Data Manipulation Language) Questions

Question 6: Insert Data into Tables

Task: Insert records into the Customers, Couriers, and Deliveries tables.

Query:

1. Insert Customers

INSERT INTO Customers (customer_id, name, address, phone, email)

VALUES

(1, 'John Doe', '123 Maple St, NY', '9876543210', 'john@[Link]'),

(2, 'Alice Smith', '456 Oak St, LA', '8765432109', 'alice@[Link]'),

(3, 'Mark Johnson', '789 Pine St, SF', '7654321098', NULL);

2. Insert Couriers

INSERT INTO Couriers (courier_id, sender_id, receiver_id, package_type, weight, status)

VALUES

(101, 1, 2, 'Documents', 1.5, 'In Transit'),

(102, 2, 3, 'Electronics', 2.8, 'Delivered'),

(103, 3, 1, 'Clothes', 3.0, 'Pending');

3. Insert Deliveries

INSERT INTO Deliveries (delivery_id, courier_id, delivery_date, delivery_status, assigned_agent)

VALUES

(201, 101, '2024-02-01', 'On Route', 'Agent A'),

(202, 102, '2024-02-03', 'Delivered', 'Agent B'),

(203, 103, '2024-02-05', 'Pending', NULL);

Question 7: Select Data from Tables

1. Retrieve all customers from New York.

2. Retrieve all couriers that are in transit or pending.

3. Retrieve deliveries assigned to Agent A.

Question 8: Use LIKE Operator


1. Find customers whose name starts with ‘A’.

2. Find couriers with package types containing ‘Electronics’.

Question 9: Use BETWEEN Operator

1. Find couriers weighing between 2kg and 5kg.

2. Find deliveries made between ‘2024-01-01’ and ‘2024-02-28’.

Question 10: Use IS NULL Operator

1. Find customers whose email is not entered (NULL values).

2. Find deliveries where the assigned agent is missing.

Question 11: Update Data in Tables

1. Update a customer’s address.

2. Update a courier’s status to ‘Delivered’.

3. Update delivery agent for a pending delivery.

Question 12: Delete Data from Tables

1. Delete a customer record.

2. Delete all couriers with status ‘Delivered’.

Common questions

Powered by AI

The IS NULL operator is crucial in identifying incomplete records, such as missing patient emails or unassigned appointment statuses, which can lead to miscommunication and data errors. Detecting NULL values allows for timely data correction and enhances the reliability of the database for decision-making and patient management .

The LIKE operator enhances querying by enabling pattern-based searches. For example, finding patients whose names start with 'A' or doctors whose specialization includes 'Neuro.' This functionality allows flexible data extraction based on partial matches and improves the system's capability to retrieve specified groups of records efficiently .

Adding a new column using the ALTER TABLE command affects data integrity by extending the table schema without modifying existing records. The new column is added with NULL values unless specified otherwise, thus preserving the integrity and consistency of pre-existing data across the database .

To rename a table in SQL, the RENAME TABLE statement is used. For instance, to rename the 'Appointments' table to 'Hospital_Appointments', execute the command: RENAME TABLE Appointments TO Hospital_Appointments. This operation does not affect the data but changes the table's identifier in the system. Care must be taken to update any dependencies such as SQL queries, views, or application code that reference the old table name to avoid runtime errors .

Deleting records with the DELETE statement removes specific rows from a table while maintaining the table structure, which could impact relational integrity if foreign key constraints exist. It requires careful execution to preserve relationships between tables, such as those between patients and their appointments. Despite being slower compared to TRUNCATE due to logging and transaction needs, it allows granular control over which data is removed .

Updating records like a doctor's experience or a patient's address can ensure data accuracy and reflect the most current information, crucial for operational efficacy and patient care. However, risks include accidental data corruption or inaccuracies if updates are erroneously inputted. Furthermore, constant updates might strain database performance if not handled properly .

The BETWEEN operator allows selection of data within specified ranges, enhancing query flexibility by allowing multi-condition filtering. For example, retrieving patients aged between 25 to 40 or doctors with a certain range of experience, streamlining data analysis and reporting efforts without needing complex logical operators .

Truncating a table, such as the 'Doctors' table, removes all records but retains the table and its structure for further use. This can quickly release storage space and reset the table for new data inputs, but it could lead to temporary loss of historical data, which is critical in a medical context for tracking patient history and physician assignments .

Dropping a table, such as 'Hospital_Appointments', permanently removes all data and its structure, which can free resources but risks loss of critical appointment data. It might be suitable for retiring obsolete structures but requires careful planning and data backup to avoid unintended loss of valuable historical data needed for audits or patient care continuity .

TRUNCATE removes all rows from a table without logging individual row deletions and is faster due to less resource usage, while DELETE can selectively remove specific rows, allowing finer control with logging. In a hospital management system, using TRUNCATE is suitable for clearing tables for new cycles but DELETE offers controlled data management critical for maintaining historical record integrity .

You might also like