0% found this document useful (0 votes)
18 views32 pages

Ansh Isml

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)
18 views32 pages

Ansh Isml

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
You are on page 1/ 32

Ques 1 Demonstrate the understanding of facts by:

A. Making a pictorial presentation of a table and its different components


B. Make a cheat sheet of all the Basic DDL commands.

Ans 1- A) Pictorial presentation of a table and its different components

1
B) Cheat sheet of all the Basic DDL commands

CREATE: Used to create a new table or database.


CREATE TABLE Users (
ID INT,
Name VARCHAR(100),
Age INT
);

ALTER: Modifies an existing table (e.g., adding/removing columns).


ALTER TABLE table_name
ADD column_name datatype;

DROP: Deletes a table or database.


DROP TABLE table_name;

TRUNCATE: Removes all rows from a table but keeps its structure.
TRUNCATE TABLE table_name;

RENAME: Changes the name of a table.


RENAME TABLE old_name TO new_name;

COMMENT: Adds a descriptive remark to a table or column.


For example:- COMMENT ON {TABLE | COLUMN} object_name
IS 'comment_text';

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

Ans 2- Entity Relationship (ER) model for a college database

3
Ques 3 Create a table Student using the following Schema:

Column name Data type Size


Reg_no varchar2 10
Name char 30
DOB date
Address varchar2 50

Ans 3- Create a table ‘STUDENTS’

CREATE TABLE STUDENTS(


REG_NO VARCHAR2(10),
NAME CHAR (30),
DOB DATE,
ADDRESS VARCHAR2(50)
);

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.

Ans 4- Students1 Table

This table stores information about students in the college.

CREATE TABLE Students1 (


Student_ID INT PRIMARY KEY,
Student_Name VARCHAR2(50),
Enrollment_Date DATE
);

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');

SELECT * FROM Students1

7
Ques 5 Using the table you created (Student) in question 3 (Annexure 1) solve the
following queries using DDL commands.

A. Display the schema of the Student table.


B. Rename the table to ‘Candidate’.
C. Single line and Multi line comment

D. Truncate the table Student.


E. DROP the table Student.

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;

C. Single line and Multi-line comment


--This is a single-line comment
/*
This is a multi-line comment.
You can write multiple lines here.
*/

9
D. Truncate the table Student
TRUNCATE TABLE Candidate;

E. DROP the table Student


DROP TABLE Candidate;

10
Ques 6 Solve the following queries making use of DDL commands:

Columns Data Type & constraints


EMP_NO Number
EName Varchar (20), NOT NULL
JOB Varchar(10), DEFAULT ‘MANAGER’
HIREDATE number, UNIQUE
SAL number, CHECK (SAL>=800)
A. Create table Employee as per the data provided above in the table.
B. Add new Column email as varchar(15) .
C. Change the data type of Column Hiredate to date in the table.
D. Which field will you choose as primary key? Why? Apply to it primary key
constraint.
E. Drop the attribute email.
Ans 6
A. Create the Employees table
CREATE TABLE Employees
( EMP_NO INT,
ENAME VARCHAR(20) NOT NULL,
JOB VARCHAR(10) DEFAULT 'MANAGER',
HIREDATE INT UNIQUE,
SAL INT CHECK (SAL >= 800) );

11
B. Add new column email
ALTER TABLE Employees ADD email VARCHAR(15);
DESC Employees;

C. Change the data type of HIREDATE to DATE


ALTER TABLE Employees MODIFY HIREDATE DATE;
DESC Employees;

12
D. Add primary key constraint
DESC EMPLOYEES;
ALTER TABLE Employees ADD PRIMARY KEY (EMP_NO);
DESC EMPLOYEES;

E. Drop the email column


ALTER TABLE Employees DROP COLUMN email;
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.

B. Enter Salary as 100 to verify the running of Check constraint.

C.DELETE the third row.

D. Add Attribute experience as varchar(10). Fill the records in the column using

reference of empno and name.

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);

C. DELETE the third row


DELETE FROM Employees WHERE EMP_NO = 7521;

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';

C. Display name of Employees having 'll' or 'tt' in their names.


SELECT Ename FROM Employees WHERE Ename LIKE '%TT%' OR Ename LIKE
'%ll%' ;

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;

G. Display Employeess who are NOT analysts.


SELECT * FROM Employees WHERE JOB != 'ANALYST';

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

A. Count the number of job available.


Count number of clerks.
B. Find the total of EMPNO and SAL.
C. Find minimum and maximum salary of Managers.
D. Find the average of salary and name it Average Salary.
E. Write a query to get the difference between the highest and lowest salaries.

Ans 9

A. Count the number of jobs available and the number of clerks.


-- Count the number of distinct jobs available
SELECT COUNT(DISTINCT JOB) AS Number Of Jobs FROM Employees;

24
-- Count the number of clerks
SELECT COUNT(*) AS NumberOfClerks FROM Employees WHERE job =
'CLERK';

B. Find the total of EMPNO and SAL.


SELECT SUM(EMP_NO) AS TotalEMP_NO, SUM(SAL) AS TotalSAL FROM
Employees;

25
C. Find the minimum and maximum salary of Managers.
SELECT MIN(SAL) AS MinSAL, MAX(SAL) AS MaxSAL FROM Employees
WHERE JOB = 'MANAGER';

D. Find the average salary and name it AverageSalary.


SELECT AVG(SAL) AS AverageSAL FROM Employees;

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

More on AGGREGATE FUNCTIONS

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;

B.CREATE TABLE BONUS(


NAME CHAR (30),
SALARY INT,
BONUS INT
);

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');

SELECT MAX (BONUS) AS HIGHEST_BONUS,MIN (BONUS) AS


LOWEST_BONUS, SUM (BONUS) AS TOTAL_BONUS, AVG (BONUS) AS
AVERAGE_BONUS FROM BONUS;

30
C. SELECT MAX(SAL) AS MAXIMUN_SALESMAN_SAL FROM Employee
WHERE JOB = 'SALESMAN';

D. SELECT * FROM employee WHERE SAL > (SELECT AVG(SAL) FROM


employee) ORDER BY ENAME DESC;

31
E. SELECT ENAME, HIREDATE, JOB FROM employee WHERE SAL > (SELECT
AVG(SAL) FROM employee) ORDER BY ENAME DESC;

32

You might also like