Ansh Isml
Ansh Isml
1
B) Cheat sheet of all the Basic DDL commands
TRUNCATE: Removes all rows from a table but keeps its structure.
TRUNCATE TABLE table_name;
2
Ques 2 Design an Entity Relationship (ER) model for a college database in MS word.
Say wehave the following statements.
A college contains many departments
Each department can offer any number of courses
Many instructors can work in a department
An instructor can work only in one department
For each department there is a Head
An instructor can be head of only one department
Each instructor can take any number of courses
A course can be taken by only one instructor
A student can enroll for any number of courses
Each course can have any number of students
3
Ques 3 Create a table Student using the following Schema:
4
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = 'STUDENTS';
5
Ques 4 Insert at least 5 records in the table created in the above question no. 3 and
display the entire table.
6
Inserting 5 rows into Student table
INSERT INTO Students1 (Student_ID, Student_Name) VALUES (301, 'Alice');
INSERT INTO Students1 (Student_ID, Student_Name) VALUES (302, 'Bob');
INSERT INTO Students1 (Student_ID, Student_Name) VALUES (303, 'Charlie');
INSERT INTO Students1 (Student_ID, Student_Name) VALUES (304, 'David');
INSERT INTO Students1 (Student_ID, Student_Name) VALUES (305, 'Eve');
7
Ques 5 Using the table you created (Student) in question 3 (Annexure 1) solve the
following queries using DDL commands.
Ans 5
A. Display the schema of the Student table
DESC Student;
8
B. Rename the table to ‘Candidate’
ALTER TABLE Student RENAME TO Candidate;
9
D. Truncate the table Student
TRUNCATE TABLE Candidate;
10
Ques 6 Solve the following queries making use of DDL commands:
11
B. Add new column email
ALTER TABLE Employees ADD email VARCHAR(15);
DESC Employees;
12
D. Add primary key constraint
DESC EMPLOYEES;
ALTER TABLE Employees ADD PRIMARY KEY (EMP_NO);
DESC EMPLOYEES;
13
Ques 7 Perform following SQL DML commands using table (Employee) created in
the question above.
A. Insert records to the table with row 4 having JOB entry as default value.
D. Add Attribute experience as varchar(10). Fill the records in the column using
Ans 7
A. Insert records
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7369, 'SMITH', 'CLERK', TO_DATE('1980-12-17', 'YYYY-MM-DD'), 800);
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7499, 'ALLEN', 'SALESMAN', TO_DATE('1981-02-20', 'YYYY-MM-DD'), 1600);
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7521, 'WARD', 'SALESMAN', TO_DATE('1981-02-22', 'YYYY-MM-DD'), 1250);
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7566, 'JONES', DEFAULT, TO_DATE('1981-04-02', 'YYYY-MM-DD'), 2975);
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7654, 'MARTIN', 'SALESMAN', TO_DATE('1981-09-28', 'YYYY-MM-DD'),
1250);
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7698, 'BLAKE', 'MANAGER', TO_DATE('1981-05-01', 'YYYY-MM-DD'), 2850);
14
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7782, 'CLARK', 'MANAGER', TO_DATE('1981-06-09', 'YYYY-MM-DD'), 2450);
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7788, 'SCOTT', 'ANALYST', TO_DATE('1987-04-19', 'YYYY-MM-DD'), 3000);
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7839, 'KING', 'PRESIDENT', TO_DATE('1981-11-17', 'YYYY-MM-DD'), 5000);
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(7844, 'TURNER', 'SALESMAN', TO_DATE('1981-09-08', 'YYYY-MM-DD'),
1500);
15
B. Enter Salary as 100 (this will fail)
INSERT INTO Employees (EMP_NO, ENAME, JOB, HIREDATE, SAL) VALUES
(9999, 'TEST', 'EMPLOYEES', TO_DATE('2024-10-04', 'YYYY-MM-DD'), 100);
16
D. Add attribute experience
ALTER TABLE Employees ADD experience VARCHAR(10);
UPDATE Employees SET experience = '5 years' WHERE EMP_NO = 7369 AND
ENAME = 'SMITH';
UPDATE Employees SET experience = '2 years' WHERE EMP_NO = 7499 AND
ENAME = 'ALLEN';
UPDATE Employees SET experience = '10 years' WHERE EMP_NO = 7566 AND
ENAME = 'JONES';
UPDATE Employees SET experience = '4 years' WHERE EMP_NO = 7654 AND
ENAME = 'MARTIN';
UPDATE Employees SET experience = '6 years' WHERE EMP_NO = 7698 AND
ENAME = 'BLAKE';
UPDATE Employees SET experience = '8 years' WHERE EMP_NO = 7782 AND
ENAME = 'CLARK';
UPDATE Employees SET experience = '7 years' WHERE EMP_NO = 7788 AND
ENAME = 'SCOTT';
UPDATE Employees SET experience = '12 years' WHERE EMP_NO = 7839 AND
ENAME = 'KING';
UPDATE Employees SET experience = '9 years' WHERE EMP_NO = 7844 AND
ENAME = 'TURNER';
17
select * from Employees;
18
Ques 8 Solve the following SQL queries using table (Employee) created in the
question above.
A. Write a query to display the records of employees in alphabetical order of
their name.
B. Write a query to display name, job and hiredate of salesman only.
C. Write the query to display name of employee having in their names ‘ll' or
‘tt’.
D. Write a query to display employees who are analyst or salesman in
descending order of names.
E. Write the query to display name, empno, job and hiredate of employees
hired between '08–jun–1981' and '16–apr–1982';
F. Write the query to display employees who are managers and have salary
above 2500.
G. Write the query to display employees who are NOT analyst.
Write the query to display the distinct jobs.
Ans 8
A. Display the records of Employeess in alphabetical order of their name.
SELECT * FROM Employees
ORDER BY Ename;
19
B. Display name, job and hiredate of salesman only.
SELECT * FROM Employees WHERE JOB = 'SALESMAN';
20
D. Display Employeess who are analyst or salesman in descending order of names.
SELECT * FROM Employees WHERE JOB = 'ANALYST' OR JOB = 'SALESMAN'
ORDER BY Ename DESC;
E. Display name, empno, job and hiredate of Employeess hired between '08–jun–
1981' and '16–apr–1982'.
SELECT * FROM Employees
WHERE hiredate BETWEEN '08-JUN-1981' AND '16-APR-1982';
21
F. Display Employeess who are managers and have salary above 2500.
SELECT * FROM Employees WHERE JOB = 'MANAGER' AND SAL> 2500;
22
H. Display the distinct jobs.
SELECT DISTINCT job FROM Employees;
23
Ques 9 Use the table Employee given in Q7 and solve the following queries to
understand the usage of
AGGREGATE FUNCTIONS
Ans 9
24
-- Count the number of clerks
SELECT COUNT(*) AS NumberOfClerks FROM Employees WHERE job =
'CLERK';
25
C. Find the minimum and maximum salary of Managers.
SELECT MIN(SAL) AS MinSAL, MAX(SAL) AS MaxSAL FROM Employees
WHERE JOB = 'MANAGER';
26
E. Write a query to get the difference between the highest and lowest salaries.
SELECT MAX(SAL) - MIN(SAL) AS SALDifference FROM Employees;
27
Ques 10Write the Query to get the given output.
A.
AVG(SAL) COUNT(DISTINCTJOB)
2380.55556 5
B.
C. MAXIMUN_SALESMAN_SALARY
____________________________
1600
D. Display data of table having salary greater than the average salary of all
employees in descending order of names.
E. Display name, hiredate and job of the employees having salary equal to the
average salary of all staff members in ascending order of names.
28
Ans 10
A. SELECT AVG(SALARY) AS AverageSalary, COUNT( DISTINCT) FROM
BONUS;
29
INSERT INTO BONUS VALUES ('ANSH','10000','25000');
INSERT INTO BONUS VALUES ('ANSHUL','15000','25000');
INSERT INTO BONUS VALUES ('YASH','20000','25000');
INSERT INTO BONUS VALUES ('AMAN','25000','25000');
INSERT INTO BONUS VALUES ('VANSH','30000','12000');
INSERT INTO BONUS VALUES ('VARUN','35000','40000');
30
C. SELECT MAX(SAL) AS MAXIMUN_SALESMAN_SAL FROM Employee
WHERE JOB = 'SALESMAN';
31
E. SELECT ENAME, HIREDATE, JOB FROM employee WHERE SAL > (SELECT
AVG(SAL) FROM employee) ORDER BY ENAME DESC;
32