My SQL Practical
My SQL Practical
Q1. Write a query to Create a database SCHOOL and Create a table DEPARTMENT.
CREATE DATABASE SCHOOL;
USE SCHOOL;
CREATE TABLE DEPARTMENT
(
DEPT_ID INT PRIMARY KEY,
DEPT_NAME VARCHAR(50),
STRENGTH INT
);
DESCRIBE DEPARTMENT;
Output
Field Type Null Key Default Extra
DEPT_ID int NO PRI NULL
DEPT_NAME varchar(50) YES NULL
STRENGTH int YES NULL
Q2. Write a query to Create a table TEACHER.
DESCRIBE DEPARTMENT;
Output
Field Type Null Key Default Extra
DEPT_ID int NO PRI NULL
DEPT_NAME varchar(50) YES NULL
STRENGTH int YES NULL
DEPT_ADDRESS varchar(50) YES NULL
Q5. Write a query to ensure that the salary entered must be greater and equal to zero.
ALTER TABLE
TEACHER
ADD CONSTRAINT CK_1
CHECK (SALARY>=0.0);
Q6. Write a query to add foreign key for DEPT_ID to reference from DEPT_ID of DEPARTMENT table.
Output
Output
Q.8. Display the teacher informa on with their respec ve department name.
UPDATE TEACHER
SET ADDRESS='CUTTUCK'
WHERE TEACHER_ID=9;
Q11. Write a query to display the details of the teachers with salary between 45000 and 60000.
SELECT * FROM TEACHER
WHERE SALARY BETWEEN 45000 AND 60000;
Output
TEACHER_ID TEACHER_NAME DOB DOJ ADDRESS SALARY DEPT_ID
1 AKASH 23-04-1996 15-07-2021 BHUBANESWAR 50000 101
5 SUBHASMITA 18-07-1997 22-06-2022 NAYAGARH 48000 103
Q12. Write a query to display the details of the teachers with DEPT_ID 101 or DEPT_ID 104.
SELECT TEACHER_NAME, DOB, DOJ, SALARY, DEPT_ID
FROM TEACHER
WHERE DEPT_ID IN (101,104);
Output
TEACHER_NAME DOB DOJ SALARY DEPT_ID
AKASH 23-04-1996 15-07-2021 50000 101
ARUN 20-08-1992 15-07-2019 65000 101
SUNIL 25-01-1993 24-07-2019 65000 104
PRAKASH 12-11-1988 16-09-2018 73000 104
Q13. Write a query the details of the teachers with name star ng with A.
SELECT * FROM TEACHER
WHERE TEACHER_NAME LIKE 'A%';
Output
TEACHER_ID TEACHER_NAME DOB DOJ ADDRESS SALARY DEPT_ID
1 AKASH 23-04-1996 15-07-2021 BHUBANESWAR 50000 101
2 ARUN 20-08-1992 15-07-2019 KEONJHAR 65000 101
6 AMAN 17-12-1989 07-09-2018 BOUDH 73000 103
Q14. Write a query to display the teacher name with salary and 30 percent of salary as Bonus.
SELECT TEACHER_NAME, SALARY, SALARY*0.3 AS BONUS
FROM TEACHER;
Output
TEACHER_NAME SALARY BONUS
AKASH 50000 15000
ARUN 65000 19500
MAHESH 73000 21900
SONALI 73000 21900
SUBHASMITA 48000 14400
AMAN 73000 21900
SUNIL 65000 19500
PRAKASH 73000 21900
RASMITA 65000 19500
Q16. Write a query to display the total salary of the Teachers dept wise.
SELECT DEPT_ID, SUM(SALARY) AS TOTAL_SALARY
FROM TEACHER
GROUP BY DEPT_ID;
Output
DEPT_ID TOTAL_SALARY
101 115000
102 146000
103 121000
104 138000
105 65000
Q17. Write a query to display teachers with salary from highest to lowest.
SELECT * FROM TEACHER
ORDER BY SALARY DESC;
Output
TEACHER_ID TEACHER_NAME DOB DOJ ADDRESS SALARY DEPT_ID
3 MAHESH 1994-04-11 2018-09-11 DHENKANAL 73000.00 102
4 SONALI 1993-05-21 2018-09-18 BHUBANESWAR 73000.00 102
6 AMAN 1989-12-17 2018-09-07 BOUDH 73000.00 103
8 PRAKASH 1988-11-12 2018-09-16 CUTTACK 73000.00 104
2 ARUN 1992-08-20 2019-07-15 KEONJHAR 65000.00 101
7 SUNIL 1993-01-25 2019-07-24 ROURKELA 65000.00 104
9 RASMITA 1990-02-26 2019-07-12 CUTTUCK 65000.00 105
1 AKASH 1996-04-23 2021-07-15 BHUBANESWAR 50000.00 101
5 SUBHASMITA 1997-07-18 2022-06-22 NAYAGARH 48000.00 103
Q18. Write a query to display the teachers with the dept name displayed from A to Z and with in each
department display the teachers name from Z to A.
SELECT DEPT_NAME, TEACHER_ID, TEACHER_NAME, SALARY
FROM TEACHER, DEPARTMENT
WHERE TEACHER.DEPT_ID = DEPARTMENT.DEPT_ID
ORDER BY DEPT_NAME , TEACHER_NAME DESC;
Output
DEPT_NAME TEACHER_ID TEACHER_NAME SALARY
BIOLOGY 7 SUNIL 65000.00
BIOLOGY 8 PRAKASH 73000.00
CHEMISTRY 4 SONALI 73000.00
CHEMISTRY 3 MAHESH 73000.00
ENGLISH 9 RASMITA 65000.00
MATHEMATICS 5 SUBHASMITA 48000.00
MATHEMATICS 6 AMAN 73000.00
PHYSICS 2 ARUN 65000.00
PHYSICS 1 AKASH 50000.00
Q19. Write a query to display the no. of employees with department name with in each department
displayed department wise.
SELECT DEPT_NAME, COUNT(TEACHER_ID) AS NO_OF_EMPLOYEES
FROM TEACHER, DEPARTMENT
WHERE TEACHER.DEPT_ID=DEPARTMENT.DEPT_ID
GROUP BY DEPT_NAME;
Output
DEPT_NAME NO_OF_EMPLOYEES
PHYSICS 2
CHEMISTRY 2
MATHEMATICS 2
BIOLOGY 2
ENGLISH 1
Q20. Write a query to display the average salary with department name of the departments with average
salary greater than 65000.
SELECT DEPT_NAME, AVG(SALARY) AS AVERAGE_SALARY
FROM TEACHER, DEPARTMENT
WHERE TEACHER.DEPT_ID=DEPARTMENT.DEPT_ID
GROUP BY DEPT_NAME
HAVING AVG(SALARY)>65000;
Output
DEPT_NAME AVERAGE_SALARY
CHEMISTRY 73000
BIOLOGY 69000