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

SQL Student Table

The document contains SQL statements and queries that create tables, insert data, perform joins, and more. It defines tables for students, courses, and salaries with various fields. Data is inserted and queries retrieve aggregate values and find highest/second highest percentages. Stored procedures and triggers are also created to calculate totals and percentages on student records.

Uploaded by

Daric Olive
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

SQL Student Table

The document contains SQL statements and queries that create tables, insert data, perform joins, and more. It defines tables for students, courses, and salaries with various fields. Data is inserted and queries retrieve aggregate values and find highest/second highest percentages. Stored procedures and triggers are also created to calculate totals and percentages on student records.

Uploaded by

Daric Olive
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

INSERT INTO student(name,sub1,sub2,sub3) VALUES('alan',20,77,54);

INSERT INTO student(name,sub1,sub2,sub3) VALUES('bharath',50,57,74);


INSERT INTO student(name,sub1,sub2,sub3) VALUES('chethan',70,98,46);
INSERT INTO student(name,sub1,sub2,sub3) VALUES('dhanin',84,65,86);
INSERT INTO student(name,sub1,sub2,sub3) VALUES('erika',72,59,46);
INSERT INTO student(name,sub1,sub2,sub3) VALUES('fahim',84,38,86);
INSERT INTO student(name,sub1,sub2,sub3) VALUES('gopal',63,58,46);
INSERT INTO student(name,sub1,sub2,sub3) VALUES('harish',84,94,84);

SELECT * FROM student;


------------------------------------------
CREATE TABLE Student(
studentID INT NOT NULL AUTO_INCREMENT,
FName VARCHAR(20),
LName VARCHAR(20),
Address VARCHAR(30),
City VARCHAR(15),
Marks INT,
PRIMARY KEY(studentID)
);

INSERT INTO Student(Fname,Lname, Address, City, Marks)


VALUES('Ashok','Sinha','#231, Megarth Road','Mumbai',538);
INSERT INTO Student(Fname,Lname, Address, City, Marks)
VALUES('Bharath','Singh','#11,24th cross, IG Road','Pune',385);

INSERT INTO Student(Fname,Lname, Address, City, Marks) VALUES('Bhuvi','Kumar','#28,


Dollars Colony','GOA',578);

INSERT INTO Student(Fname,Lname, Address, City, Marks)


VALUES('Dhruv','Lekhan','#626, Velli pakkam','Chennai',455);

INSERT INTO Student(Fname,Lname, Address, City, Marks)


VALUES('Prakash','Thambe','#90, Bridge Apartments, JP nagar','Bengaluru',488);

---------------------------------------------

truncate TABLE student;

actual trigger

CREATE TRIGGER cal


before INSERT
ON student
FOR EACH ROW
SET new.total = new.sub1+new.sub2+new.sub3, new.per = (new.total/300)*100;

actual stored procedure

DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;

SHOW TABLE STATUS FROM `sql_tutorial`;


SHOW FUNCTION STATUS WHERE `Db`='sql_tutorial';
SHOW PROCEDURE STATUS WHERE `Db`='sql_tutorial';
SHOW TRIGGERS FROM `sql_tutorial`;

CREATE TABLE course(


cid INT NOT NULL AUTO_INCREMENT,
cname VARCHAR(20),
PRIMARY KEY(cid)
);

INSERT INTO course(cname) VALUES('maths');

INSERT INTO course(cname) VALUES('physics');

INSERT INTO course(cname) VALUES('db');

INSERT INTO course(cname) VALUES('sql');

INSERT INTO course(cname) VALUES('os');

INSERT INTO course(cname) VALUES('algorithm');

INSERT INTO course(cname) VALUES('or');

create TABLE student_course(


id INT REFERENCES student(id),
cid INT REFERENCES course(cid)
);

SELECT * FROM student_course;

INSERT INTO student_course VALUES(2,2);


INSERT INTO student_course VALUES(3,3);
INSERT INTO student_course VALUES(4,4);
INSERT INTO student_course VALUES(5,5);
INSERT INTO student_course VALUES(6,6);
INSERT INTO student_course VALUES(7,7);
INSERT INTO student_course VALUES(8,8);

select NAME FROM student WHERE id IN(


SELECT id FROM student_course WHERE cid IN(
SELECT cid FROM course WHERE cname ='db' OR cname='maths'
)
);

second highest %

SELECT MAX(per)
FROM student WHERE per NOT IN(
SELECT MAX(per) FROM student
);
CREATE TABLE info(
stu_id INT NOT NULL AUTO_INCREMENT,
stu_name VARCHAR(20),
phone VARCHAR(10),
address VARCHAR(30),
PRIMARY KEY(stu_id),
INDEX idx_name(stu_name)
);

SELECT * FROM info;

stored procedure working in heidiSQL


------------------------------------

DELIMITER //
CREATE PROCEDURE student_info()
BEGIN
SELECT Avg(per) AS percentage_average
FROM student;
END //
DELIMITER ;

CALL student_info();

-----------------------------------------

salary table
------------
CREATE TABLE salary(
id INT NOT NULL AUTO_INCREMENT,sql_tutorialsql_tutorial
NAME VARCHAR(20),
student_id INT NOT NULL,
salary INT,
variable REAL,
PRIMARY KEY(id)
);

INSERT INTO salary(name,student_id,salary,variable) VALUES('alan',1,7711,.54);


INSERT INTO salary(name,student_id,salary,variable) VALUES('bharath',2,5327,.74);
INSERT INTO salary(name,student_id,salary,variable) VALUES('chethan',3,9438,.46);
INSERT INTO salary(NAME,student_id,salary,variable) VALUES('dhanin',4,6425,.86);
INSERT INTO salary(name,student_id,salary,variable) VALUES('erika',5,5942,.46);
INSERT INTO salary(name,student_id,salary,variable) VALUES('fahim',6,3358,.86);
INSERT INTO salary(name,student_id,salary,variable) VALUES('gopal',7,5428,.46);
INSERT INTO salary(name,student_id,salary,variable) VALUES('harish',8,9452,.84);

SELECT * FROM salary;

You might also like