0% found this document useful (0 votes)
178 views13 pages

Dbms Worksheet: Name: - Praduman Kumar Section: - 20ITB5 UID: - 20BCS9446

The document contains questions and solutions related to SQL operations on database tables. It includes questions on creating tables, inserting values, updating columns, deleting records and using aggregate functions to retrieve summary data from tables. Code snippets are provided to demonstrate SQL commands for performing various operations like creating tables, inserting records, adding/updating/deleting columns, joining tables and using aggregate functions.

Uploaded by

Raja Raj
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)
178 views13 pages

Dbms Worksheet: Name: - Praduman Kumar Section: - 20ITB5 UID: - 20BCS9446

The document contains questions and solutions related to SQL operations on database tables. It includes questions on creating tables, inserting values, updating columns, deleting records and using aggregate functions to retrieve summary data from tables. Code snippets are provided to demonstrate SQL commands for performing various operations like creating tables, inserting records, adding/updating/deleting columns, joining tables and using aggregate functions.

Uploaded by

Raja Raj
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/ 13

DBMS WORKSHEET

Name: - Praduman kumar


Section: - 20ITB5
UID: - 20BCS9446

Ques No 1. Consider the following schema

ord_no purch_amt ord_date customer_id salesman_id


---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001

a) Create the table orders with columns order_no number type,


purch_amt number(precision, scale), ord_date date, customer_id
number and salesman_id number
b) Insert the values as given in the table.
c) Add customer name, email address and contact_number columns
in the given table
d) Add column gender in the table with a single character value.
e) Update the values of newly added columns in the records

For c & d) Add column gender in the table with a single character value.
ALTER TABLE table_name
ADD column_name datatype;
For e) UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Code:
UPDATE order1
SET gender = M, email_id = [email protected]
WHERE condition;

Ques No 2. Create table student for the given set of attributes and
implement given operations using SQL commands:

1) Create table Student (Rno, Name, DOB, Gender, Class,


College,City, Marks)
2) Insert 5 records in student table
3) Display the information of all the students
4) Display the detail structure of student table
5) Display Rno, Name and Class information of ‘Patiala’ students.
6) Display information on ascending order of marks
7) Change the marks of Rno 5 to 89.
8) Change the name and city of R.no 9.
9) Delete the information of ‘Amritsar’ city records
10) Delete the records of students where marks<30.
Note:- In this question I used Kali linux Command prompt as a
mysql Command prompt so that all output screenshot are taken
from kali linux Command Prompt

1. Find the Emloyees who were hired in the Last n months


Finding the Employees who have been hire in the last n months.
Here we get desired output by using TIMESTAMPDIFF() mysql
function
Employees
ID FName LName Gender Salary Hiredate

1 Rajveer Singh Male 30000 2017/11/05

2 Manveer Singh Male 50000 2017/11/05

3 Ashutosh Kumar Male 40000 2017/12/12

4 Ankita Sharma Female 45000 2017/12/15

5 Vijay Kumar Male 50000 2018/01/12

6 Dilip Yadav Male 25000 2018/02/26

7 Jayvijay Singh Male 30000 2018/02/18

8 Reenu Kumari Female 40000 2017/09/19

9 Ankit Verma Male 25000 2018/04/04

10 Harpreet Singh Male 50000 2017/10/10

2. Query:
Select *, TIMESTAMPDIFF(month, Hiredate, current_date()) as
DiffMonth from employees
where TIMESTAMPDIFF(month, Hiredate, current_date()) between
1 and 5 order by Hiredate desc;
3. Note: Here in query 1 and 5 are indicates 1 to n months which show
the Employees who have hired last 1 to 5 months. In this query,
DiffMonth is an extra column for our understanding which shows the
Nth months.
4. Output:

5. Find the Emloyees who hired in the Last n days


Finding the Employees who have been hired in the last n days.
Here we get desired output by using DATEDIFF() mysql function
Employees
ID FName LName Gender Salary Hiredate

1 Rajveer Singh Male 30000 2017/11/05

2 Manveer Singh Male 50000 2017/11/05


ID FName LName Gender Salary Hiredate

3 Ashutosh Kumar Male 40000 2017/12/12

4 Ankita Sharma Female 45000 2017/12/15

5 Vijay Kumar Male 50000 2018/01/12

6 Dilip Yadav Male 25000 2018/02/26

7 Jayvijay Singh Male 30000 2018/02/18

8 Reenu Kumari Female 40000 2017/09/19

9 Ankit Verma Male 25000 2018/04/04

10 Harpreet Singh Male 50000 2017/10/10

Query:

select *, DATEDIFF(current_date(), Hiredate)as


DiffDay from employees
where DATEDIFF(current_date(), Hiredate) between
1 and 100 order by Hiredate desc;
Note : Here in query 1 and 100 indicates 1 to n days which show the
Employees who have hired last 1 to 100 days. In this query DiffDay
is an extra column for our understanding which shows the Nth days.
Output:

6. Find the Employees who were hired in the Last n years


Finding the Employees who have been hired in the last n years.
Here we get desired output by using TIMESTAMPDIFF() MySQL
function
Employees
ID FName LName Gender Salary Hiredate

1 Rajveer Singh Male 30000 2010/11/05

2 Manveer Singh Male 50000 2017/11/05

3 Ashutosh Kumar Male 40000 2015/12/12


ID FName LName Gender Salary Hiredate

4 Ankita Sharma Female 45000 2016/12/15

5 Vijay Kumar Male 50000 2017/01/12

6 Dilip Yadav Male 25000 2011/02/26

7 Jayvijay Singh Male 30000 2012/02/18

8 Reenu Kumari Female 40000 2013/09/19

9 Ankit Verma Male 25000 2017/04/04

10 Harpreet Singh Male 50000 2017/10/10

Query:
select *, TIMESTAMPDIFF(year, Hiredate, current_date()) as
DiffYear from employees
where TIMESTAMPDIFF(year, Hiredate, current_date())
between 1 and 4 order by Hiredate desc;
Note: Here in query 1 and 4 are indicates 1 to n years which shows
the Employees who have hired last 1 to 4 years. In this query,
DiffYear is a extra column for our understanding which show the Nth
years.
Output:

7. Select all names that start with a given letter


Here we get desired output by using three different queries
Employees
ID FName LName Gender Salary Hiredate

1 Rajveer Singh Male 30000 2010/11/05

2 Manveer Singh Male 50000 2017/11/05

3 Ashutosh Kumar Male 40000 2015/12/12

4 Ankita Sharma Female 45000 2016/12/15

5 Vijay Kumar Male 50000 2017/01/12

6 Dilip Yadav Male 25000 2011/02/26


ID FName LName Gender Salary Hiredate

7 Jayvijay Singh Male 30000 2012/02/18

8 Reenu Kumari Female 40000 2013/09/19

9 Ankit Verma Male 25000 2017/04/04

10 Harpreet Singh Male 50000 2017/10/10

Query:
select *from employees where Fname like 'A%';

select *from employees where left(FName, 1)='A';

select *from employees where substring(FName, 1, 1)='A';


Note: Here every query will give same output and the list of
Employees who’s FName start with letter A.

Ques No 3). Create table employee(ename,ecode,dep_name,salary)


implement following operations using aggregate functions:

1. Display the number of employees working in each department.


2. Display the average salary of employees working in each
department.
3. Find the employee with highest salary in ‘Physics’ Department.

Code:-

create table employee(


emp_id number,
name varchar(20),
mobile number,
salary number
);
desc employee;

insert into employee(emp_id, name , mobile , salary) values(1,’Aditi',82365523013,35000);


insert into employee(emp_id, name , mobile , salary) values(2,’Rahul',82365523012,25000);
insert into employee(emp_id, name , mobile , salary) values(3,'Diya',82365524013,47000);
insert into employee(emp_id, name , mobile , salary) values(2,'Raja',82365523012,45500);
insert into employee(emp_id, name , mobile , salary) values(5,’Riya',82365525013,74332);
insert into employee(emp_id, name , mobile , salary) values(6,' Raj',82365526013,256422);
select * from employee;
alter table employee add address varchar(50);
update employee set address='Banglore_BTM_Electronic_City';
alter table employee rename column mobile to phone;
select * from employee where salary=47000;

Screenshots:-

You might also like