dbmsall
dbmsall
Creating and modifying database table using DDL and DML commands.
Questions 1:
(a) Create a Table
Create a table named Students with the following fields:
StudentID (Primary Key), Name, Age, Class, Email.
(b) Alter a Table
Add a column PhoneNumber to the Students table.
Modify the data type of Age to TINYINT.
Drop the column Email from the Students table.
(c ) Drop a Table
Drop the Students table from the database.
(d ) Rename a Table
Rename the Students table to StudentRecords.
Question 2:
(a) Create a Table with Constraints
Create a table named Courses with the following fields:
CourseID (Primary Key), CourseName (Unique), Credits (Default value 3),
InstructorID (Foreign Key referencing Instructors table).
Question 3:
(a) Design and Create Tables for a Library Database
Create tables Books, Members, and Transactions with appropriate fields
and relationships.
(b) Implement Referential Integrity
Create a Department table and an Employee table.
Ensure referential integrity such that every Employee belongs to a valid
Department.
(c) Create a Table with a Composite Primary Key
Create a Marks table with a composite primary key on StudentID and
SubjectCode.
(d) Perform DDL Statements for Migration
Write DDL commands to copy the structure of a table OldData into a new
table
NewData without copying its data.
Questions 1:
UPDATE Students
SET Age = 21
WHERE StudentID = 1;
UPDATE Students
SET Class = 'B.Sc (Hons)'
WHERE Class = 'B.Sc';
SELECT *
FROM Students
WHERE Age > 20;
SELECT *
FROM Students
ORDER BY Age DESC;
UPDATE Employees
SET Salary = 17000
WHERE EmployeeID = 2;
UPDATE Orders
SET Quantity = 50
WHERE OrderID = 1 AND ProductID = 1;
UPDATE Customers
SET Balance = Balance + (SELECT Amount FROM Transactions WHERE TransactionID = 1)
WHERE CustomerID = 101;
START TRANSACTION;
ROLLBACK;
START TRANSACTION;
COMMIT;
Week 2
• Students
o student_id (INTEGER, PRIMARY KEY)
o name (VARCHAR(100), NOT NULL)
o email (VARCHAR(100), UNIQUE)
o department_id (INTEGER, FOREIGN KEY referencing
o Departments(department_id))
• Departments
o department_id (INTEGER, PRIMARY KEY)
o department_name (VARCHAR(100), UNIQUE, NOT NULL)
Question 2:
Insert the following records into the Departments table.
INSERT INTO Departments (department_id, department_name) VALUES (101, 'Computer
Science');
INSERT INTO Departments (department_id, department_name) VALUES (102,'Electrical
Engineering');
INSERT INTO Departments (department_id, department_name) VALUES (103, 'Mechanical
Engineering');
Code:
Output:
Question 3:
Insert the following records into the Students table and explain any constraint
violations that occur.
INSERT INTO Students (student_id, name, email, department_id) VALUES (1,
'Alice','[email protected]', 101);
INSERT INTO Students (student_id, name, email, department_id) VALUES (2, NULL,
'[email protected]', 102);
INSERT INTO Students (student_id, name, email, department_id) VALUES (3,
'Charlie','[email protected]', 103);
INSERT INTO Students (student_id, name, email, department_id) VALUES (4,
'David','[email protected]', 104);
Question 4:
Modify the Students table to add a constraint that ensures email cannot be NULL.
Code:
Output:
Question 5:
Drop the FOREIGN KEY constraint from the Students table that references Departments.
Code:
Output:
Question 6:
Modify the Students table so that if a department is deleted from Departments, all
students in that department are also deleted.
Code:
Output:
Question 7:
Can a table have a column that is both UNIQUE and NOT NULL? Justify your answer.
Solution:
Yes, a table can have a column that is both UNIQUE and NOT NULL.
Together, they enforce that every row must have a distinct, non-null value in that column.
Question 8:
Write an SQL query to find students whose email is NULL (if allowed).
Code:
Output:
Question 9:
If a table has both PRIMARY KEY and UNIQUE constraints on different columns,
what is the effect?
Solution:
If both exist on different columns:
• The PRIMARY KEY column(s) will have unique and non-null values.
• The UNIQUE column(s) must have distinct values but can contain NULLs if not explicitly NOT NULL.
Week 3
(SQL Query Writing with Functions and Sorting)
>Creating Database
Output:
Output:
Output:
Output:
Output:
9. What is the number of products that have a price greater than 500?
Code: Output:
10.What is the sum of the quantities in stock for products in the "Clothing" category that cost less than
100?
Code: Output:
Week 4
(SQL Query Writing with Functions and Sorting)
>Database Creation
1. Using GROUP BY
i. Write a query to find the total sales (Quantity * Price) for each product.
Code: Output:
ii. Write a query to count the number of sales transactions for each product.
Code: Output:
iii. Write a query to find the total quantity sold for each category.
Code: Output:
iv. Write a query to find the average sale price of each product.
Code: Output:
v. Write a query to find the highest sale price recorded for each category.
Code: Output:
Code:
Output:
ii. Write a query to find categories where the total sales (Quantity * Price) exceed 5000.
Code:
Output:
iii. Write a query to find products where the average sale price is greater than 50.
Code:
Output:
iv. Write a query to find categories that have more than 2 different products sold.
Code:
Output:
v. Write a query to find products with total sales greater than 8000 but only for the "Electronics"
category.
Code:
Output:
3. Using ORDER BY
i. Write a query to display products along with their total sales (Quantity *Price), sorted in
descending order of total sales.
Code:
Output:
ii. Write a query to display categories along with their total quantity sold, sorted in ascending
order
Code:
Output:
iii. Write a query to find the average sale price per product and order it in descending order.
Code:
Output:
iv. Write a query to display the total number of transactions per category, sorted in descending
order.
Code:
Output:
v. Write a query to list products where the total sales are greater than 5000,ordered by
total sales in descending order
Code:
Output:
Week-5
Relational Algebra is a formal language for querying relational databases. It provides
operations to retrieve and manipulate data stored in relational tables. Three important set
operations in relational algebra are:
1. UNION ( ) – Combines results from two relations and removes duplicates.
2. INTERSECTION ( ) – Returns only the common tuples between two relations.
3. MINUS ( ) – Returns tuples that exist in the first relation but not in the second.
Create Table:
1. Retrieve all unique students (RollNo, Name) who have either secured a
scholarship or participated in sports.
2. Find all students (RollNo) who have either scored more than 80 marks in an
exam or have won a scholarship.
4. Find students (RollNo) who have both a scholarship and play sports.
5. Get students (RollNo) who have scored more than 90 marks in at least one
subject and also have a scholarship.
7. Find students (RollNo, Name) who have a scholarship but have NOT played
any sports.
8. Retrieve students (RollNo, Name) who have played sports but never
received a scholarship.
9. Get students (RollNo, Name) who have scored below 50 marks in all their
subjects but are not involved in sports.
10. Find students from the "Mechanical" department who have not received
any scholarship and have not played any sports.
11. Find students who have played at least one sport at the "National" or
"International" level but have not scored above 70 in any exam.
12. Find students who have received a scholarship but have neither played sports
nor scored more than 60 marks in any exam.
13. Find students who have taken at least two different subjects in exams and have
either received a scholarship or played sports but not both.
Week-06
1. For all the staff members assigned to the existing departments, select all information about the staff
members and their respective departments.
2.Display the name salary, department identifier, and building and room location for every staff member
assigned to an existing department whose yearly salary exceeds $1000.
3. Display the name and title of every staff member who works in the humanities building.
4. Display the building and room of any academic department which employs a staff member whose title
begins with “EVANGLIST”.
5. For each department described in the DEPARTMENT table which employs at least one staff member,
display the department identifier followed by the number of staff members assigned to the department.
6. Form the cross product of the STAFF table and the DEPARTMENT table.
7. Assume that the dean is considering moving the administrative office of the management department. The
intention is to combine its administrative facilities with those of another department which is located in the
same building (which is unknown to the dean). To evaluate all possible options, display all information
about the management department followed by all information about any department which is located in the
same building.
Week-7
DBMS-Subqueries and Nested Queries
Questions:
Question-1: - Write a query to find the second highest salary from the
Employee table.
Query:
SELECT MAX(salary) AS second_highest_salary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);
Output:
Question-2: - List employees who earn more than the average salary.
Query:
SELECT *
FROM Employee
WHERE salary > (SELECT AVG(salary) FROM Employee);
Output:
Question-4: - Find employees working in a specific department.
Query:
SELECT *
FROM Employee
WHERE dept_id = 102;
Output:
3. Create a view named ProjectDepartmentSummary showing the department name and the
count of projects in each department.
CREATE VIEW ProjectDepartmentSummary AS
SELECT d.department_name,
COUNT(p.project_id) AS project_count
FROM Departments d
LEFT JOIN Projects p ON d.department_id = p.department_id
GROUP BY d.department_name;
4. Write a query to retrieve departments with more than 2 projects from this
view.
SELECT * FROM ProjectDepartmentSummary
WHERE project_count > 2;
5. Create a view named HighSalaryEmployees that shows th e first name, last name and
salary of employees who earn over $50,000.
CREATE VIEW HighSalaryEmployees AS
SELECT first_name, last_name, salary
FROM Employee
WHERE salary > 50000;
6. Write a query to update the last name of an employee in the
HighSalaryEmployees view.
UPDATE Employee
SET last_name = 'Smithson'
WHERE last_name = 'Doe' AND salary > 50000;
7. Create a view named SalesEmployeeDetails that displays employee first name, last
name, and total sales amount for each employee.
CREATE VIEW SalesEmployeeDetails AS
SELECT e.first_name, e.last_name, SUM(s.amount) AS total_sales
FROM Employee e
JOIN Sales s ON e.employee_id = s.employee_id
GROUP BY e.first_name, e.last_name;
8. Write a query to return the top 5 sales persons from this view, ordered by total sale
amount descending.
SELECT * FROM SalesEmployeeDetails
ORDER BY total_sales_amount DESC
LIMIT 5;
13.Create a sequence named project_id_seq for the Projects table, starting at 1000 and
incrementing by 1.
CREATE SEQUENCE project_id_seq START WITH 1000 INCREMENT BY 1
NOCACHE;
14.Write a SQL statement to reset the employee_id_seq sequence to a
specific value.
ALTER SEQUENCE employee_id_seq RESTART WITH 200;