0% found this document useful (0 votes)
3 views3 pages

14 qustion

Uploaded by

agrawalruchi912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

14 qustion

Uploaded by

agrawalruchi912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

14.

Write Sql program with 5 commands on


one or two tables.
-- Step 1: Create a student table and insert data

CREATE TABLE Class12_students (

id INT PRIMARY KEY,

name VARCHAR(50),

age INT,

marks INT

);

INSERT INTO Class12_students (id, name, age, marks) VALUES

(1, 'Ayush', 18, 85),

(2, 'Shifa', 19, 92),

(3, 'Dhruv', 18, 76),

(4, 'Anikant', 20, 88),

(5, 'Devansh', 19, 79),

(6, 'Payal', 18, 95),

(7, 'Gauri', 20, 89);

-- Display the table after inserting data

SELECT * FROM Class12_students;

-- Step 2: ALTER table to add a new attribute

ALTER TABLE Class12_students ADD COLUMN grade CHAR(1);

-- Display the table after adding a new attribute


SELECT * FROM Class12_students;

-- Step 3: UPDATE table to modify data

UPDATE Class12_students

SET grade = CASE

WHEN marks >= 90 THEN 'A'

WHEN marks >= 80 THEN 'B'

WHEN marks >= 70 THEN 'C'

ELSE 'D'

END;

-- Display the table after updating data

SELECT * FROM Class12_students;

-- Step 4: ORDER BY to display data in ascending order of marks

SELECT * FROM Class12_students

ORDER BY marks ASC;

-- Display the table after ordering

-- The ORDER BY does not change the original table; it only displays results.

-- Step 5: DELETE to remove students with marks below 80

DELETE FROM Class12_students

WHERE marks < 80;

-- Display the table after deleting records

SELECT * FROM Class12_students;


-- Step 6: GROUP BY to find min, max, sum, count, and average of marks

SELECT

MIN(marks) AS MinMarks,

MAX(marks) AS MaxMarks,

SUM(marks) AS TotalMarks,

COUNT(*) AS StudentCount,

AVG(marks) AS AvgMarks

FROM Class12_students;

-- Display the final state of the table

SELECT * FROM Class12_students;

Select age,count(*) from class12_students group by age;

Select age from class12_students group by age;

You might also like