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

SQL_practicesheet_ANSKEY

The document provides a comprehensive set of SQL commands for various operations including creating tables, inserting records, updating, and deleting data in a database. It covers multiple tables such as PRODUCT, Employee, and StudAdmin, along with examples of queries to manipulate and retrieve data. Additionally, it includes activities and exercises for practicing SQL queries related to student and employee data management.

Uploaded by

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

SQL_practicesheet_ANSKEY

The document provides a comprehensive set of SQL commands for various operations including creating tables, inserting records, updating, and deleting data in a database. It covers multiple tables such as PRODUCT, Employee, and StudAdmin, along with examples of queries to manipulate and retrieve data. Additionally, it includes activities and exercises for practicing SQL queries related to student and employee data management.

Uploaded by

devak k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SQL Class test

Write the SQL command to perform the following operations


1. Create a table PRODUCT with given column specifications
P_id char 4 primary key
P_name varchar 15 not null
Manufacturer char 4
Price decimal 42
Ans: CREATE TABLE PRODUCT(P_ID CHAR(4) PRIMARY KEY, P_NAME
VARCHAR(15) NOT NULL, MANUFACTURER CHAR(4), PRICE
DECIMAL(4,2));
2. Display the structure of product table.
DESC PRODUCT
3. Display all databases in SQL.
SHOW DATABASES;
4. Create a new database school.
CREATE DATABASE SCHOOL
5. Open the database school.
USE SCHOOL
6. Display all tables in school database
SHOW TABLES;
7. Insert the given values in Product table (separately)
a) TP01, Talcum Powder, LAK, 40
INSERT INTO PRODUCT VALUES(‘TP01’,’TALCUM POWDER’,’LAK’,40);
b) FW05, Face wash, null, 45.50
INSERT INTO PRODUCT VALUES(‘FW05’,’FACE WASH’,NULL,45.50);

8. Display the contents in the given table.


SELECT * FROM PRODUCT
9. Remove the details of Face wash from the given table
DELETE FROM PRODUCT WHERE P_NAME=”FACE WASH”;
10. Remove the entries of manufacturer LAK from the table.
DELETE FROM PRODUCT WHERE MANUFACTURER=”LAK”;
11. Change the data type p_name to varchar 20.
ALTER TABLE PRODUCT MODIFY P_NAME VARCHAR(20);
12. Add a new column discount with data type integer to the given table.
ALTER TABLE PRODUCT ADD DISCOUNT INTEGER;
13. Remove the primary key from the given table.
14. ALTER TABLE PRODUCT DROP PRIMARY KEY;
15. Rename the price column to Pr_price
ALTER TABLE PRODUCT CHANGE PRICE PR_PRICE VARCHAR(15);

16. Remove the column discount from Product table.


ALTER TABLE PRODUCT DROP DISCOUNT;
17. Remove the records with p_name as Soap & manufacturer as SAK
DELETE FROM PRODUCT WHERE P_NAME=”SOAP” AND
MANUFACTURER=”SAK”;
1
18. Remove all the records from the Product table.
DELETE FROM PRODUCT;
19. Remove the table Product from the database.
DROP TABLE PRODUCT;
20. Delete the database school.
DROP DATABASE SCHOOL;
21. In Product table set P_id as the primary key
ALTER TABLE PRODUCT ADD PRIMARY KEY(P_ID);

P_ID P_NAME MANUFACTURER PRICE


TP01 TALCUM POWDER LAK 130
FW05 FACE WASH XYZ 120
BS01 SOAP SAK 45
SH06 SOAP XYZ 40
22. Modify the product name of product id TP01 as Detergent and price as 230.
UPDATE PRODUCT SET P_NAME=”DETERGENT”, PRICE=230 WHERE
P_ID=”TP01”;
23. Multiply the price of each product by 2 in the product table.
UPDATE PRODUCT SET PRICE=PRICE*2;
24. Update the price of product SOAP to 60.
UPDATE PRODUCT SET PRICE =60 WHERE P_NAME=”SOAP”;
25. Remove the records of manufacturer XYZ from Product table.
DELETE FROM PRODUCT WHERE MANUFACTURER=”XYZ”;
26. A table, ITEM has been created in a database with the following fields:
ITEMCODE, NAME, QTY, PRICE
Give the SQL command to rename the column Name(varchar(15)) as ITEMNAME
in the ITEM table
ALTER TABLE PRODUCT CHANGE NAME ITEMNAME VARCHAR(15);
27. Identify the candidate & alternate keys from the given table.
CANDIDATE KEYS - P_ID, PRICE
ALTERNATE KEYS - PRICE

CLASS XII – SQL PRACTICE SHEET


Create a table Employee in school database with following fields.
Empno integer primary key
Empname varchar 15 not null
Desig varchar 25
Salary decimal(8,2) / numeric(8,2)
Gender char 1
Create table employee(Empno int primary key, Empname varchar(15) not null, Desig
varchar(25), salary decimal(8,2), Gender char(1));
1. Create a table “StudAdmn” with the given column specifications
Adm_No integer primary key
First_Name Varchar 15
Last_Name varchar 15
DOB date

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)

Insert into StudAdmin values(2013, ‘Jatin’, ‘Kumar’, ‘1998-02-05’);


Insert into StudAdmin values(2011, ‘Mona’, ‘Sinha’, ‘2004-09-24’);

3. Add a new column (age integer) in students table.


Alter table studadmin add age integer;
4. Change the data type of Last_name to varchar(20)
Alter table studadmin modify Last_name varchar(20);
5. Modify the age column value as 25 for student Jatin.
Update studadmin set age=25 where First_name=”Jatin”;
6. Delete the record of Admission number2011
Delete from studadmin where Adm_no=2011;

Create a table StudDtls with the given column specification,


Rollno integer primary key
Name varchar 15 not null
Gender char 1
Marks decimal 5,2
Class char 3
Stream varchar 20

i) Display all the details of rollno 3


Select * from StudDtls where rollno=3;
ii) Display rollno, name and stream from students table.
Select rollno,name, stream from studDtls;
iii) Display the details of students whose marks is less than 45.
Select * from studDtls where marks<45;
iv) Display rollno,name,Class and stream of students whose stream is not Biomaths.
Select rollno,name,class, stream from studDtls where not stream= ‘Biomaths’;
Select rollno,name,class, stream from studDtls where stream <> ‘Biomaths’;
v) Display stream from students table
Select stream from studdtls;
vi) Display stream column from students table by eliminating duplicate values.
Select distinct stream from studdtls;
vii) Display Class column from students table by eliminating Duplicate values.
Select distinct class from studdtls;
viii) Display rollno,name,class and marks of all students by incrementing marks by 10
Select rollno,name,class,marks+10 from studdtls;

Table name: Students

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

Rollno Name Gender Marks Class Stream Dob


1 Arjun M 43 12A COMPUTER 2010-08-02
2 Ardra F 47 12A BIOMATHS 2010-10-10
3 Amith M 45 12B BIOIP 2011-11-18
4 Payal Sharma F 44 12A BIOMATHS 2011-01-22
5 Sakshi Sharma F 48 12B BIOIP 2010-07-11

SQL Query

1) select * from student;


2) select Rollno,Name,Class,Stream from student where Gender='F';
3) select Rollno,Name from student where Marks is null;
4) select * from student where Dob between '2010-10-07' and '2011-02-04';
5) select Name,Class,Stream from student where Name like ‘Ar_ _ _’;
6) select Name,Gender,Marks from student where Name like '%sharma';
7) select count(*),Stream from student group by stream;
8) select avg(marks),stream from student group by stream having sum(marks)>45;
9) select Name, Marks, Class from student order by Name;
10) select * from student where stream in (‘BioIP’,’computer’);

5
Table: Employee

EmpNo EmpName DeptId Designation


6
E001 Ravi 101 Manager
E002 Rachel 201 Sr.Executive
E003 Sam 101 Executive
E004 Avinash 101 Clerk
E005 Peter 301 Manager

Table: Department

DeptId DeptName Location


101 Sales Kochi
201 Marketing Chennai
301 Production Bangalore

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";

3. Select Empno,Empname,E.Deptid,Designation, DeptName from Employee E,


Department D where E.DeptId=D.DeptId and Desigantion=’Manager’;
or
select empno,empname,designation,deptid,deptname from employee natural join
department where designation="manager";

4. Select Empname,Designation, DeptName,location from Employee E, Department D


where E.DeptId=D.DeptId and location in('kochi','bangalore') order by empname;
or
select empname,designation,deptname,location from employee natural join
department where location in("kochi",'bangalore') order by empname;

5. select count(*), deptname from Employee E, Department D where


E.DeptId=D.DeptId group by deptname;
or
select count(*), deptname from Employee natural join Department group by
deptname;

6. Select Empno,Empname,E.deptid,Designation, DeptName,location from Employee E,


Department D where E.DeptId=D.DeptId and empname like 'R%';
or
Select Empno,Empname,deptid,Designation, DeptName,location from Employee
natural join Department where empname like 'R%';

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”;

2. SELECT MIN(MARKS), STREAM FROM STUDENT GROUP BY STREAM;

3. SELECT NAME,CLASS,MARKS FROM STUDENT WHERE NAME LIKE ‘AR_ _ _’;

8
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%";

9
4. SELECT DISTINCT(DESIGNATION) FROM EMPLOYEE;

5. SELECT * FROM EMPLOYEE WHERE DESIGNATION IN('CLERK','EXECUTIVE');

Table: Teacher

Write the SQL commands for the given questions


1. Display the details of teachers who belong to History & Mathematics.
SELECT * FROM TEACHER WHERE DEPARTMENT IN
(‘HISTORY’,’MATHEMATICS’);
2. Display Name & department of teachers whose age is not entered.
SELECT NAME, DEPARTMENT FROM TEACHER WHERE AGE IS NULL;
3. Display id, name & department of teachers whose name starts with s in
ascending order of name.

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;

2. select name,department, salary from teacher where salary<30000 and not


department="history";
3. select name,department, salary,age from teacher where age between 31 and
42;
4. select name,department from teacher where tname like "%n";
5. select count(*),department from teacher group by department;
6. select count(*),department from teacher group by department having
count(*)>2;
7. select min(date_of_join),max(date_of_join),department from teacher group by
department;
8. select name, department,date_of_join from teacher where tname not like
"s%" order by tname desc;

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

You might also like