0% found this document useful (0 votes)
19 views

Experiment 1 & 2 DBMS

Uploaded by

gakaj57157
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)
19 views

Experiment 1 & 2 DBMS

Uploaded by

gakaj57157
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/ 6

Experiment 1: Create Table Student and insert 10 values in the table and list

them.

CREATE TABLE student(roll_no numeric(10), fname


varchar(20) NOT NULL, lname
varchar(20) NOT NULL, dept_name
varchar(3), total_credit numeric(3),
PRIMARY KEY(roll_no));
INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210055', 'Ankit', 'Sengar', 'CSE',
'65');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210056', 'Alok', 'Ojha', 'EE', '75');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210057', 'Shreyas', 'Negi', 'ME', '70');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210058', 'Harshit', 'Singh', 'CE', '55');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210059', 'Prince', 'Raghav', 'EC', '75');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210060', 'Utkarsh', 'Singh', 'CSE', '85');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210061', 'Dipika', 'Chauhan', 'CSE',
'63');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210062', 'Prakhar', 'Srivastav', 'EC',
'72');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210063', 'Prakhar', 'Srivastav', 'ME',
'81');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210064', 'Rahul', 'Yadav', 'CE', '55');

INSERT INTO student (roll_no, fname, lname, dept_name, total_credit) VALUES('1819210065', 'Antra', 'Gupta', 'CSE', '85');
Experiment 2: On the basis of table student perform the SQL queries

Questions:
1. Write an SQL query to fetch all the data from database.

Ans. SELECT * FROM student;

2. Write an SQL query name of students having department CSE.


Ans. SELECT fname,lname FROM student WHERE dept_name = 'CSE';
3. Write an SQL query name of students have total credit less than 60.
Ans. SELECT fname,lname FROM student WHERE total_credit < 60;

4. Write an SQL query to insert data of student Deepesh Rao having roll no.
1819210093 and total credit 78.
Ans. INSERT INTO student (roll_no, fname, lname, dept_name, total_credit)
VALUES('1819210093','Deepesh','Rao','ME','78');

5. Write an SQL query to find names of students starting with letter A.


Ans. SELECT fname,lname FROM student WHERE fname LIKE 'A%';

6. List the name of the Students whose first name has only five characters and
starting with ‘A’ and ending with ‘a’.
Ans. SELECT fname,lname FROM student WHERE LENGTH(fname) = 5 AND
fname LIKE 'A%' AND fname LIKE '%a';

7. List the name , roll no. whose depatment is CSE and first name starts with
‘A’.
Ans. SELECT fname,lname,roll_no FROM student WHERE dept_name = 'CSE'
AND fname LIKE 'A%';

8. List the names of those Students whose name have second alphabet ‘n’ in their
names.
Ans. SELECT fname,lname FROM student WHERE fname LIKE '_N%' OR
'_n%';

9. List the name, department of those Students whose first name having a
characterset ‘an’ together.
Ans. SELECT fname,lname FROM student WHERE fname LIKE '%An%';

10. Write an sql query to print the fname and lname from table into a single column
Complete_name. A space char should separate them.
Ans. SELECT fname || ' ' || lname AS Complete_name FROM student;

11. Write a query to print first name,last name and total credit of those student
having department name computer science.
Ans. SELECT fname,lname,total_credit FROM student WHERE dept_name =
'CSE';
12. Write a query to display the roll no. and department name of those student
whose first name start with D.
Ans. SELECT roll_no,dept_name FROM student WHERE fname LIKE 'D%';

13. Write a query to drop the total credit of those student whose department name
is mechanical and last name start with D.
Ans. UPDATE student SET total_credit = NULL WHERE dept_name = 'ME'
AND lname LIKE 'D%';

14. Write a query to print the number of student and their roll no. and department
whose credit is more than 60%.
Ans. SELECT COUNT(*) AS num_students,
roll_no,
dept_name
FROM student
WHERE total_credit > 60
GROUP BY dept_name;

15. Write a query to delete the record of students whose branch is computer
science or first name start with S.
Ans. DELETE FROM student WHERE dept_name = 'CSE' or fname LIKE
'S%';

16. Write a SQL statement to drop the column total credit from the table .
Ans. ALTER TABLE student
DROP COLUMN total_credit;

17. Write a SQL statement to change the name of the column dept_name to
dept , keeping the data type and size same.
Ans. ALTER TABLE student
RENAME COLUMN dept_name TO dept;
18. Write a SQL statement to add column named year_of_graduation in the table.
Ans. ALTER TABLE student
ADD COLUMN year_of_graduation INT;

19. Write a SQL statement to drop the column named


year_of_graduation from table.
Ans. ALTER TABLE student
DROP COLUMN year_of_graduation;

20. Write a SQL statement to rename the table student to student data.
Ans. ALTER TABLE student
RENAME TO student_data;

You might also like