DBMS Experiments
DBMS Experiments
SQL functions are used to perform calculations on data. Here are some categories of
functions and their uses:
1. Aggregate Functions:
This will return the total number of employees in the employees table.
This will return the total salary of all employees in the HR department.
This will return the full name of each employee by combining their first and last
names.
This will extract the first three characters of the employee_code for each employee.
These functions allow you to manipulate and retrieve information from date values.
This will return the date exactly 7 days from the current date.
This will return the number of days each employee has been employed by calculating
the difference between the current date and their hire_date.
This will return the salary of each employee rounded to two decimal places.
This will return the absolute value of the salary change, ensuring there are no negative
values.
Primary Key
Example:
The SQL query creates an Employees table to store employee information. It defines four
columns: EmployeeID, FirstName, LastName, and Email, with appropriate data types. The
EmployeeID column is designated as the primary key, ensuring each identifier is unique and
cannot be null. This setup enforces data integrity by guaranteeing that every employee has a
distinct identifier, which is essential for managing and referencing records accurately in the
database.
● Definition: A foreign key is a field (or a collection of fields) in one table that
uniquely identifies a row in another table.
● Relationship: Establishes a link between the data in two tables, creating a
relationship.
● Referential Integrity: Foreign keys allow for referential integrity by ensuring that the
value in the foreign key column must match a primary key value in the referenced
table or be null.
● Multiple Values: A foreign key can have duplicate values in the referencing table,
allowing for many-to-one relationships.
● Purpose: Used to create relationships between tables, facilitating data normalization
and reducing redundancy.
Example:
In this example, two tables are created: Departments and Employees. The Departments table
has a primary key, DepartmentID, to uniquely identify each department. In the Employees
table, the DepartmentID column serves as a foreign key, establishing a link between
employees and their respective departments. This foreign key constraint ensures that any
value entered in the DepartmentID column of the Employees table must correspond to a valid
DepartmentID in the Departments table, thus enforcing referential integrity and ensuring
meaningful relationships between the two tables.
GROUP BY SO.OrderNo;
Aim: Given a relational database, create expressions in QBE for each of the
SQL queries.
In QBE (Query by Example), the queries are written in a graphical format where you fill in
values into templates to describe what data you want to manipulate or retrieve.
Relational Schema
UPDATE Employee
SET city = 'Newtown'
WHERE personname = 'Jones';
UPDATE Works
SET salary = salary * 1.10
WHERE companyname = 'First Bank Corporation';
UPDATE Works
SET salary = salary * 1.10
WHERE personname IN (SELECT personname FROM Manages);
4. Give all managers a 10% raise unless the salary would be greater than
Rs.100,000. In such a case, give only a 3% raise:
UPDATE Works
SET salary = CASE
WHEN salary * 1.10 > 100000 THEN salary * 1.03
ELSE salary * 1.10
, END
WHERE personname IN (SELECT personname FROM Manages);
Aim: Construct the SQL queries for the given relational database
1. Creating Tables:
SELECT pname
FROM patients
WHERE age <= 10;
This query selects the names of all patients who are 10 years old or younger.
SELECT dname
FROM doctors
WHERE rank = 'surgeon';
This query selects the names of all doctors who are surgeons.
This query selects the names and phone numbers of all doctors.
This query selects the names and phone numbers of doctors who are surgeons.
Aim: Assignment 4.
INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO) VALUES
(7369, 'Smith', 'Clerk', 7902, '1980-12-17', 800, NULL, 20),
(7499, 'Allen', 'Salesman', 7698, '1981-02-20', 1600, 300, 30),
(7521, 'Ward', 'Salesman', 7698, '1981-02-22', 1250, 500, 30),
(7566, 'Jones', 'Manager', 7839, '1981-04-02', 2975, NULL, 20),
(7654, 'Martin', 'Salesman', 7698, '1981-09-28', 1250, 1400, 30),
(7698, 'Blake', 'Manager', 7839, '1981-05-01', 2850, NULL, 30),
(7782, 'Clark', 'Manager', 7839, '1981-06-09', 2450, NULL, 10),
(7788, 'Scott', 'Analyst', 7566, '1982-12-09', 3000, NULL, 20),
(7839, 'King', 'President', NULL, '1981-11-17', 5000, NULL, 10),
(7844, 'Turner', 'Salesman', 7698, '1981-09-08', 1500, 0, 30),
(7876, 'Adams', 'Clerk', 7788, '1983-01-12', 1100, NULL, 20),
(7900, 'James', 'Clerk', 7698, '1981-12-03', 950, NULL, 30),
(7902, 'Ford', 'Analyst', 7566, '1981-12-04', 3000, NULL, 20),
(7934, 'Miller', 'Clerk', 7782, '1982-01-23', 1300, NULL, 10);
SELECT ENAME FROM EMP WHERE EMPNO IN (7369, 7521, 7839, 7934,
7788);
SELECT ENAME FROM EMP WHERE DEPTNO NOT IN (30, 40, 10);
6. List employee names for those who have joined between 30 June and 31 Dec. ‘81.
8. List the names of employees who are not eligible for commission.
9. List the name and designation of the employee who does not report to anybody.
SELECT ENAME FROM EMP WHERE COMM IS NOT NULL AND COMM > 0;
12. List employees whose names either start or end with “S”.
SELECT ENAME FROM EMP WHERE ENAME LIKE 'S%' OR ENAME LIKE
'%S';
13. List names of employees whose names have “i” as the second character.
17. List the maximum, minimum, and average salary in the company.
1. Find names, street address, and cities of residence for employees who work for
'First Bank Corporation' and earn more than $10,000.
2. Find names of all employees who live in the same cities as the companies for
which they work.
SELECT e.employeename
FROM employee e
JOIN works w ON e.employeename = w.employeename
JOIN company c ON w.companyname = c.companyname
WHERE e.city = c.city;
SELECT e.employeename
FROM employee e
JOIN manages m ON e.employeename = m.employeename
JOIN employee mgr ON m.managername = mgr.employeename
WHERE e.city = mgr.city AND e.street = mgr.street;
5. Find names of employees who do not work for 'First Bank Corporation'.
6. Find names of employees who earn more than every employee of 'Small Bank
Corporation'.
SELECT employeename
FROM works
WHERE salary > (SELECT MAX(salary) FROM works WHERE companyname =
'Small Bank Corporation');
8. Find names of employees who live in the same cities as the companies for which
they work.
SELECT e.employeename
FROM employee e
JOIN works w ON e.employeename = w.employeename
JOIN company c ON w.companyname = c.companyname
WHERE e.city = c.city;
SELECT employeename
FROM works
WHERE salary > (SELECT MAX(salary) FROM works WHERE
companyname = 'Small Bank Corporation');