SQL_practicesheet_ANSKEY
SQL_practicesheet_ANSKEY
2
Create table StudAdmin(Adm_No int primary key, First_Name varchar(15), Last_Name
varchar(15), DOB date);
2. Insert a record with the given details in “students” table
(2013, Jatin, Kumar, 05-02-1998)
(2011, Mona, Sinha, 24-09-2004)
3
1. Display the details of all students from Students table.
Select * from students;
2. Display Rollno, Name and class of all female students.
Select rollno,name,class from students where gender=’f’;
3. Display the student Name, stream and date of birth of students born after 9th Jan 2011.
Select name,stream,dob from students where dob>’2011-01-09’;
4. Display the details of students whose marks is given as null.
Select * from students where marks is null;
5. Display the details of students whose stream is not BioIP.
Select * from students where stream <> ‘bioip’;
6. Display the details of students whose mark is in the range 48 to 50.
Select * from students where marks between 48 and 50;
7. Display Rollno, name and stream of students whose marks is greater than or equal to
47.
Select rollno,name,stream from students where marks>=47;
8. Display distinct stream from students table.
Select distinct stream from students;
9. Display the details of all male students of 12A.
Select * from students where gender=’m’ and class=’12a’;
10. Display the details of those students who scored above 47 or belong to computer
stream.
Select * from students where marks>47 or stream=’computer’;
11. Display Name and class of those students not born between 2nd August 2010 and 8th
August 2011.
Select name, class from students where dob between ‘2010-08-02’ and ‘2011-08-08’;
12. Display the details of students who belong to computer and BioIP stream.
Select * from students where stream in (‘computer’,’bioip’);
13. Display the details of students who belong to computer , BioIP and CommerceIP
stream.
Select * from students where stream in (‘computer’,’bioip’,’commerceip’);
14. Display rollno, name and class of students in ascending order of student name.
4
Select rollno,name,class from students order by name;
15. Display the details of students who does not belong to computer and BioIP stream.
Select * from students where stream not in (‘computer’,’bioip’);
16. Display Name, gender and class of students whose name starts with "S”
Select name,gender,class from students where name like “s%”;
17. Display Name, gender and class of students whose name starts with "A” followed by
3 characters.
Select name,gender,class from students where name like “A_ _ _;
18. Display the Name and Date of birth of female students. Display DOB with column
heading as Date_of_Birth
Select name,dob as Date_of_Birth from students where gender=”F”;
19. Display the details of students who scored marks above 45 in descending order of
marks.
Select * from students where marks>45 order by marks desc;
20. Display the details of students who belong to 12A class and with stream as computer.
Select * from students where class=”12a” and stream=”computer”;
Activity 1
SQL Query on Student Table
SQL Query
5
Table: Employee
Table: Department
SQL Query
1. Select Empno,EmpName, E.DeptId,Designation, Location, DeptName from
Employee E, Department D where E.DeptId=D.DeptId;
Or
Select * from employee NATURAL JOIN department;
2. Select Empno,EmpName, Designation, Location from Employee E, Department D
where E.DeptId=D.DeptId and DeptName=’Sales’;
or
select empno,empname,designation,location from employee natural join department
where deptname="sales";
7
MySQL Activity – 1
1. SELECT NAME, GENDER, CLASS FROM STUDENT WHERE NAME LIKE “%SHARMA”;
2. SELECT MIN(MARKS), STREAM FROM STUDENT GROUP BY STREAM;
3. SELECT NAME,CLASS,MARKS FROM STUDENT WHERE NAME LIKE ‘AR_ _ _’;
4. SELECT ROLLNO,NAME, MARKS FROM STUDENT WHERE MARKS BETWEEN 45 AND
48;
5. SELECT NAME,CLASS,MARKS FROM STUDENT WHERE CLASS=’12A’ ORDER BY
MARKS;
MySQL Activity – 2
1. SELECT COUNT(*),DEPTNAME FROM EMPLOYEE E,DEPARTMENT D WHERE
E.DEPTID=D.DEPTID GROUP BY DEPTNAME;
2. SELECT * FROM EMPLOYEE WHERE DEPTID=201 OR DEPTID=301;
3. SELECT EMPNO,EMPNAME FROM EMPLOYEE WHERE EMPNAME LIKE "%V%";
4. SELECT DISTINCT(DESIGNATION) FROM EMPLOYEE;
5. SELECT * FROM EMPLOYEE WHERE DESIGNATION IN('CLERK','EXECUTIVE');
MySQL Activity – 1
1. SELECT NAME, GENDER, CLASS FROM STUDENT WHERE NAME LIKE “%SHARMA”;
8
4. SELECT ROLLNO,NAME, MARKS FROM STUDENT WHERE MARKS BETWEEN 45 AND
48;
MySQL Activity – 2
1. SELECT COUNT(*),DEPTNAME FROM EMPLOYEE E,DEPARTMENT D WHERE
E.DEPTID=D.DEPTID GROUP BY DEPTNAME;
9
4. SELECT DISTINCT(DESIGNATION) FROM EMPLOYEE;
Table: Teacher
10
SELECT T_ID,NAME, DEPARTMENT FROM TEACHER WHERE NAME LIKE
‘S%’ ORDER BY NAME;
4. Remove the details of History department teachers.
DELETE FROM TEACHER WHERE DEPARTMENT=’HISTORY’;
5. Modify the age and salary of Shiv as 46 and 24000 respectively.
UPDATE TEACHER SET AGE=46 AND SALARY=24000 WHERE
NAME=’SHIV’;
6. Increment the salary of Mathematics department teachers by 500.
UPDATE TEACHER SET SALARY=SALARY+500 WHERE
DEPARTMENT=’MATHEMATICS’;
7. Display the Name, gender and department of teachers joined after August
2019.
SELECT NAME, GENDER, DEPARTMENT FROM TEACHER WHERE
DATE_OF_JOIN > ‘2019-08-31’;
8. Display the details of teachers whose age in the range of 35 to 40 (both
included).
SELECT * FROM TEACHER WHERE AGE BETWEEN 35 AND 40;
9. Remove all teacher details from the table.
DELETE FROM TEACHER;
10. Display the male teachers who belong to Computer department.
SELECT * FROM TEACHER WHERE GENDER=’M’ AND
DEPARTMENT=’COMPUTER’;
11. Display the no of records in the teacher table.
SELECT COUNT(*) FROM TEACHER;
12. Display the minimum and maximum salary of each department.
SELECT MIN(SALARY),MAX(SALARY), DEPARTMENT FROM TEACHER
GROUP BY DEPARTMENT;
13. Display the count of male & female staff.
SELECT COUNT(GENDER), GENDER FROM TEACHER GROUP BY
GENDER;
14. Display the sum of salary of each department.
SELECT SUM(SALARY), DEPARTMENT FROM TEACHER GROUP BY
DEPARTMENT;
15. Display the sum of salary of each department whose no.of teachers is greater
than 2.
SELECT SUM(SALARY), DEPARTMENT FROM TEACHER GROUP BY
DEPARTMENT HAVING COUNT(*) > 2;
16. Rename the Date_of_join column as join_date.
ALTER TABLE TEACHER CHANGE DATE_OF_JOIN JOIN_DATE DATE;
17. Display the id,name & department of teachers who has salary greater than
25000.
SELECT T_ID,NAME, DEPARTMENT FROM TEACHER WHERE
SALARY>25000;
18. Display the details of teacher who does not belong to history department.
SELECT * FROM TEACHER WHERE DEPARTMET NOT IN (‘HISTORY’);
SELECT * FROM TEACHER WHERE NOT DEPARTMENT = 'HISTORY';
SELECT * FROM TEACHER WHERE DEPARTMENT <>'HISTORY';
SELECT * FROM TEACHER WHERE DEPARTMENT !='HISTORY';
19. Display the name & department of teachers whose name starts with Sh
followed by 3 characters.
SELECT NAME,DEPARTMENT FROM TEACHER WHERE NAME
LIKE “SH_ _ _” ;
20. Display the details of teachers whose data of join is not null.
11
SELECT * FROM TEACHER WHERE DATE_OF_JOIN IS NOT NULL;
21. Display the History teachers whose salary is greater than 30000.
SELECT * FROM TEACHER WHERE SALARY>30000 AND
DEPARTMENT=’HISTORY’;
22. Display the department column without repeated values.
SELECT DISTINCT DEPARTMENT FROM TEACHER;
23. Display the count of department without repeated values.
SELECT COUNT(DISTINCT DEPARTMENT) FROM TEACHER;
Write the output of given SQL statements.
1. select name,department, salary from teacher where salary<30000;
12
TABLE: EMP
TABLE: DEPT
13
Write the SQL statements
1. Display empno,empname, designation and department name of all employees.
SELECT EMPNO,EMPNAME,DESIGNATION, DEPTNAME FROM EMP NATURAL
JOIN DEPT;
2. Show the details of employees who belong to Accounts & Research department.
SELECT * FROM EMP NATURAL JOIN DEPT WHERE DEPTNAME
IN ('ACCOUNTS','RESEARCH');
3. Display empname, deptname and location of employees who are from either
Mumbai or from Delhi.
SELECT EMPNAME,DEPTNAME,LOCATION FROM EMP E,DEPT D WHERE
E.DEPTID=D.DEPTID AND LOCATION IN('MUMBAI','DELHI');
OR
SELECT EMPNAME,DEPTNAME,LOCATION FROM EMP NATURAL JOIN DEPT
WHERE LOCATION IN('MUMBAI','DELHI');
4. Display the Empno,empname, deptid and deptname of employees who are not
getting commission.
SELECT EMPNO,EMPNAME,DEPTID,DEPTNAME FROM EMP NATURAL JOIN
DEPT WHERE COMM IS NULL;
OR
SELECT EMPNO,EMPNAME,E.DEPTID,DEPTNAME FROM EMP E, DEPT D
WHERE E.DEPTID=D.DEPTID AND COMM IS NULL;
5. Display empname, city, designation & date of join of employees who joined in
the year 1990.
SELECT EMPNAME,CITY,DESIGNATION,DOJ FROM EMP WHERE DOJ
BETWEEN '1990-01-01' AND '1990-12-31';
6. Display Empid, empname, deptid, deptname, location and mgrid of employees
from personnel department and whose name starts with ‘S’.
SELECT EMPNO,EMPNAME, DEPTID,DEPTNAME, LOCATION,MGRID FROM
EMP NATURAL JOIN DEPT WHERE DEPTNAME='PERSONNEL' AND
EMPNAME LIKE 'S%';
7. Display empname,designation, salary of employees getting salary in the range
1000 and 2000 (both not included).
SELECT EMPNAME,DESIGNATION,SAL FROM EMP WHERE SAL>1000 AND
SAL<2000;
8. Display the details of employees from employee table who are not manager.
SELECT * FROM EMP WHERE DESIGNATION !='MANAGER';
9. Display deptid,deptname, location of Personnel or Research department.
SELECT DEPTID,DEPTNAME,LOCATION FROM DEPT WHERE
DEPTNAME='PERSONNEL' OR DEPTNAME='RESEARCH';
OR
14
SELECT DEPTID,DEPTNAME,LOCATION FROM DEPT WHERE DEPTNAME IN
('PERSONNEL' ,'RESEARCH');
10. Display empname,designation, deptname of employees who are not located in
Delhi.
SELECT EMPNAME,DESIGNATION,DEPTNAME FROM EMP NATURAL JOIN
DEPT WHERE LOCATION !='DELHI';
11. Display the details of employees from emp table whose salary is less than
1000.
SELECT * FROM EMP WHERE SAL<1000;
12. Display deptid,deptname,empname & commission of employees who are
commissioned.
SELECT DEPTID,DEPTNAME,EMPNAME,COMM FROM EMP NATURAL JOIN
DEPT WHERE COMM IS NOT NULL;
13. List the primary key, alternate & candidate keys from both the table.
14. Show the minimum, maximum and average salary of managers.
SELECT MIN(SAL),MAX(SAL),AVG(SAL),DESIGNATION FROM EMP WHERE
DESIGNATION='MANAGER' GROUP BY DESIGNATION;
15. Count the number of clerks in the organization.
SELECT COUNT(*), DESIGNATION FROM EMP WHERE
DESIGNATION='CLERK' GROUP BY DESIGNATION;
16. Display the designation-wise list of employees with name, salary and date of
joining.
SELECT EMPNAME,SAL,DOJ,DESIGNATION FROM EMP ORDER BY
DESIGNATION;
17. Count the number of employees who are not getting commission.
SELECT COUNT(*) FROM EMP WHERE COMM IS NULL;
18. Show the average salary for all departments with more than 5 working people.
SELECT AVG(SAL), DEPTNAME FROM EMP NATURAL JOIN DEPT GROUP BY
DEPTNAME HAVING COUNT(*)>5;
19. List the count of employees grouped by deptid.
SELECT COUNT(*),DEPTID FROM EMP NATURAL JOIN DEPT GROUP BY
DEPTID;
20. Display the maximum salary of employees in each department;
SELECT MAX(SAL),DEPTNAME FROM EMP NATURAL JOIN DEPT GROUP BY
DEPTNAME;
21. Display the name of employees along with their designation & department
name.
SELECT EMPNAME,DEPTNAME,DESIGNATION FROM EMP NATURAL JOIN
DEPT;
22. Count the number of employees working in ACCOUNTS department.
SELECT COUNT(*) FROM EMP NATURAL JOIN DEPT WHERE
DEPTNAME="ACCOUNTS";
15