dbms

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.

Define the following terms:


Instance: An instance refers to a specific occurrence of an object or entity within a database.
Schema: A schema is the blueprint or structure that defines the organization of data within a
database.

2. What are the advantages of using a Database Management System (DBMS) over a
traditional file processing system?
1. Data Redundancy and Inconsistency Reduction: DBMS minimizes data redundancy and
ensures data consistency by centralizing data storage and management.
2. Data Integrity and Security: DBMS enforces data integrity constraints and provides
robust security measures to protect data from unauthorized access.
3. Data Independence: DBMS allows data to be modified without affecting the application
programs, providing logical and physical data independence.
4. Efficient Data Access: DBMS uses sophisticated algorithms to efficiently retrieve and
manipulate data, improving performance and speed.

Q. Enlist and explain aggregate functions in SQL.


1) COUNT() 2) SUM() 3) AVG()
Aggregate Functions in SQL
Aggregate functions perform calculations on a set of values and return a single value. Here are
some common aggregate functions in SQL:

Normalization
Normalization is the process of organizing data in a database to reduce redundancy and
improve data integrity. It involves dividing a database into two or more tables and defining
relationships between them. The main types of normalization are:
1. First Normal Form (1NF)
2. Second Normal Form (2NF ) 3 Third Normal Form (3NF)
3. Differentiate between the Primary Key and Foreign Key.
Primary Key
 Uniqueness: A primary key uniquely identifies each record in a table.
 Null Values: A primary key cannot contain null values.
 Single Table: It is defined within a single table.
 Purpose: Ensures that each record in the table is unique and can be referenced.
Foreign Key
 Relationship: A foreign key is a field (or collection of fields) in one table that uniquely
identifies a row of another table.
 Null Values: A foreign key can contain null values.
 Multiple Tables: It is used to establish a link between two tables.
 Purpose: Enforces referential integrity by ensuring that the value in the foreign key
column matches a value in the primary key column of the referenced table.
Q1
a) Create a table EMPLOYEE with attributes EmpID, Name, Salary, and Dept.
CREATE TABLE EMPLOYEE (
EmpID INT PRIMARY KEY,
Name VARCHAR(100),
Salary DECIMAL(10, 2),
Dept VARCHAR(50)
);
b) Display employees whose salary is greater than 50,000.
SELECT * FROM EMPLOYEE
WHERE Salary > 50000;

2. Write an SQL query to:


a) Increase the salary of employees by 10% in the department where the Department ID is 102.
UPDATE EMPLOYEE
SET Salary = Salary * 1.10
WHERE DeptID = 102;
b) Display employees whose names start with the letter 'A'.
SELECT * FROM EMPLOYEE
WHERE Name LIKE 'A%';
3. Explain the GRANT and REVOKE commands with syntax and examples.
I, The GRANT command is used to give specific privileges to users or roles.
Syntax GRANT privilege_name ON object_name TO {user | role} [WITH GRANT OPTION];

2 The REVOKE command is used to remove specific privileges from users or roles.
REVOKE privilege_name ON object_name FROM {user | role};

4. Write a query to:


a) Find the average salary of all employees.
SELECT AVG(Salary) AS AverageSalary
FROM EMPLOYEE;
c) Display the total salary for each department.
SELECT Dept, SUM(Salary) AS TotalSalary
FROM EMPLOYEE
GROUP BY Dept;

1. Write a PL/SQL program to check whether a specific employee exists in the EMPLOYEE
table based on the employee number. If not, display a message "Employee Not Found."
DECLARE
emp_id EMPLOYEE.EmpID%TYPE;
emp_count NUMBER;
BEGIN
-- Set the employee number to check
emp_id := 101;
SELECT COUNT(*)
INTO emp_count
FROM EMPLOYEE
WHERE EmpID = emp_id;
IF emp_count = 0 THEN
DBMS_OUTPUT.PUT_LINE('Employee Not Found');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee Exists');
END IF;
END;
Q. Write a PL/SQL program to calculate the total salary of employees in a specific
department.
DECLARE
dept_id EMPLOYEE.DeptID%TYPE;
total_salary NUMBER;
BEGIN
dept_id := 102; SELECT SUM(Salary)
INTO total_salary
FROM EMPLOYEE
WHERE DeptID = dept_id;
DBMS_OUTPUT.PUT_LINE('Total Salary for Department ' || dept_id || ' is: ' || total_salary);
END;
Q. Consider the table EMP (EmpID, Name, Salary, Department, JoiningDate). Write SQL
queries to:
a)Display employees who joined after January 1, 2020.
SELECT * FROM EMP
WHERE JoiningDate > '2020-01-01';
b) Display the names of employees who have more than 5 years of experience.
SELECT Name
FROM EMP
WHERE DATEDIFF(YEAR, JoiningDate, GETDATE()) > 5;
d) Find the department with the highest average salary.
SELECT Department, AVG(Salary) AS AverageSalary
FROM EMP
GROUP BY Department
ORDER BY AverageSalary DESC
LIMIT 1;
3. Create a table BOOK with the following attributes: BookID, Title, Author, Publisher,
Price. Write SQL queries to:
CREATE TABLE BOOK (
BookID INT PRIMARY KEY,
Title VARCHAR(255),
Author VARCHAR(255),
Publisher VARCHAR(255),
Price DECIMAL(10, 2)
);
a) Add a new book to the table.
INSERT INTO BOOK (BookID, Title, Author, Publisher, Price)
VALUES (1, 'The Great Gatsby', 'F. Scott Fitzgerald', 'Scribner', 10.99);
b) Update the price of a book based on its ID.
UPDATE BOOK
SET Price = 12.99
WHERE BookID = 1;
c) Delete books older than 5 years.
DELETE FROM BOOK
WHERE PublicationDate < DATEADD(YEAR, -5, GETDATE());

3. Write SQL queries for the following:


a) Display employees' names and department names who have more than 3 years of
experience.
SELECT Name, Department
FROM EMP
WHERE DATEDIFF(YEAR, JoiningDate, GETDATE()) > 3;
b) Display the employee with the highest salary in each department.
SELECT Department, Name, Salary
FROM EMP e1
WHERE Salary = (
SELECT MAX(Salary)
FROM EMP e2
WHERE e1.Department = e2.Department
);
c) Count the number of employees in each department.
SELECT Department, COUNT(*) AS EmployeeCount
FROM EMP
GROUP BY Department;

DARLING ALL THE BEST👍✌️🔝

You might also like