Practical Assignment SQL Assignment 1: Createtable Numeric Varchar Varchar Numeric Numeric Select From
Practical Assignment SQL Assignment 1: Createtable Numeric Varchar Varchar Numeric Numeric Select From
ASSIGNMENT 1
Q1. Write an SQL command that will create a table named Friend with the
following fields and types: - idno NUMERIC (10), fname VARCHAR (24),
address VARCHAR (30), age NUMERIC (10), giftvalue NUMERIC (10, 2).
Ans:
createtable friend (idno numeric(10), fname varchar(24), address varchar(30), age
numeric(10), giftvalue numeric(10,2));
select*from friend
Q2. Insert the following items in the table you have created
Idno
01
FName
Ram
02
Sita
Address
Dwarka sector
10
Janakpuri
block c
Age
41
Giftvalue
200
26
250
03
Rajesh
04
Ajit
05
Rita
Dwarka sector
15
Noida sector
11
Noida sector
11
23
200
35
150
40
200
Ans:
INSERTINTO friend values('01','Ram','Dwarka Sector 10','41','200')
INSERTINTO friend values('02','Sita','Janakpuri Block C','26','250')
INSERTINTO friend values('03','Rajesh','Dwarka Sector 15','23','200')
INSERTINTO friend values('04','Ajit','Noida Sector 11','35','150')
INSERTINTO friend values('05','Rita','Noida Sector 11','40','200')
select*from friend
select*from friend
Q4. Write an SQL query to display all the records where age is >40.
Ans:select*from friend where age>'40';
Q5. Write an SQL query to display Fname, Age from the table.
Ans:select Fname, Age from friend
Q6. Write an SQL query to display Fname, Age, Gift value where Age > 35 from
the table.
Ans:select Fname, Age, GiftValue from friend where Age>'35'
Q7. Write an SQL query to display all record where Gift vale is > 200 and Age
>20.
Ans:select*from friend where Giftvalue>'200'and Age>'20'
Q8. Write an SQL query to display all record where Gift vale is > 200 or Age >20.
Ans:select*from friend where Giftvalue>'200'or Age>'20'
ASSIGNMENT 2
Q1. Create a table with the following specifications
Field name
EMPID
DEPT
EMPNAME
ADDRESS
SALARY
Data type
Numeric(10)
CHAR(5)
VARCHAR(15)
VARCHAR(30)
NUMERIC(7)
DEPT
RD01
RD01
RD02
RD02
ED01
EMPNAME
Prince
Harry
Tom
Susan
Mark
106
AD01
Francis
107
GR01
Robert
108
109
110
RD03
RD03
AD01
Philip
Henry
Frank
Ans:-
ADDRESS
Park Way
Pebble Street
Park Avenue
Model Town
Victor
Crescent
Chelmsford
Park
Downtown
Cross
Park Avenue
Malibu Beach
St. Peters Lane
SALARY
15000
12000
11000
10000
10000
13000
14000
15000
14000
7000
2 Write an SQL query to display all the records where RD01is the department.
Ans:Select * from Employee where Dept='RD01'
3 Write an SQL query to display EMPNAME, DEPT, Salary from the table.
Ans:Select Empname, Dept, Salary from Employee
4 Write an SQL query to display EMPNAME, DEPT, Salary from the table
where salary is greater than 13000.
Ans:Select Empname, Dept from Employee where Salary>'13000'
5 Write an SQL query to display the record of those employees who lives in
Park Avenue.
Ans:Select * from Employee where Address= 'Park Avenue'
1. Display name, id of those employees who salary is 15000 and lives in Park
Avenue
ANS:
Select Empid, Empname from Employee where Salary='15000' and
Address='Park Avenue'
2. Find names for all employees who work for the department RD01.
Ans:
Select Empname from Employee where Dept='RD01
Ans:
Select max (Salary) from Employee
10. Name the employees and their department whose salaries are greater than
12000.
Ans:
Select Empname, Dept from Employee where Salary>'12000'
13. Name the employee in department RD03 who lives in Park Avenue
Ans:
select Empname from Employee where Dept='RD03' and Address='Park Avenue';
16. Find details of those employees whose salary is > the average salary for all
employees
Ans:
select * from Employee where salary>'12000'
Assignment 3
1. Write an SQL command that will create a table named FriendNew with the
following fields and types: idno NUMERIC(10) PRIMARY KEY, fname
VARCHAR(24), address VARCHAR(30), age NUMERIC(10) , giftvalue
NUMERIC(10,2).
Ans:
createtable FriendNew (Idno numeric(10)PrimaryKey, Fname varchar(24),
Address varchar(30), Age numeric(10), Giftvalue Numeric(10,2));
select*from FriendNew
FName
Ram
02
Sita
03
Rajesh
04
Ajit
05
Rita
Ans:
Address
Dwarka
sector 10
Janakpuri
block c
Dwarka
sector 15
Noida sector
11
Noida sector
11
Age
41
Giftvalue
200
26
250.80
23
200
35
150.50
40
200
3. Write a SQL query to display all the records whose name starts with R.
Ans:
Select*from FriendNew where Fname like'R%';
4. Write an SQL query that will insert a complete record into the Friend table with
these values for the respective fields: '123', 'Anil', 'Dwarka Sector 11', 23, 29.99.
Ans:
Insertinto FriendNew values(123,'Anil','Dwarka Sector 11', 23, 29.99);
7. Write an SQL query that will update the giftvalue to 49.99 for all people in the
Friend table whose age is equal to or greater than 31 years.
Ans:
Update FriendNew Set Giftvalue=49.99 where Age=31 OR Age>31;
8. Write an SQL query that will add a field named City to the Friend table with
datatype as varchar and size equal to 15.
Ans:
AlterTable FriendNew Add City Varchar(15);
9. Add the name of the city for all the records in the table.
Ans:
Update FriendNew Set City='Delhi';
10. Write a SQL query to display fname and age of all the records in ascending
order.
Ans:
Select Fname, Age from FriendNew Orderby Fname;
11. Write SQL query to get the cumulative giftvalue for all records.
Ans:
SelectSum(Giftvalue)from FriendNew;
12. Write a query to average of the age of all the friends under the heading
Average Age.
Ans:
SelectAvg(Age)'Average Age'from FriendNew;
13. Write a query to display the name and age of the youngest member.
Ans:
Select Fname, Age from FriendNew where Age=(SelectMin(Age)from
FriendNew);
14. Write a query to count the number of candidates whose age in more than 30
years.
Ans:
SelectCount(*)from FriendNew where Age>30;
15. Write a query to display the name and giftvalue of the record with highest gift
value.
Ans:
Select Fname, Giftvalue from Friendnew where
Giftvalue=(SelectMax(Giftvalue)from FriendNew);
16. Write an SQL query that will delete all records from the Friend table whose
idno is 123.
Ans: Deletefrom FriendNew where Idno=123;