You are on page 1of 3

1.

Create tables of
Department with columns Dept_ID, Dept_Name, where Dept_ID is primary key
Teacher with column Teacher_ID, First_Name, Last_Name, Gender, Salary,
Date_of_Birth, Dept_No, where Teacher_ID is primary key,Dept_no is foreign key and
salary is in decimal type with default value 40000.
CREATE TABLE Department (Dept_ID INTEGER PRIMARY KEY, Dept_Name
VARCHAR(20) NOT NULL);
CREATE TABLE Teacher (Teacher_ID INTEGER PRIMARY KEY, First_Name
VARCHAR(20) NOT NULL,Last_Name VARCHAR(20),Gender CHAR(1),Salary
DECIMAL(10,2) DEFAULT 40000,Date_of_Birth DATE,Dept_No INTEGER,
FOREIGN KEY (Dept_No) REFERENCES Department(Dept_ID));
2. Insert rows in both tables as following

3. To retrieve all the information about Teacher with ID=101


SELECT *FROM Teacher WHERE Teacher_ID=101;
4. To find the names of all teachers earning more than 50000.
SELECT First_Name,Last_Name FROM Teacher WHERE salary > 50000;
5. To display Teacher_ID,First_Name,Last_Name and Dept_No of teachers who belongs
to department number 4 or 7.
SELECT Teacher_ID,First_Name,Last_Name, Dept_No FROM Teacher WHERE
Dept_No = 4 OR Dept_No = 7;
6. To retrieve names of all the teachers and the names and numbers of their respective
departments.
SELECT First_Name, Last_Name, Dept_ID, Dept_Name FROM Teacher, Department;
7. To retrieve names of all the teachers and the names and numbers of their respective
departments based on the equality between Dept_No and Dept_ID columns.
SELECT First_Name, Last_Name, Dept_ID, Dept_Name FROM Teacher, Department
WHERE Dept_ID=Dept_No;
8. Modify the column dept_no by dept_id in teacher table.
9. To retrieve names of all the teachers who belong to Hindi department.
SELECT First_Name, Last_Name FROM Teacher AS T, Department AS D WHERE
D.Dept_ID = T. Dept_ID AND Dept_Name="Hindi";
10. list all the Department numbers corresponding to departments having male teachers.
SELECT Dept_No FROM Teacher; WHERE GENDER ='M';
11. To retrieve names of all the teachers starting from letter 'S'.
SELECT First_Name FROM Teacher WHERE First_Name LIKE "S%";
12. To retrieve names of all the teachers having 6 characters in the first name and
starting with 'S'.
SELECT First_Name FROM Teacher WHERE First_Name LIKE "S_ _ _ _ _";
13. To retrieve names of all the teachers having at least 6 characters in the first name.
SELECT First_Name FROM Teacher WHERE First_Name LIKE "_ _ _ _ _ _%";
14. To list the names of teachers in alphabetical order.
SELECT First_Name, Last_Name FROM Teacher ORDER BY First_Name, Last_Name;
15. To list the names of all the Departments in the descending order of their names.
SELECT Dept_Name FROM Department ORDER BY Dept_Name DESC;
16. To retrieve the names and department numbers of all the teachers ordered by the
Department number and within each department ordered by the names of the
teachers in descending order.
SELECT First_Name, Last_Name, Dept_No FROM Teacher ORDER BY Dept_No ASC,
First_Name DESC, Last_Name DESC;
17. To retrieve all the details of those employees whose last name is not specified.
SELECT * FROM Teacher WHERE Last_Name IS NULL;
18. To retrieve the names of all the departments having female teachers.
SELECT DISTINCT Dept_Name FROM Department , Teacher WHERE Dept_ID =
Dept_No AND Gender='F';
19. To find total salary of all the teachers .
SELECT SUM(Salary) AS Total_Salary FROM Teacher;
20. To find the maximum and minimum salary.
SELECT MAX(Salary) AS Max_Salary, MIN(Salary) AS Min_Salary FROM Teacher;
21. To count the number of teachers earning more than Rs 40000.
SELECT COUNT(Salary) FROM Teacher WHERE Salary > 40000;
22. To retrieve the number of teachers in “Computer Science” Department.
SELECT COUNT(*) AS No_of_Computer_Science_Teachers FROM Department,
Teacher WHERE Dept_Name = "Computer Science"AND Dept_No=Dept_ID;
23. find the number of teachers teaching in each Department
SELECT Dept_No, COUNT(*) AS No_of_Teachers FROM Teacher GROUP BY Dept_No;
24. find those departments which have more than one teacher.
SELECT Dept_No, Dept_Name, COUNT(*) AS No_of_Teachers FROM Teacher,
Department WHERE Dept_No=Dept_ID GROUP BY Dept_No HAVING COUNT(*) > 1;
2. Consider the following Employee table:
Table Name: Employee

The primary key of this table is Employee_ID

Write SQL commands for the following:


(a) Create the above table.
(b) Insert values as shown above.
(c) Delete the Employee having Employee_ID 1217.
(d) Update the salary of “Amyra” to 40000.
(e) Alter the table Employee so that NULL values are not allowed for Age column.
(f) Write a query to display names and salaries of those employees whose salary are
greater than 20000.
(g) Write a query to display details of employees who are not getting any bonus.
(h) Write a query to display the names of employees whose name contains “a” as the
last alphabet.
(i) Write a query to display the name and Job title of those employees whose
Manager_ID is 1201.
(j) Write a query to display the name and Job title of those employees whose
Manager is “Amyra”.
(k) Write a query to display the name and Job title of those employees aged between
26 years and 30 years (both inclusive)

You might also like