0% found this document useful (0 votes)
159 views8 pages

IP Practicals TERM2

The document contains 6 questions with multiple SQL queries to perform operations like creating tables, inserting data, updating, deleting and selecting data from tables. Some examples of queries included are to create a student table with attributes, insert student records, delete records where marks are null, select records where marks are greater than 80, order records by descending marks, add a new column to the table, etc.

Uploaded by

zahir khan
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)
159 views8 pages

IP Practicals TERM2

The document contains 6 questions with multiple SQL queries to perform operations like creating tables, inserting data, updating, deleting and selecting data from tables. Some examples of queries included are to create a student table with attributes, insert student records, delete records where marks are null, select records where marks are greater than 80, order records by descending marks, add a new column to the table, etc.

Uploaded by

zahir khan
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/ 8

Q1) Write SQL Query for the following

a. Create a student table with student_id,name and marks as


attributes where student_id is primary key
SQL> CREATE TABLE STUDENT
( Student_id int primary key ,
Name Varchar(20) ,
MARKS INT );

b. Insert the details of a new students in the above table.


SQL>Insert into STUDENT values(1,'Ravi',75);
SQL> Insert into STUDENT values(2,'Ravi',75);
SQL> Insert into STUDENT values(3,'Raj',35);
SQL> Insert into STUDENT values(4,'Shabnam',NULL);
SQL> Insert into STUDENT values(5,'Shaila',90);
SQL> Insert into STUDENT values(6,'Avinash',50)
SQL> Insert into STUDENT values(7,'Meena',40);
SQL> Insert into STUDENT values(8,'Naren',95);
c. Delete the details of a student in the above table where marks is
null
SQL> DELETE FROM STUDENT WHERE MARKS IS NULL;
d. Use the select command to get the details of the students with
marks more than 80.
SQL>SELECT * FROM STUDENT WHERE MARKS>80;
e. Find the min, max, sum, and average of the marks in a student
marks table
SQL>SELECT MIN(MARKS),MAX(MARKS),SUM(MARKS)FROM
STUDENT;
f. Write a SQL query to order the (student ID, marks) table in
descending order of the marks
SQL>SELECT Student_id ,MARKS FROM STUDENT ORDER BY
Student_id ,MARKS DESC;
g. Add a new column to the student table named fees and data
type integer
SQL>ALTER TABLE STUDENT
ADD FEES INT;
Q2). Write SQL Query for the following
a. Create a student table with StudentID, Name and Marks as
attributes where StudentId is primary key
SQL> CREATE TABLE STUDENT
( Student_id CHAR(3) primary key ,
Name Varchar(20) ,
MARKS INT );
b. Insert details of a new student in the above table
SQL> Insert into STUDENT values(‘A11’,'Ram',75);
SQL> Insert into STUDENT values(‘A12’,'Rahul',75);
SQL> Insert into STUDENT values(‘A13’,'Radhika',35);
SQL> Insert into STUDENT values(‘A14’,'Ruhika',NULL);
SQL> Insert into STUDENT values(‘A15’,'Ria',90);
SQL> Insert into STUDENT values(‘A16’,'Anisha',50)
SQL> Insert into STUDENT values(‘A17’,'Sheena',40);
SQL> Insert into STUDENT values(‘A18’,'Anjali',95);
c. Delete the student details where studentID is ‘A11’
SQL> DELETE FROM STUDENT WHERE Student_id =’A11’;
d. Use SELECT command to get details of the students where marks
within 45 & 59
SQL> SELECT * FROM STUDENT WHERE MARKS BETWEEN 45 AND 59;
e. Write a query to count the no of records in the table.
SQL>SELECT COUNT(*) AS ‘TOTAL’ FROM STUDENT;
f. Write an SQL query to obtain the Student marks in descending
order.
SQL> SELECT * FROM STUDENT ORDER BY MARKS DESC;
g. Change the size to 25 for the name field.
SQL> ALTER TABLE STUDENT MODIFY Name VARCHAR(25);
Q3) Write SQL Query for the following
a. Create a student table with student_id,name ,marks and grade as
attributes where student_id is primary key(add NOT NULL
constraint to field marks)
SQL> CREATE TABLE STUDENT
( Student_id CHAR(3) primary key ,
Name Varchar(20) ,
Marks INT NOT NULL, Grade VARCHAR(5);
b. Insert the details of new students in the above table.
SQL> Insert into STUDENT values(‘B11’,'Ram',75,’B1’);
SQL> Insert into STUDENT values(‘B12’,'Rahul',78,’B1’);
SQL> Insert into STUDENT values(‘B13’,'Deemetrius',35,’D’);
SQL> Insert into STUDENT values(‘B14’,'Ruhika',92,’A1’);
SQL> Insert into STUDENT values(‘B15’,'Ria',90,’A2’);
SQL> Insert into STUDENT values(‘B16’,'Anisha',50,NULL)
SQL> Insert into STUDENT values(‘B17’,'Zachaarias',42,’C2’);
SQL> Insert into STUDENT values(‘B18’,'Anjali',95,’A1’);
c. Write a query to increase marks by 10 where marks below 45.
SQL>UPDATE STUDENT SET Marks=Marks+10 WHERE Marks<45;
d. Write select command to get the details of the students grade
wise
SQL> SELECT * FROM STUDENT GROUP BY Grade;
e. Write query to find the average marks where grade is not null.
SQL>SELECT AVG(Marks) FROM STUDENT WHERE Grade IS NOT
NULL;
f. Write a SQL query to order the (student ID, marks) table in
descending order of the marks
SQL> SELECT Student_id ,MARKS FROM STUDENT ORDER BY Student_id
,MARKS DESC;
g. Find the total marks of students where name ends with ‘s’ having
10 characters
SQL>SELECT SUM(Marks) FROM STUDENT WHERE length(Name)=10
AND Name LIKE “ %s”;

4. Write SQL Query for the following


a. Create a customer table with Cust_id, Name, location, ord_id,
delivery status (T/F) as attributes where Cust_id is primary key.
SQL > CREATE TABLE CUSTOMER
(Cust_id int primary key,
Name varchar (20), Location varchar (20),
ord_id int, Delivery status CHAR (1));
b. Insert the details of a new customers in the above table.
SQL > Insert into CUSTOMER values (101,'Rahul', 'Goa','3005','T');
SQL > Insert into CUSTOMER values (102,'Meera', 'Delhi','3006','F');
SQL > Insert into CUSTOMER values (103,'Priya', 'Pune','3007','F');
SQL > Insert into CUSTOMER values (104,'Ishani', 'Delhi’,'3008','T');
SQL > Insert into CUSTOMER values (105,'Aditya', 'Pune','3009','T');
SQL > Insert into CUSTOMER values (106,'Hardik', 'Kochi', '3010','F');
SQL > Insert into CUSTOMER values (107,'Varsha', 'Delhi','3011','F');
SQL > Insert into CUSTOMER values (108,'Maya', 'Goa','3012','T');
SQL > Insert into CUSTOMER values (109,'Avinash', 'Pune','3013','T');
SQL > SELECT * FROM CUSTOMER;
c. Delete the details of customers whose items are not delivered.
SQL > DELETE FROM CUSTOMER WHERE Delivery Status = 'F’
d. Use the select command to get the details of the customers
located in ‘DELHI’.
SQL > SELECT * FROM CUSTOMER WHERE Location = 'Delhi';
e. Find location where number of customers are maximum.
SQL> SELECT Location, MAX(COUNT(Cust_ID)) FROM CUSTOMER
GROUP BY Location;
f. Write a SQL query to order the customer table delivery status
wise.
SQL> SELECT* FROM CUSTOMER ORDER BY Delivery status ;
g. Select the cust_id, name from the customer table where name has
‘a’ anywhere in the string
SQL > SELECT cust_Id , name FROM CUSTOMER WHERE name LIKE
'%a%';
5. Write SQL Query for the following
a. Create a student table with Admno, stud_name, d_o_b, Stream
and Stipend as attributes where Admno is primary key
SQL > CREATE TABLE STUDENT
(Admno INT PRIMARY KEY,
Stud_name VARCHAR (20),
d_o_b DATE,
Stream VARCHAR (5),
Stipend INT);
b. Insert the details of students in the above table.
SQL>INSERT INTO STUDENT VALUES (125,'Anjali',’2005-
08-02’,'MPCIP',2000);
SQL>INSERT INTO STUDENT VALUES (123,'Ruhika',’2003-
07-07’,'MPCIP',2500);
SQL>INSERT INTO STUDENT VALUES (124,'Ria',’2004-
05-23’,'BiPCIP',3000);
SQL>INSERT INTO STUDENT VALUES (122,'Anisha',’2001-
09-12’,'MEC',2700);
SQL>INSERT INTO STUDENT VALUES (121,'Radhika',’1999-
02-25’,'CEC',1900);
c. Write a query to arrange the table Admno wise.
SQL > SELECT* FROM STUDENT ORDER BY Admno;
d. Use the select command to get the details of the students who were
born before 2004.
SQL > SELECT * FROM STUDENT WHERE YEAR(d_o_b)<2004;
e. Write a query to display a report to display yearly stipend (assuming
stipend paid month wise).
SQL>SELECT Admno, Stud_name , d_o_b ,Stream , Stipend*12 AS
yearly_stipend FROM STUDENT;
f. Write a SQL command to display sum of stipend paid stream wise.
SQL> SELECT Stream, SUM(Stipend) FROM STUDENT GROUP BY
Stream;
g. Increase the stipend by 1000 where stipend paid is less than 3000.
SQL> UPDATE STUDENT SET Stipend =Stipend+1000 WHERE
Stipend<3000;
6. Write SQL Query for the following
a) Create a customer table with cust_id, name, location, ord_id,
delivery status(T/F) ,total_cost as attributes where cust_id is primary
key .
SQL > CREATE TABLE CUSTOMER
(Cust_id int primary key,
Name varchar (20), Location varchar (20),
ord_id int, delivery_status char (1),
total_cost int);
b. Insert the details of a new customers in the above table.
SQL > Insert into CUSTOMER values (101,'Priya', 'Goa','3005','T',
'2500');
SQL > Insert into CUSTOMER values (102,'Hardik', 'Delhi','3006','F',
'1600');
SQL > Insert into CUSTOMER values (103,'Ishani', 'Pune','3007','F',
'1900');
SQL > Insert into CUSTOMER values (104,'Aditya', 'Delhi ','3008','T',
'4500');
SQL > Insert into CUSTOMER values (105,'Rishab', 'Pune','3009','T',
'8900');
SQL > Insert into CUSTOMER values (106,'Avinash','Pune', '3010','F',
'6500');
SQL > Insert into CUSTOMER values (107,'Meera', 'Delhi','3011','F',
'4300');
SQL > Insert into CUSTOMER values (108,'Mayank', 'Goa','3012','T',
'3000');
SQL > Insert into CUSTOMER values (109,'Akira', 'Pune','3013','T',
'2500');
SQL > SELECT * FROM CUSTOMER;
c) Write a query to give 10% discount to customers whose items are
not delivered.
SQL > UPDATE CUSTOMER SET total_cost = total_cost-(total_cost*0.1)
WHERE delivery_status= ‘F’
d. Use the select command to get the details of the customers
located in ‘DELHI’.
SQL > SELECT * FROM CUSTOMER WHERE Location = 'Delhi';
e. Find location where number of customers are maximum.
SQL > SELECT Location, MAX(COUNT(Cust_ID)) FROM Student GROUP
BY Location ;
f. Write a SQL query to order the customer table delivery status
wise.
SQL> SELECT* FROM CUSTOMER ORDER BY Delivery Status;
g. Select the cust_id, name from the customer table where name has
‘a’ anywhere in the string
SQL > SELECT cust_Id , name FROM CUSTOMER WHERE name LIKE
'%a%'

You might also like